# observed variables in the LMS- and QML approach

    library(modsem)

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

In contrast to the other approaches, the `LMS` and `QML` approaches are designed to handle latent variables only. Thus, observed variables cannot be used as easily as in the other approaches. One way to get around this is by specifying your observed variable as a latent variable with a single indicator. `modsem()` will, by default, constrain the factor loading to `1` and the residual variance of the indicator to `0`. The only difference between the latent variable and its indicator, assuming it is an exogenous variable, is that it has a zero-mean. This approach works for both the `LMS` and `QML` methods in most cases, with two exceptions.

## 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 N(Az, ) ), where ( Az ) is the expected value of ( X ) at quadrature point `k`, and ( ) 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.

    library(modsem)

    # 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)
    #> Estimating baseline model (H0)
    #>
    #> modsem (version 1.0.8):
    #>   Estimator                                         LMS
    #>   Optimization method                         EM-NLMINB
    #>   Number of observations                           2000
    #>   Number of iterations                               38
    #>   Loglikelihood                                -6632.86
    #>   Akaike (AIC)                                 13285.71
    #>   Bayesian (BIC)                               13341.72
    #>
    #> Numerical Integration:
    #>   Points of integration (per dim)                    24
    #>   Dimensions                                          1
    #>   Total points of integration                        24
    #>
    #> Fit Measures for H0:
    #>   Loglikelihood                                   -9369
    #>   Akaike (AIC)                                 18756.46
    #>   Bayesian (BIC)                               18806.87
    #>   Chi-square                                       0.00
    #>   Degrees of Freedom (Chi-square)                     0
    #>   P-value (Chi-square)                            0.000
    #>   RMSEA                                             Inf
    #>
    #> Comparative fit to H0 (no interaction effect)
    #>   Loglikelihood change                          2736.38
    #>   Difference test (D)                           5472.75
    #>   Degrees of freedom (D)                              1
    #>   P-value (D)                                     0.000
    #>
    #> R-Squared:
    #>   Y                                               0.494
    #> R-Squared Null-Model (H0):
    #>   Y                                               0.335
    #> R-Squared Change:
    #>   Y                                               0.160
    #>
    #> Parameter Estimates:
    #>   Coefficients                           unstandardized
    #>   Information                                  expected
    #>   Standard errors                              standard
    #>
    #> Latent Variables:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   X =~
    #>     x1               1.000
    #>   Z =~
    #>     z1               1.000
    #>   Y =~
    #>     y1               1.000
    #>
    #> Regressions:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   Y ~
    #>     X                0.663      0.036    18.32    0.000
    #>     Z                0.482      0.035    13.90    0.000
    #>     X:Z              0.586      0.028    20.86    0.000
    #>
    #> Intercepts:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>     x1               1.023      0.028    36.92    0.000
    #>     z1               1.011      0.026    39.05    0.000
    #>     y1               1.057      0.023    46.12    0.000
    #>     Y                0.000
    #>     X                0.000
    #>     Z                0.000
    #>
    #> Covariances:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   X ~~
    #>     Z                0.208      0.032     6.57    0.000
    #>
    #> Variances:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>     x1               0.100
    #>     z1               0.000
    #>     y1               0.000
    #>     X                1.028      0.044    23.57    0.000
    #>     Z                1.184      0.040    29.65    0.000
    #>     Y                1.323      0.041    32.65    0.000

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.

    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")
    #> Error: The variance of a moderating variable of integration has an indicator with zero residual variance!
    #> This will likely not work with the LMS approach, see:
    #>    `vignette('observed_lms_qml', 'modsem')` for more information.
    #>
    #> The following indicators have zero residual variance:
    #>   -> x1

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.

    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)
    #> Estimating baseline model (H0)
    #>
    #> modsem (version 1.0.8):
    #>   Estimator                                         QML
    #>   Optimization method                            NLMINB
    #>   Number of observations                           2000
    #>   Number of iterations                               11
    #>   Loglikelihood                                -9117.07
    #>   Akaike (AIC)                                 18254.13
    #>   Bayesian (BIC)                               18310.14
    #>
    #> Fit Measures for H0:
    #>   Loglikelihood                                   -9369
    #>   Akaike (AIC)                                 18756.46
    #>   Bayesian (BIC)                               18806.87
    #>   Chi-square                                       0.00
    #>   Degrees of Freedom (Chi-square)                     0
    #>   P-value (Chi-square)                            0.000
    #>   RMSEA                                             Inf
    #>
    #> Comparative fit to H0 (no interaction effect)
    #>   Loglikelihood change                           252.17
    #>   Difference test (D)                            504.33
    #>   Degrees of freedom (D)                              1
    #>   P-value (D)                                     0.000
    #>
    #> R-Squared:
    #>   Y                                               0.470
    #> R-Squared Null-Model (H0):
    #>   Y                                               0.320
    #> R-Squared Change:
    #>   Y                                               0.150
    #>
    #> Parameter Estimates:
    #>   Coefficients                           unstandardized
    #>   Information                                  observed
    #>   Standard errors                              standard
    #>
    #> Latent Variables:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   X =~
    #>     x1               1.000
    #>   Z =~
    #>     z1               1.000
    #>   Y =~
    #>     y1               1.000
    #>
    #> Regressions:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   Y ~
    #>     X                0.605      0.028    21.26    0.000
    #>     Z                0.490      0.028    17.55    0.000
    #>     X:Z              0.544      0.023    23.95    0.000
    #>
    #> Intercepts:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>     x1               1.023      0.024    42.83    0.000
    #>     z1               1.011      0.024    41.56    0.000
    #>     y1               1.066      0.034    31.64    0.000
    #>     Y                0.000
    #>     X                0.000
    #>     Z                0.000
    #>
    #> Covariances:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>   X ~~
    #>     Z                0.210      0.026     7.95    0.000
    #>
    #> Variances:
    #>                   Estimate  Std.Error  z.value  P(>|z|)
    #>     x1               0.000
    #>     z1               0.000
    #>     y1               0.000
    #>     X                1.141      0.036    31.62    0.000
    #>     Z                1.184      0.037    31.62    0.000
    #>     Y                1.399      0.044    31.62    0.000