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, ...)

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.

Note

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.

Examples

data(efc) efc$c172code <- sjmisc::to_factor(efc$c172code) fit <- lm(barthtot ~ c12hour + c161sex + c172code * neg_c_7, data = efc) gginteraction(fit)
#> # A tibble: 63 x 5 #> x predicted conf.low conf.high group #> * <dbl> <dbl> <dbl> <dbl> <fct> #> 1 7 77.4 2.82 71.9 low level of education #> 2 7 76.5 1.77 73.0 intermediate level of education #> 3 7 70.9 3.26 64.5 high level of education #> 4 8 74.4 2.50 69.5 low level of education #> 5 8 74.3 1.55 71.2 intermediate level of education #> 6 8 69.4 2.89 63.7 high level of education #> 7 9 71.4 2.22 67.0 low level of education #> 8 9 72.0 1.35 69.4 intermediate level of education #> 9 9 67.9 2.55 62.9 high level of education #> 10 10 68.4 2.00 64.4 low level of education #> # ... with 53 more rows
# this would give the same results ggpredict(fit, terms = c("neg_c_7", "c172code"))
#> # A tibble: 63 x 5 #> x predicted conf.low conf.high group #> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 7 77.4 71.9 82.9 low level of education #> 2 7 76.5 73.0 80.0 intermediate level of education #> 3 7 70.9 64.5 77.3 high level of education #> 4 8 74.4 69.5 79.3 low level of education #> 5 8 74.3 71.2 77.3 intermediate level of education #> 6 8 69.4 63.8 75.1 high level of education #> 7 9 71.4 67.0 75.7 low level of education #> 8 9 72.0 69.4 74.7 intermediate level of education #> 9 9 67.9 62.9 72.9 high level of education #> 10 10 68.4 64.4 72.3 low level of education #> # ... with 53 more rows
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()