Skip to content

Commit

Permalink
actually add the vignette...
Browse files Browse the repository at this point in the history
  • Loading branch information
Kss2k committed Jan 7, 2025
1 parent ed208cd commit c5fe7cb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
6 changes: 4 additions & 2 deletions R/plot_interaction.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ plot_interaction <- function(x, z, y, xz = NULL, vals_x = seq(-3, 3, .001),
ci_width = 1.96, rescale = TRUE, ...) {
df <- simple_slopes(x = x, z = z, y = y, model = model, vals_x = vals_x, vals_z = vals_z,
rescale = rescale, ci_width = ci_width, ...)
df$cat_z <- as.factor(round(df$vals_z, digits))

df$cat_z <- as.factor(round(df$vals_z, digits))

# declare within the scope, to not get notes in R CMD check
std.error <- df$std.error
predicted <- df$predicted
cat_z <- df$cat_z
Expand Down Expand Up @@ -93,6 +94,7 @@ calc_se <- function(x, var, n, s) {
s * sqrt(1 / n + x ^ 2 / SSx)
}


#' Plot Interaction Effect Using the Johnson-Neyman Technique
#'
#' This function plots the simple slopes of an interaction effect across different values of a moderator variable using the Johnson-Neyman technique. It identifies regions where the effect of the predictor on the outcome is statistically significant.
Expand Down
92 changes: 92 additions & 0 deletions vignettes/simple_slopes.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
---
title: "simple slopes analysis"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{simple slopes analysis}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

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

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

# Simple Slopes Analysis

Simple slope 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 = c(0, 1), model = est1)
```

If you want to see the numerical values of the simple slopes, you can use the `simple_slopes()` function:

```{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)
simple_slopes(x = "X", z = "Z", y = "Y", vals_z = c(0, 1), model = est1)
```

The `simple_slopes()` function returns a simple_slopes object, which is a `data.frame`
with some additional attributes. It only has a single method (or technically, a generic function),
`print.simple_slopes()`, which prints the simple slopes in a easy-to-read format. If you want to
extract the simple slopes as a `data.frame`, you can use the `as.data.frame()` function:


```{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)
slopes <- simple_slopes(x = "X", z = "Z", y = "Y",
vals_z = c(0, 1), model = est1)
as.data.frame(slopes)
```


0 comments on commit c5fe7cb

Please sign in to comment.