Learn R Programming

sjPlot (version 2.1.0)

sjp.int: Plot interaction effects of (generalized) linear (mixed) models

Description

Plot regression (predicted values) or probability lines (predicted probabilities) of significant interaction terms to better understand effects of moderations in regression models. This function accepts following fitted model classes:
  • linear models (lm)
  • generalized linear models (glm)
  • linear mixed effects models (lmer)
  • generalized linear mixed effects models (glmer)
  • non-linear mixed effects models (nlmer)
  • linear mixed effects models (lme, but only for type = "eff")
  • generalized least squares models (gls, but only for type = "eff")
  • panel data estimators (plm)

Note that beside interaction terms, also the single predictors of each interaction (main effects) must be included in the fitted model as well. Thus, lm(dep ~ pred1 * pred2) will work, but lm(dep ~ pred1:pred2) won't!

Usage

sjp.int(fit, type = c("eff", "cond", "emm"), int.term = NULL, int.plot.index = NULL, mdrt.values = c("minmax", "meansd", "zeromax", "quart", "all"), swap.pred = FALSE, plevel = 0.1, diff = FALSE, title = NULL, axis.title = NULL, axis.labels = NULL, legend.title = NULL, legend.labels = NULL, wrap.title = 50, wrap.legend.labels = 20, wrap.legend.title = 20, geom.colors = "Set1", geom.size = NULL, fill.color = "grey", fill.alpha = 0.3, show.values = FALSE, show.ci = FALSE, jitter.ci = FALSE, p.kr = TRUE, grid.breaks = NULL, xlim = NULL, ylim = NULL, y.offset = 0.07, digits = 2, facet.grid = FALSE, prnt.plot = TRUE, ...)

Arguments

fit
the fitted (generalized) linear (mixed) model object, including interaction terms. Accepted model classes are
  • linear models (lm)
  • generalized linear models (glm)
  • linear mixed effects models (lmer)
  • generalized linear mixed effects models (glmer)
  • non-linear mixed effects models (nlmer)
  • linear mixed effects models (lme, but only for type = "eff")
  • generalized least squares models (gls, but only for type = "eff")
  • panel data estimators (plm)
type
interaction plot type. Use one of following values:
int.term
name of interaction term of fit (as character), which should be plotted when using type = "eff". By default, this argument will be ignored (i.e. int.term = NULL). See 'Details'.
int.plot.index
numeric vector with index numbers that indicate which interaction terms should be plotted in case the fit has more than one interaction. By default, this value is NULL, hence all interactions are plotted.
mdrt.values
indicates which values of the moderator variable should be used when plotting the interaction effects.
swap.pred
if TRUE, the predictor on the x-axis and the moderator value in an interaction are swapped. For type = "eff", the first interaction term is used as moderator and the second term is plotted at the x-axis. For type = "cond", the interaction's predictor with less unique values is printed along the x-axis. Default is FALSE, so the second predictor in an interaction, respectively the predictor with more unique values is printed along the x-axis.
plevel
indicates at which p-value an interaction term is considered as significant, i.e. at which p-level an interaction term will be considered for plotting. Default is 0.1 (10 percent), hence, non-significant interactions are excluded by default. This argument does not apply to type = "eff".
diff
if FALSE (default), the minimum and maximum interaction effects of the moderating variable is shown (one line each). if TRUE, only the difference between minimum and maximum interaction effect is shown (single line). Only applies to type = "cond".
title
default title used for the plots. Should be a character vector of same length as interaction plots to be plotted. Default value is NULL, which means that each plot's title includes the dependent variable as well as the names of the interaction terms.
axis.title
a default title used for the x-axis. Should be a character vector of same length as interaction plots to be plotted. Default value is NULL, which means that each plot's x-axis uses the predictor's name as title.
axis.labels
character vector with value labels of the interaction, used to label the x-axis. Only applies to type = "emm".
legend.title
title of the plot's legend. A character vector of same length as amount of interaction plots to be plotted (i.e. one vector element for each plot's legend title).
legend.labels
labels for the guide/legend. Either a character vector of same length as amount of legend labels of the plot, or a list of character vectors, if more than one interaction plot is plotted (i.e. one vector of legend labels for each interaction plot). Default is NULL, so the name of the predictor with min/max-effect is used as legend label.
wrap.title
numeric, determines how many chars of the plot title are displayed in one line and when a line break is inserted.
wrap.legend.labels
numeric, determines how many chars of the legend labels are displayed in one line and when a line break is inserted.
wrap.legend.title
numeric, determines how many chars of the legend's title are displayed in one line and when a line break is inserted.
geom.colors
vector of color values or name of a valid color brewer palette. If not a color brewer palette name, geom.colors must be of same length as moderator values used in the plot (see mdrt.values). See also 'Details' in sjp.grpfrq.
geom.size
size resp. width of the geoms (bar width, line thickness or point size, depending on plot type and function). Note that bar and bin widths mostly need smaller values than dot sizes.
fill.color
fill color of the shaded area between the minimum and maximum lines. Default is "grey". Either set fill.color to NULL or use 0 for fill.alpha if you want to hide the shaded area.
fill.alpha
alpha value (transparancy) of the shaded area between the minimum and maximum lines. Default is 0.4. Use either 0 or set fill.color to NULL if you want to hide the shaded area.
show.values
logical, whether values should be plotted or not.
show.ci
may be a numeric or logical value. If show.ci is logical and TRUE, a 95% confidence region will be plotted. If show.ci if numeric, must be a number between 0 and 1, indicating the proportion for the confidence regeion (e.g. show.ci = 0.9 plots a 90% CI). Only applies to type = "emm" or type = "eff".
jitter.ci
logical, if TRUE and show.ci = TRUE and confidence bands are displayed as error bars, adds jittering to lines and error bars to avoid overlapping.
p.kr
logical, if TRUE, p-value estimation is based on conditional F-tests with Kenward-Roger approximation for the df. Caution: Computation may take very long time for large samples!
grid.breaks
numeric; sets the distance between breaks for the axis, i.e. at every grid.breaks'th position a major grid is being printed.
xlim
numeric vector of length two, defining lower and upper axis limits of the x scale. By default, this argument is set to NULL, i.e. the x-axis fits to the required range of the data.
ylim
numeric vector of length two, defining lower and upper axis limits of the y scale. By default, this argument is set to NULL, i.e. the y-axis fits to the required range of the data.
y.offset
numeric, offset for text labels when their alignment is adjusted to the top/bottom of the geom (see hjust and vjust).
digits
numeric, amount of digits after decimal point when rounding estimates and values.
facet.grid
TRUE to arrange the lay out of of multiple plots in a grid of an integrated single plot. This argument calls facet_wrap or facet_grid to arrange plots. Use plot_grid to plot multiple plot-objects as an arranged grid with grid.arrange.
prnt.plot
logical, if TRUE (default), plots the results as graph. Use FALSE if you don't want to plot any graphs. In either case, the ggplot-object will be returned as value.
...
other arguments, passed down to the effect resp. allEffects function when type = "eff".

Value

(Insisibily) returns the ggplot-objects with the complete plot-list (plot.list) as well as the data frames that were used for setting up the ggplot-objects (data.list).

Details

The argument int.term only applies to type = "eff" and can be used to select a specific interaction term of the model that should be plotted. The function then calls effect(int.term, fit) to compute effects for this specific interaction term only. This approach is recommended, when the fitted model contains many observations and/or variables, which may slow down the effect-computation dramatically. In such cases, consider computing effects for selected interaction terms only with int.terms. See 'Examples'.

References

  • Aiken and West (1991). Multiple Regression: Testing and Interpreting Interactions.
  • Brambor T, Clark WR and Golder M (2006) Understanding Interaction Models: Improving Empirical Analyses. Political Analysis 14: 63-82. \Sexpr[results=rd,stage=build]{tools:::Rd_expr_doi("#1")}10.1093/pan/mpi014http://doi.org/10.1093/pan/mpi014doi:\ifelse{latex}{\out{~}}{ }latex~ 10.1093/pan/mpi014

  • Esarey J, Sumner JL (2015) Marginal Effects in Interaction Models: Determining and Controlling the False Positive Rate. download
  • Fox J (2003) Effect displays in R for generalised linear models. Journal of Statistical Software 8:15, 1–27,
  • Hayes AF (2012) PROCESS: A versatile computational tool for observed variable mediation, moderation, and conditional process modeling [White paper] download
  • Grace-Martin K: Interpreting Interactions in Regression
  • Grace-Martin K: Clarifications on Interpreting Interactions in Regression

See Also

sjPlot manual: sjp.int

Examples

Run this code
# Note that the data sets used in this example may not be perfectly suitable for
# fitting linear models. I just used them because they are part of the R-software.

# fit "dummy" model. Note that moderator should enter
# first the model, followed by predictor. Else, use
# argument "swap.pred" to change predictor on 
# x-axis with moderator
fit <- lm(weight ~ Diet * Time, data = ChickWeight)

# show summary to see significant interactions
summary(fit)

# plot regression line of interaction terms, including value labels
sjp.int(fit, type = "eff", show.values = TRUE)


# load sample data set
library(sjmisc)
data(efc)
# create data frame with variables that should be included
# in the model
mydf <- data.frame(usage = efc$tot_sc_e,
                   sex = efc$c161sex,
                   education = efc$c172code,
                   burden = efc$neg_c_7,
                   dependency = efc$e42dep)
# convert gender predictor to factor
mydf$sex <- relevel(factor(mydf$sex), ref = "2")
# fit "dummy" model
fit <- lm(usage ~ .*., data = mydf)
summary(fit)

# plot interactions. note that type = "cond" only considers 
# significant interactions by default. use "plevel" to 
# adjust p-level sensivity
sjp.int(fit, type = "cond")

# plot only selected interaction term for
# type = "eff"
sjp.int(fit, type = "eff", int.term = "sex*education")

# plot interactions, using mean and sd as moderator
# values to calculate interaction effect
sjp.int(fit, type = "eff", mdrt.values = "meansd")
sjp.int(fit, type = "cond", mdrt.values = "meansd")

# plot interactions, including those with p-value up to 0.1
sjp.int(fit, type = "cond", plevel = 0.1)

# -------------------------------
# Predictors for negative impact of care.
# Data from the EUROFAMCARE sample dataset
# -------------------------------
library(sjmisc)
data(efc)
# create binary response
y <- ifelse(efc$neg_c_7 < median(stats::na.omit(efc$neg_c_7)), 0, 1)
# create data frame for fitted model
mydf <- data.frame(y = as.factor(y),
                   sex = as.factor(efc$c161sex),
                   barthel = as.numeric(efc$barthtot))
# fit model
fit <- glm(y ~ sex * barthel, data = mydf, family = binomial(link = "logit"))
# plot interaction, increase p-level sensivity
sjp.int(fit, type = "eff", legend.labels = get_labels(efc$c161sex), plevel = 0.1)
sjp.int(fit, type = "cond", legend.labels = get_labels(efc$c161sex), plevel = 0.1)
        
## Not run: 
# # -------------------------------
# # Plot estimated marginal means
# # -------------------------------
# # load sample data set
# library(sjmisc)
# data(efc)
# # create data frame with variables that should be included
# # in the model
# mydf <- data.frame(burden = efc$neg_c_7,
#                    sex = efc$c161sex,
#                    education = efc$c172code)
# # convert gender predictor to factor
# mydf$sex <- factor(mydf$sex)
# mydf$education <- factor(mydf$education)
# # name factor levels and dependent variable
# levels(mydf$sex) <- c("female", "male")
# levels(mydf$education) <- c("low", "mid", "high")
# mydf$burden <- set_label(mydf$burden, "care burden")
# # fit "dummy" model
# fit <- lm(burden ~ .*., data = mydf)
# summary(fit)
# 
# # plot marginal means of interactions, no interaction found
# sjp.int(fit, type = "emm")
# # plot marginal means of interactions, including those with p-value up to 1
# sjp.int(fit, type = "emm", plevel = 1)
# # swap predictors
# sjp.int(fit, type = "emm", plevel = 1, swap.pred = TRUE)
# 
# # -------------------------------
# # Plot effects
# # -------------------------------
# # add continuous variable
# mydf$barthel <- efc$barthtot
# # re-fit model with continuous variable
# fit <- lm(burden ~ .*., data = mydf)
# 
# # plot effects
# sjp.int(fit, type = "eff", show.ci = TRUE)
# 
# # plot effects, faceted
# sjp.int(fit, type = "eff", int.plot.index = 3, show.ci = TRUE, facet.grid = TRUE)## End(Not run)

Run the code above in your browser using DataLab