This function decomposes a 2-way x1*x2 interaction into simple effects, and provides means, pairwise comparisons, and a plot.
Usage
decompose_interaction(
model,
df = NULL,
x1,
x2,
at_x1 = NULL,
at_x2 = NULL,
ci = FALSE,
...
)Arguments
- model
A model object compatible with
emmeans(e.g.lm)- df
data.frame or NULL if
modelhasmodel$model- x1, x2
(character) variable name
- at_x1, at_x2
(numeric or character vector)
x1andx2values at which to calculate and compare means (ifNULL, all unique values)- ci
(logical) Whether to plot confidence intervals (default
FALSE)- ...
additional arguments passed to the emmeans functions (see Details)
Value
List with elements:
- slopes
Simple slopes and pairwise comparisons (from calling
emmeans::emtrends())- means
(adjusted) means for every combination of
x1*x2(may need to specifyat_x1and/orat_x2values)- contrasts
Comparisons of (adjusted)
x1means at each value ofx2(may need to specifyat_x1and/orat_x2values)- plot
Plot (
ggplot2line graph using data fromemmeans::emmip())
Details
This function uses the facilities provided by the library emmeans, and follows the procedure outlined by UCLA OARC Statistical and Data Analytics. It might be helpful to execute the function with x1 and x2 inverted, and see which set of results and plot make most sense for your scenario.
Note that if x1 or x2 has many different distinct values and at_x1
or at_x2 is not specified, then not all elements of the returned list will be useful
($means and $contrasts in particular may be difficult to use).
If model is of class lavaan, an additional argument lavaan.DV must be
passed to specify the name of the outcome variable. Transfer of model
information to emmeans is handled by semTools::lavaan2emmeans().
Examples
if (FALSE) {
fit <- lm(SelfEsteem ~ Gender*AttitudeTowardsSchool, df)
fit |> decompose_interaction(x1="Gender", x2="AttitudeTowardsSchool")
}