# LMS and QML approaches

    library(modsem)

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

Both the `LMS` and `QML` approaches work on most models, but interaction effects with endogenous variables can be tricky to estimate (see the vignette). Both approaches, particularly the `LMS` approach, are computationally intensive and are partially implemented in C++ (using `Rcpp` and `RcppArmadillo`). Additionally, starting parameters are optimized, to generate good starting parameters for faster convergence. If you want to monitor the progress of the estimation process, you can use `verbose = TRUE`.

## A Simple Example

Here is an example of the `LMS` approach for a simple model. By default, the `summary()` function calculates fit measures compared to a null model (i.e., the same model without an interaction term).

    library(modsem)
    m1 <- '
    # Outer Model
      X =~ x1 + x2 + x3
      Z =~ z1 + z2 + z3
      Y =~ y1 + y2 + y3

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

    lms1 <- modsem(m1, oneInt, method = "lms")
    # Standardized estimates
    summary(lms1, standardized = TRUE)

Here is the same example using the `QML` approach:

    qml1 <- modsem(m1, oneInt, method = "qml", robust.se = TRUE) # get robust standard errors
    summary(qml1, fit = TRUE, adjusted = TRUE) # get additional fit indices

## A More Complicated Example

Below is an example of a more complex model based on the theory of planned behavior (TPB), which includes two endogenous variables and an interaction between an endogenous and exogenous variable. When estimating more complex models with the `LMS` approach, it is recommended to increase the number of nodes used for numerical integration. By default, the number of nodes is set to 24, but this can be increased using the `nodes` argument. The `nodes` argument has no effect on the `QML` approach.

When there is an interaction effect between an endogenous and exogenous variable, it is recommended to use at least 32 nodes for the `LMS` approach. You can also obtain robust standard errors by setting `robust.se = TRUE` in the `modsem()` function.

NOTE: If you want the `LMS` approach to produce results as similar as possible to `Mplus`, you should consider increasing the number of nodes using the `nodes` argument, and or using a quasi-adaptive quadrature by setting `adaptive.quad=TRUE`.

    # ATT = Attitude
    # PBC = Perceived Behavioral Control
    # INT = Intention
    # SN = Subjective Norms
    # BEH = Behavior
    tpb <- '
    # Outer Model (Based on Hagger et al., 2007)
      ATT =~ att1 + att2 + att3 + att4 + att5
      SN =~ sn1 + sn2
      PBC =~ pbc1 + pbc2 + pbc3
      INT =~ int1 + int2 + int3
      BEH =~ b1 + b2

    # Inner Model (Based on Steinmetz et al., 2011)
      INT ~ ATT + SN + PBC
      BEH ~ INT + PBC
      BEH ~ INT:PBC
    '

    lms2 <- modsem(tpb, TPB, method = "lms", nodes = 32)
    summary(lms2)

    qml2 <- modsem(tpb, TPB, method = "qml")
    summary(qml2, standardized = TRUE) # Standardized estimates