jtools (version 0.4.5)

interact_plot: Plot interaction effects in regression models

Description

interact_plot() plots regression lines at user-specified levels of a moderator variable to explore interactions. The plotting is done with ggplot2 rather than base graphics, which some similar functions use.

Usage

interact_plot(model, pred, modx, modxvals = NULL, mod2 = NULL,
  mod2vals = NULL, centered = NULL, standardize = FALSE, n.sd = 1,
  plot.points = FALSE, interval = FALSE, int.type = c("confidence",
  "prediction"), int.width = 0.95, outcome.scale = "response",
  x.label = NULL, y.label = NULL, modx.labels = NULL,
  mod2.labels = NULL, main.title = NULL, legend.main = NULL,
  color.class = NULL, line.thickness = 1.1, vary.lty = TRUE)

Arguments

model

A regression model of type lm, glm, svyglm, or merMod. It should contain the interaction of interest.

pred

The name of the predictor variable involved in the interaction.

modx

The name of the moderator variable involved in the interaction.

modxvals

For which values of the moderator should lines be plotted? Default is NULL. If NULL, then the customary +/- 1 standard deviation from the mean as well as the mean itself are used for continuous moderators. If the moderator is a factor variable and modxvals is NULL, each level of the factor is included. If "plus-minus", plots lines when the moderator is at +/- 1 standard deviation without the mean.

mod2

Optional. The name of the second moderator variable involved in the interaction.

mod2vals

For which values of the second moderator should the plot be facetted by? That is, there will be a separate plot for each level of this moderator. Defaults are the same as modxvals.

centered

A vector of quoted variable names that are to be mean-centered. If NULL, all non-focal predictors are centered. If not NULL, only the user-specified predictors are centered. User can also use "none" or "all" arguments. The response variable is not centered unless specified directly.

standardize

Logical. Would you like to standardize the variables that are centered? Default is FALSE, but if TRUE it will standardize variables specified by the centered argument. Note that non-focal predictors are centered when centered = NULL, its default.

n.sd

How many standard deviations should be used if standardize = TRUE? Default is 1, but some prefer 2.

plot.points

Logical. If TRUE, plots the actual data points as a scatterplot on top of the interaction lines. If moderator is a factor, the dots will be the same color as their parent factor.

interval

Logical. If TRUE, plots confidence/prediction intervals the line using geom_ribbon. Not supported for merMod models.

int.type

Type of interval to plot. Options are "confidence" or "prediction". Default is confidence interval.

int.width

How large should the interval be, relative to the standard error? The default, .95, corresponds to roughly 1.96 standard errors and a .05 alpha level for values outside the range. In other words, for a confidence interval, .95 is analogous to a 95% confidence interval.

outcome.scale

For nonlinear models (i.e., GLMs), should the outcome variable be plotted on the link scale (e.g., log odds for logit models) or the original scale (e.g., predicted probabilities for logit models)? The default is "response", which is the original scale. For the link scale, which will show straight lines rather than curves, use "link".

x.label

A character object specifying the desired x-axis label. If NULL, the variable name is used.

y.label

A character object specifying the desired x-axis label. If NULL, the variable name is used.

modx.labels

A character vector of labels for each level of the moderator values, provided in the same order as the modxvals argument. If NULL, the values themselves are used as labels unless modxvals is also NULL. In that case, "+1 SD" and "-1 SD" are used.

mod2.labels

A character vector of labels for each level of the 2nd moderator values, provided in the same order as the mod2vals argument. If NULL, the values themselves are used as labels unless mod2vals is also NULL. In that case, "+1 SD" and "-1 SD" are used.

main.title

A character object that will be used as an overall title for the plot. If NULL, no main title is used.

legend.main

A character object that will be used as the title that appears above the legend. If NULL, the name of the moderating variable is used.

color.class

Any palette argument accepted by scale_colour_brewer. Default is "Set2" for factor moderators, "Blues" for +/- SD and user-specified modxvals values.

line.thickness

How thick should the plotted lines be? Default is 1.1; ggplot's default is 1.

vary.lty

Should the resulting plot have different shapes for each line in addition to colors? Defaults to TRUE.

Value

The functions returns a ggplot object, which can be treated like a user-created plot and expanded upon as such.

Details

This function provides a means for plotting conditional effects for the purpose of exploring interactions in the context of regression. You must have the package ggplot2 installed to benefit from these plotting functions.

The function is designed for two-way interactions. For additional terms, the effects package may be better suited to the task.

This function supports nonlinear models and by default will plot them on their original scale (outcome.scale = "response").

While mixed effects models from lme4 are supported, only the fixed effects are plotted.

References

Bauer, D. J., & Curran, P. J. (2005). Probing interactions in fixed and multilevel regression: Inferential and graphical techniques. Multivariate Behavioral Research, 40(3), 373-400. http://dx.doi.org/10.1207/s15327906mbr4003_5

Cohen, J., Cohen, P., West, S. G., & Aiken, L. S. (2003). Applied multiple regression/correlation analyses for the behavioral sciences (3rd ed.). Mahwah, NJ: Lawrence Erlbaum Associates, Inc.

See Also

plotSlopes performs a similar function, but but with the base graphics package--this function is meant, in part, to simulate its features.

sim_slopes performs a simple slopes analysis with a similar argument syntax to this function.

Other interaction tools: johnson_neyman, probe_interaction, sim_slopes

Examples

Run this code
# NOT RUN {
# Using a fitted lm model
states <- as.data.frame(state.x77)
states$HSGrad <- states$`HS Grad`
fit <- lm(Income ~ HSGrad + Murder*Illiteracy,
  data = states)
interact_plot(model = fit, pred = Murder,
  modx = Illiteracy)

# Using interval feature
fit <- lm(accel ~ mag*dist, data=attenu)
interact_plot(fit, pred = mag, modx = dist, interval = TRUE,
  int.type = "confidence", int.width = .8)

# Using second moderator
fit <- lm(Income ~ HSGrad*Murder*Illiteracy,
  data = states)
interact_plot(model = fit, pred = Murder,
  modx = Illiteracy, mod2 = HSGrad)

# With svyglm
library(survey)
data(api)
dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
regmodel <- svyglm(api00~ell*meals,design=dstrat)
interact_plot(regmodel, pred = ell, modx = meals)

# With lme4
# }
# NOT RUN {
library(lme4)
data(VerbAgg)
mv <- glmer(r2 ~ Anger * mode + (1 | item), data = VerbAgg, family = binomial,
            control = glmerControl("bobyqa"))
interact_plot(mv, pred = Anger, modx = mode)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab