---
title: "non-centered interaction terms (LMS and QML)"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{non-centered interaction terms (LMS and QML)}
  %\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)
```

# Non-centered interaction terms
Using the LMS and QML approaches it is possible to estimate interaction terms where the
means of the latent variables are not centered (i.e., they have non-zero means).

Here we can see an example using the `TPB` dataset:
```{r}
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

# Adding Latent Intercepts
  INT ~ 1
  BEH ~ 1
  PBC ~ 1
  SN  ~ 1
  ATT ~ 1
'

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

Comparing this to the estimates we get when `PBC` and `INT` have zero means,
we see that the coefficients `BEH~PBC` and `BEH~INT` are drastically changed.
This is not a bug, and is a function of the interaction effect rescaling the
coefficients, when not centered at zero. When using the `standardized_estimates`
function, or `summary(est, standardized = TRUE)` the interaction effect is
centered, and we can see that the coefficients `BEH~PBC` and `BEH~INT` are
rescaled once again.

```{r}
summary(est, standardized = TRUE, centered = TRUE)
```
It is also possible to get the centered solution using the `centered_estimates()` function.
Note, that `centered_estimates()` removes the mean structure of the model all together.

```{r}
centered_estimates(est)
```
