Skip to content

Commit

Permalink
add test-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Kss2k committed Dec 17, 2024
1 parent 5073241 commit e05a35f
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Description:
<doi:10.1037/1082-989X.9.3.275>.
"Structural equation models of latent interactions: evaluation of alternative estimation strategies and indicator construction."
Muthén, L.K. and Muthén, B.O. (1998-2017).
"'Mplus' User’s Guide. Eighth Edition."
"'Mplus' User’s Guide. Eighth Edition."
<https://www.statmodel.com/>.
Rosseel Y (2012).
<doi:10.18637/jss.v048.i02>.
Expand All @@ -58,7 +58,7 @@ LazyData: true
RoxygenNote: 7.3.2
LinkingTo: Rcpp, RcppArmadillo
Imports: Rcpp, purrr, stringr, lavaan, rlang, MplusAutomation, nlme, dplyr,
mvnfast, stats, fastGHQuad, mvtnorm, ggplot2, parallel
mvnfast, stats, fastGHQuad, mvtnorm, ggplot2, parallel, plotly
Depends:
R (>= 3.5.0)
URL: https://modsem.org
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export(multiplyIndicatorsCpp)
export(parameter_estimates)
export(plot_interaction)
export(plot_jn)
export(plot_surface)
export(standardized_estimates)
export(trace_path)
export(var_interactions)
Expand Down
105 changes: 105 additions & 0 deletions man/plot_surface.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/testthat/test_lms.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ est1 <- modsem(m1, oneInt, method = "lms", optimize = TRUE, verbose = TRUE,
convergence = 1e-2, R.max = 50000)
plot_interaction("X", "Z", "Y", "X:Z", -3:3, c(-0.5, 0.5), est1)
print(summary(est1, adjusted.stat = TRUE))
plot_surface(x = "X", z = "Z", y = "Y", model = est1)

# PROBLEM:
# I have no clue why, but changing the ordering of how the interaction terms
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test_qml.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ est1 <- modsem(m1, data = oneInt, convergence = 1e-2, method = "qml",
robust.se = TRUE)
print(summary(est1, scientific = TRUE))
plot_interaction(x = "X", z = "Z", y = "Y", xz = "X:Z", vals_z = c(-0.5, 0.5), model = est1)
plot_surface(x = "X", z = "Z", y = "Y", model = est)
plot_surface(x = "X", z = "Z", y = "Y", model = est1)


tpb <- '
# Outer Model (Based on Hagger et al., 2007)
Expand Down
36 changes: 30 additions & 6 deletions vignettes/plot_interactions.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ m1 <- "
Y ~ X + Z + X:Z
"
est1 <- modsem(m1, data = oneInt)
plot_interaction("X", "Z", "Y", "X:Z", vals_z = -3:3, range_y = c(-0.2, 0), model = est1)
plot_interaction("X", "Z", "Y", "X:Z", vals_z = -3:3,
range_y = c(-0.2, 0), model = est1)
```

Here is a different example using the `lms` approach in the theory of planned behavior model:
Expand Down Expand Up @@ -91,13 +92,12 @@ m1 <- '
visual ~ speed + textual + speed:textual
'
est <- modsem(m1, data = lavaan::HolzingerSwineford1939, method = "ca")
plot_jn(x = "speed", z = "textual", y = "visual", model = est, max_z = 6)
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
Expand All @@ -113,7 +113,31 @@ tpb <- "
"
est2 <- modsem(tpb, TPB, method = "qml")
plot_jn(x = "INT", z = "PBC", y = "BEH", model = est2,
min_z = -1.5, max_z = -0.5)
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}
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 = "X", z = "Z", y = "Y", model = est1)
```

0 comments on commit e05a35f

Please sign in to comment.