---
title: "observed variables in the LMS- and QML approach"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{observed variables in the LMS- and QML approach}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
EVAL_DEFAULT <- TRUE
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = EVAL_DEFAULT
)
```

```{r setup}
library(modsem)
```

# The Latent Moderated Structural Equations (LMS) and the Quasi Maximum Likelihood (QML) Approach

As of version `1.0.16`, observed variables are supported (in most circumstances) for both the LMS and QML approaches.
Here we can see an example using only observed variables.

```{r}
library(modsem)

fit <- modsem('y1 ~ x1 + z1 + x1:z1', data = oneInt, method = "lms")
summary(fit, standardized = TRUE)
```

If you're using an older version of `modsem`, the rest of the vignette details how to handle observed variables
in the LMS and QML approaches. There might also be some cases where where converting observed variables to
latent ones might be a more reliable option. For example, the LMS approach might struggle if it has to integrate
along an observed variable.

## The LMS Approach

For the `LMS` approach, you can use the above-mentioned method in almost all cases, except when using an observed variable as a moderating variable. In the `LMS` approach, you typically select one variable in an interaction term as the moderator. The interaction effect is then estimated via numerical integration at `n` quadrature nodes of the moderating variable. However, this process requires that the moderating variable has an error term, as the distribution of the moderating variable is modeled as \( X \sim N(Az, \varepsilon) \), where \( Az \) is the expected value of \( X \) at quadrature point `k`, and \( \varepsilon \) is the error term. If the error term is zero, the probability of observing a given value of \( X \) will not be computable.

In most instances, the first variable in the interaction term is chosen as the moderator. For example, if the interaction term is `"X:Z"`, `"X"` will usually be chosen as the moderator. Therefore, if only one of the variables is latent, you should place the latent variable first in the interaction term. If both variables are observed, you must specify a measurement error (e.g., `"x1 ~~ 0.1 * x1"`) for the indicator of the first variable in the interaction term.

```{r}
# Interaction effect between a latent and an observed variable
m1 <- '
# Outer Model
  X =~ x1 # X is observed
  Z =~ z1 + z2 # Z is latent
  Y =~ y1 # Y is observed

# Inner model
  Y ~ X + Z
  Y ~ Z:X
'

lms1 <- modsem(m1, oneInt, method = "lms")

# Interaction effect between two observed variables
m2 <- '
# Outer Model
  X =~ x1 # X is observed
  Z =~ z1 # Z is observed
  Y =~ y1 # Y is observed
  x1 ~~ 0.1 * x1 # Specify a variance for the measurement error

# Inner model
  Y ~ X + Z
  Y ~ X:Z
'

lms2 <- modsem(m2, oneInt, method = "lms")
summary(lms2)
```
If you forget to specify a measurement error for the indicator of the first variable in the interaction term, you will receive an error message.

```{r, error = TRUE}
m2 <- '
# Outer Model
  X =~ x1 # X is observed
  Z =~ z1 # Z is observed
  Y =~ y1 # Y is observed

# Inner model
  Y ~ X + Z
  Y ~ X:Z
'

lms3 <- modsem(m2, oneInt, method = "lms")
```
**Note:** You only get an error message for `X`/`x1`, since `Z` is not modelled as a moderating variable in this example.

## The QML Approach

The estimation process for the `QML` approach differs from the `LMS` approach, and you do not encounter the same issue as in the `LMS` approach. Therefore, you don't need to specify a measurement error for moderating variables.

```{r}
m3 <- '
# Outer Model
  X =~ x1 # X is observed
  Z =~ z1 # Z is observed
  Y =~ y1 # Y is observed

# Inner model
  Y ~ X + Z
  Y ~ X:Z
'

qml3 <- modsem(m3, oneInt, method = "qml")
summary(qml3)
```
