Learn R Programming

ggeffects (version 0.3.3)

gginteraction: Get marginal effects for two-way interactions from models

Description

gginteraction() computes marginal effects of interaction terms. It internally calls effect and puts the result into tidy data frames.

Usage

gginteraction(model, mdrt.values = "minmax", swap.pred = FALSE,
  ci.lvl = 0.95, x.as.factor = FALSE, ...)

Arguments

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.

"minmax"

(default) minimum and maximum values (lower and upper bounds) of the moderator are used to plot the interaction between independent variable and moderator.

"meansd"

uses the mean value of the moderator as well as one standard deviation below and above mean value to plot the effect of the moderator on the independent variable.

"zeromax"

is similar to the "minmax" option, however, 0 is always used as minimum value for the moderator. This may be useful for predictors that don't have an empirical zero-value, but absence of moderation should be simulated by using 0 as minimum.

"quart"

calculates and uses the quartiles (lower, median and upper) of the moderator value.

"all"

uses all values of the moderator variable. Note that this option only applies to type = "eff", for numeric moderator values.

swap.pred

Logical, if TRUE, the predictor (defining the x-position) and the moderator (defining the groups) in an interaction are swapped. By default, the first interaction term is considered as moderator and the second term is used to define the x-position.

ci.lvl

Numeric, the level of the confidence intervals. For ggpredict(), use ci.lvl = NA, if confidence intervals should not be calculated (for instance, due to computation time).

x.as.factor

Logical, if TRUE, preserves factor-class as x-column in the returned data frame. By default, the x-column is always numeric.

...

Further arguments passed down to effect.

Value

A tibble (with ggeffects class attribute) with consistent data columns:

x

the values of the model predictor to which the effect pertains, used as x-position in plots.

predicted

the predicted values, used as y-position in plots.

conf.low

the lower bound of the confidence interval for the predicted values.

conf.high

the upper bound of the confidence interval for the predicted values.

group

the name of x, used as grouping-aesthetics in plots.

Examples

Run this code
# NOT RUN {
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()

# }

Run the code above in your browser using DataLab