| gginteraction {ggeffects} | R Documentation |
gginteraction() computes marginal effects of interaction terms.
It internally calls effect and
puts the result into tidy data frames.
gginteraction(model, mdrt.values = "minmax", swap.pred = FALSE, ci.lvl = 0.95, x.as.factor = FALSE, ...)
model |
A fitted model object, or a list of model objects. Any model that is supported by the effects-package should work. |
mdrt.values |
Indicates which values of the moderator variable should be used to calculate marginal effects of the interaction.
|
swap.pred |
Logical, if |
ci.lvl |
Numeric, the level of the confidence intervals. For |
x.as.factor |
Logical, if |
... |
Further arguments passed down to |
gginteraction() only computes marginal effects for interaction terms,
in particular two-way interactions. Use ggeffect for
marginal effects for simple model terms. Or use ggpredict
for predictions from any model terms, including two- or three-way
interactions. gginteraction() internally calls effect() from
effects (like ggeffect() does), hence it may work for certain
models that are not yet supported by ggpredict(). Otherwise, there
should be no difference in the results between gginteraction() and
ggpredict(), or at least only minor differences (as effect()
holds categorical covariates constant at other values, see 'Details' in
ggeffect).
A tibble (with ggeffects class attribute) with consistent data columns:
xthe values of the model predictor to which the effect pertains, used as x-position in plots.
predictedthe predicted values, used as y-position in plots.
conf.lowthe lower bound of the confidence interval for the predicted values.
conf.highthe upper bound of the confidence interval for the predicted values.
groupthe name of x, used as grouping-aesthetics in plots.
data(efc)
efc$c172code <- sjmisc::to_factor(efc$c172code)
fit <- lm(barthtot ~ c12hour + c161sex + c172code * neg_c_7, data = efc)
gginteraction(fit)
# this would give the same results
ggpredict(fit, terms = c("neg_c_7", "c172code"))
library(ggplot2)
ggplot(gginteraction(fit), aes(x, predicted, colour = group)) +
geom_line()
dat <- gginteraction(fit)
ggplot(dat, aes(x, predicted, colour = group)) +
geom_line() +
labs(
colour = get_legend_title(dat),
x = get_x_title(dat),
y = get_y_title(dat),
title = get_title(dat)
) +
scale_color_manual(
values = c("red", "green", "blue"),
labels = get_legend_labels(dat)
)
# use continuous term on x-axis, but use values mean +/- sd as groups
dat <- gginteraction(fit, mdrt.values = "meansd", swap.pred = TRUE)
ggplot(dat, aes(x, predicted, colour = group)) + geom_line()