The interplot package is a tool for plotting the conditional coefficients (“marginal effects”) of variables included in multiplicative interaction terms. The function plots the changes in the coefficient of one variable in a two-way interaction term conditional on the value of the other included variable. The plot also includes simulated 95% confidential intervals of these coefficients. In the current version, the function works with ordinary linear regression models, generalized linear models (e.g., logit, probit, ordered logit, etc.), and multilevel (mixed-effects) regressions, all with complete or multiply imputed data.

Comparing to established alternatives such as effects::plot and sjplot::sjp.int, interplot provides a more user-friendly way to quickly produce plots that are easy to interpret. interplot plots the changes in the conditional coefficient of one variable in the interaction, rather than changes in the dependent variable itself as in the aforementioned functions. This approach avoids displaying interaction effects across multiple panels or multiple lines in favor of a single plot containing all the relevant information. Moreover, by outputting ggplot objects, interplot allows users to easily further customize their graphs.

This vignette purposes to illustrate how users can apply these functions to improve the presentation of the interactions in their models.

Run a Model

This example is based on the mtcars dataset, which is drawn from the 1974 volume of the US magazine Motor Trend. The dependent variable is mileage in miles per (US) gallon (mpg), and the independent variables are the number of engine cylinders (cyl) and automobile weight in thousands of pounds (wt).

data(mtcars)  #load the data

Suppose we are interested in how automobile weight affects the relationship between of the number of engine cylinders on mileage and how the number of cylinders affects the relationship between the car’s weight and its mileage. Such conditional effects are modeled using a two-way multiplicative interaction term:

m_cyl <- lm(mpg ~ wt * cyl, data = mtcars)
summary(m_cyl)
## 
## Call:
## lm(formula = mpg ~ wt * cyl, data = mtcars)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.2288 -1.3495 -0.5042  1.4647  5.2344 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  54.3068     6.1275   8.863 1.29e-09 ***
## wt           -8.6556     2.3201  -3.731 0.000861 ***
## cyl          -3.8032     1.0050  -3.784 0.000747 ***
## wt:cyl        0.8084     0.3273   2.470 0.019882 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.368 on 28 degrees of freedom
## Multiple R-squared:  0.8606, Adjusted R-squared:  0.8457 
## F-statistic: 57.62 on 3 and 28 DF,  p-value: 4.231e-12

The coefficient of the interactive term (wt:cyl) is positive and statistically significant at .05 level. This tells us that the coefficient of wt depends on the value of cyl and vice versa; these estimated coefficients are conditional. It does not indicate anything, however, about the magnitude or statistical significance of these conditional coefficients. A plot produced by interplot easily and clearly answers these latter questions.

Create a Basic Graph of the Conditional Coefficients

To plot conditional coefficients, a user needs to provide only three basic pieces of information: the object of a regression result (m), the variable whose coefficient is to be plotted (var1), and the variable on which the coefficient is conditional (var2). Taking the previous example, if we intend to know how the weight of a car can affect the coefficient for number of cylinders on the mileage, var1 is cyl, and var2 is wt.

library(interplot)
## Loading required package: ggplot2
## Loading required package: abind
## Loading required package: arm
## Loading required package: MASS
## Loading required package: Matrix
## Loading required package: lme4
## 
## arm (Version 1.8-5, built: 2015-05-13)
## 
## Working directory is E:/Dropbox_sync/Dropbox/Instruction/Research paper/Interplot/vignettes
interplot(m = m_cyl, var1 = "cyl", var2 = "wt")

The plot clearly shows that with increasing automobile weight (along in the x axis), the magnitude of the coefficient of the number of cylinders on the mileage also increases (along in the y axis).

Similarly, to show how the number of cylinders affects the coefficient of automobile weight on mileage, one only needs to switch var1 and var2:

interplot(m = m_cyl, var1 = "wt", var2 = "cyl")

The format of the plot also changes when wt is var1 and cyl is var2. This is because cyl is not a continuous variable but a categorical one with just three values: 4, 6, and 8. interplot automatically detects the number of values taken on by var2 and chooses the appropriate plot format. If there are fewer than 10 values, the function will produce a “dot-and-whisker” plot; otherwise, by default, it will generate a “line-and-ribbon” plot. Users may override this default by setting the argument point to TRUE.

interplot(m = m_cyl, var1 = "cyl", var2 = "wt", point = T) +
  # changing the angle of x labels for a clearer vision
  theme(axis.text.x  = element_text(angle=90))

Change the Appearance of the Plot

Plots generated by interplot are, by design, very basic. They are, however, ggplot objects and so may be easily modified further.

interplot(m = m_cyl, var1 = "cyl", var2 = "wt") + 
  # Add labels for X and Y axes
    xlab("Automobile Weight (thousands lbs)") +
    ylab("Estimated Coefficient for\nNumber of Cylinders") +
  # Change the background
    theme_bw() +
  # Add the title
    ggtitle("Estimated Coefficient of Engine Cylinders \non Mileage by Automobile Weight") +
    theme(plot.title = element_text(face="bold")) +
  # Add a horizontal line at y = 0
    geom_hline(yintercept = 0, linetype = "dashed")

Advanced Use: Plot Conditional Effects Without a Model

In some cases, one may analyze with some complicated or self-made regression functions which are not supported by the current version of interplot. For them, as long as the user has a dataset loading the simulated results of the interaction effects, she can still use interplot to visualize it. The dataset needs four columns the scale of the conditioning variable (fake), the simulated interactive effect at each break of the conditioning variable (coef1), and the simulated lower bound and upper bound of the confidence interval (lb, and ub). The column names should be exactly the ones shown in the above parentheses. Here is an example with some arbitrary artificial data:

# Create a fake dataset of conditional effects
fake <- rnorm(100, 0, 1)
coef1 <- fake * sample(.5:2.5, 100, replace = T)
lb <- coef1 - .5
ub <- coef1 + .5

df_fake <- data.frame(cbind(fake, coef1, lb, ub))

# Use interplot directly with the dataset
interplot(df_fake)

Conclusion

The interplot package provides a flexible and convenient way to visualize conditional coefficients of variables in multiplicative interaction terms. This vignette offers an overview of its use and features. We encourage users to consult the help files for more details.

The development of the package is ongoing, and future research promises a compatible tool for more types of regressions and more functions. Please contact us with any questions, bug reports, and comments.

Affiliation

Frederick Solt

Department of Political Science,

University of Iowa,

324 Schaeffer Hall,

20 E Washington St, Iowa City, IA, 52242

Email: frederick-solt@uiowa.edu

Website: http://myweb.uiowa.edu/fsolt


Yue Hu

Department of Political Science,

University of Iowa,

313 Schaeffer Hall,

20 E Washington St, Iowa City, IA, 52242

Email: yue-hu-1@uiowa.edu

Website: http://clas.uiowa.edu/polisci/people/yue-hu