---
title: "plotting interaction effects"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{plotting interaction effects}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

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

# Plotting Interaction Effects

Interaction effects can be plotted using the included `plot_interaction()` function. This function takes a fitted model object and the names of the two variables that are interacting. The function will plot the interaction effect of the two variables, where:

- The x-variable is plotted on the x-axis.
- The y-variable is plotted on the y-axis.
- The z-variable determines at which points the effect of x on y is plotted.
  
The function will also plot the 95% confidence interval for the interaction effect. 
Note that the `vals_z` argument (as well as the values of `x`) are scaled by the
mean and standard deviation of the variables. Unless the `rescale` argument is set to `FALSE`.

Here is a simple example using the double-centering approach:

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

# Inner Model
  Y ~ X + Z + X:Z
"
est1 <- modsem(m1, data = oneInt)
plot_interaction(x = "X", z = "Z", y = "Y",
                 vals_z = -3:3, model = est1)
```

Here is a different example using the `lms` approach in the theory of planned behavior model:

```{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 ~ PBC:INT
"

est2 <- modsem(tpb, TPB, method = "lms")
plot_interaction(x = "INT", z = "PBC", y = "BEH",
                 vals_z = c(-0.5, 0.5), model = est2)
```

# Plotting Johnson-Neyman Regions

The `plot_jn()` function can be used to plot Johnson-Neyman regions for a given interaction effect. 
This function takes a fitted model object, the names of the two variables that are interacting, 
and the name of the interaction effect. The function will plot the Johnson-Neyman regions for the interaction effect.

The `plot_jn()` function will also plot the 95% confidence interval for the interaction effect.

`x` is the name of the x-variable, `z` is the name of the z-variable, and `y` is the name of the y-variable.
`model` is the fitted model object. The argument `min_z` and `max_z` are used to specify the range of values for the 
moderating variable.

Here is an example using the `ca` approach in the Holzinger-Swineford (1939) dataset:

```{r}
m1 <-  ' 
  visual  =~ x1 + x2 + x3 
  textual =~ x4 + x5 + x6
  speed   =~ x7 + x8 + x9

  visual ~ speed + textual + speed:textual
'

est1 <- modsem(m1, data = lavaan::HolzingerSwineford1939, method = "ca")
plot_jn(x = "speed", z = "textual", y = "visual", model = est1, max_z = 6)
```

Here is another example using the `qml` approach in the theory of planned behavior model:
```{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 ~ PBC:INT
"

est2 <- modsem(tpb, TPB, method = "qml")
plot_jn(x = "INT", z = "PBC", y = "BEH", model = est2, 
        min_z = -1.5, max_z = -0.5)
```

# Plotting (3D) Surface Plots
The `plot_surface()` function can be used to plot 3D surface plots for a given interaction effect.
This function takes a fitted model object, the names of the two variables that are interacting,
and the name of the dependent variable. The function will plot the 3D surface plot for the interaction effect.

```{r, eval = EVAL_DEFAULT}
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 ~ PBC:INT
"

est2 <- modsem(tpb, TPB, method = "qml")
plot_surface(x = "INT", z = "PBC", y = "BEH", model = est2)
```