Learn R Programming

interplot (version 0.1.1.0)

interplot: Plot Conditional Coefficients of a Variable in an Interaction Term

Description

interplot is a generic function to produce a plot of the coefficient estimates of one variable in a two-way interaction conditional on the values of the other variable in the interaction term. The function invokes particular methods which depend on the class of the first argument.

Usage

interplot(m, var1, var2, plot = TRUE, point = FALSE, sims = 5000,
  xmin = NA, xmax = NA)

Arguments

m
A model object including an interaction term, or, alternately, a data frame generated by an earlier call to interplot using the argument plot = FALSE.
var1
The name (as a string) of the variable of interest in the interaction term; its conditional coefficient estimates will be plotted.
var2
The name (as a string) of the other variable in the interaction term
plot
A logical value indicating whether the output is a plot or a dataframe including the conditional coefficient estimates of var1, their upper and lower bounds, and the corresponding values of var2.
point
A logical value determining the format of plot. By default, the function produces a line plot when var2 takes on ten or more distinct values and a point (dot-and-whisker) plot otherwise; option TRUE forces a point plot.
sims
Number of independent simulation draws used to calculate upper and lower bounds of coefficient estimates: lower values run faster; higher values produce smoother curves.
xmin
A numerical value indicating the minimum value shown of x shown in the graph. Rarely used.
xmax
A numerical value indicating the maximum value shown of x shown in the graph. Rarely used.

Value

  • The function returns a ggplot object.

Details

interplot visualizes the changes in the coefficient of one term in a two-way interaction conditioned by the other term. In the current version, the function works with interactions in the following classes of models:
  • Ordinary linear models (object class:lm);
  • Generalized linear models (object class:glm);
  • Linear mixed-effects models (object class:lmerMod);
  • Generalized linear mixed-effects models (object class:glmerMod);
  • Ordinary linear models with imputed data (object class:list);
  • Generalized linear models with imputed data (object class:list)
  • Linear mixed-effects models with imputed data (object class:list);
  • Generalized linear mixed-effects models with imputed data (object class:list).
The examples below illustrate how methods invoked by this generic deal with different type of objects. Because the output function is based on ggplot, any additional arguments and layers supported by ggplot2 can be added with the +.

Examples

Run this code
data(mtcars)
m_cyl <- lm(mpg ~ wt * cyl, data = mtcars)
library(interplot)

# Plot interactions with a continous conditioning variable
interplot(m = m_cyl, var1 = 'cyl', var2 = 'wt') +
xlab('Automobile Weight (thousands lbs)') +
ylab('Estimated Coefficient for Number of Cylinders') +
ggtitle('Estimated Coefficient of Engine Cylinders\non Mileage by Automobile Weight') +
theme(plot.title = element_text(face='bold'))


# Plot interactions with a categorical conditioning variable
interplot(m = m_cyl, var1 = 'wt', var2 = 'cyl') +
xlab('Number of Cylinders') +
ylab('Estimated Coefficient for Automobile Weight (thousands lbs)') +
ggtitle('Estimated Coefficient of Automobile Weight \non Mileage by Engine Cylinders') +
theme(plot.title = element_text(face='bold'))

Run the code above in your browser using DataLab