---
title: "LMS and QML approaches"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{LMS and QML approaches}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
EVAL_DEFAULT <- FALSE
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 
Both the LMS- and QML approach works on most models, but interaction effects with 
endogenous can be a bit tricky to estimate (see the [vignette](https://kss2k.github.io/modsem/articles/interaction_two_etas.html). 
Both approaches (particularily the LMS approach) are quite computationally intensive, and are thus partly implemented in C++ (using Rcpp and RcppArmadillo). 
Additionally starting parameters are estimated using the double centering approach (and the means of the observed variables) 
are used to generate good starting parameters for faster convergence. If you want to see the progress of the estimation process you can use ´verbose = TRUE´.

## A Simple Example
Here you can see 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).

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

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

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

Here you can see the same example using the QML approach. 

```{r}
qml1 <- modsem(m1, oneInt, method = "qml")
summary(qml1)
```

## A more complicated example
Here you can see an example of a more complicated example using the model from the
theory of planned behaviour (TPB), where there are two endogenous variables, 
where there is an interaction between an endogenous and exogenous variable. 
When estimating more complicated models with the LMS-approach, 
it is recommended that you increase the number of 
nodes used for numerical integration. By default the number of nodes is set to 16,
and can be increased using the nodes argument. The argument has no effect on the QML approach.
When there is an interaction effect between an endogenous and exogenous variable, 
it is recommended that you use at least 32 nodes for the LMS-approach.
You can also get robust standard errors by setting `robust.se = TRUE` in the 
`modsem()` function.

Note: If you want the lms-approach to give as similar results as possible to
mplus, you would have to increase the number of nodes (e.g., `nodes = 100`).

```{r}
# ATT = Attitude, 
# PBC = Perceived Behavioural Control 
# INT = Intention 
# SN = Subjective Norms
# BEH = Behaviour
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
```

