rtip
viz
Author

Steven P. Sanderson II, MPH

Published

October 5, 2023

Introduction

As an R programmer, you may want to create added variable plots to visualize the relationship between a predictor variable and the response variable while controlling for the effects of other predictor variables. In this blog post, we will use the car library and the avPlots() function to create added variable plots in R.

What are Added Variable Plots?

Added variable plots, also known as partial-regression plots, are used to visualize the relationship between a predictor variable and the response variable while controlling for the effects of other predictor variables. They are useful for identifying non-linear relationships and outliers, and for checking the linearity assumption of a linear regression model.

How to Create Added Variable Plots in R

To create added variable plots in R, we will use the avPlots() function from the car library. Here is an example code block:

# Load the car library
library(car)
Loading required package: carData
# Fit a linear regression model
model <- lm(mpg ~ disp + hp + drat + am, data = mtcars)

# Create added variable plots
avPlots(model)

Let’s break down this code block:

  • First, we load the car library using the library() function.
  • Next, we fit a linear regression model using the lm() function. The model formula specifies mpg as the response variable and disp, hp, am and drat as the predictor variables. The data for the model is mtcars.
  • Finally, we create added variable plots using the avPlots() function and passing in the model object.

Interpretation of Added Variable Plots

The added variable plots show the relationship between each predictor variable and the response variable while controlling for the effects of the other predictor variables. The x-axis represents the partial residuals of the predictor variable, and the y-axis represents the partial residuals of the response variable. The line in the plot represents the fitted values from a linear regression model of the partial residuals of the response variable on the partial residuals of the predictor variable.

If the relationship between the predictor variable and the response variable is linear, the line in the plot should be approximately horizontal. If the relationship is non-linear, the line may be curved. If there is an outlier, it may be visible as a point that is far away from the other points in the plot.

Conclusion

In this blog post, we have learned how to create added variable plots in R using the car library and the avPlots() function. We have also discussed the interpretation of added variable plots and their usefulness in identifying non-linear relationships and outliers. I encourage you to try creating added variable plots on your own and explore the relationships between predictor variables and response variables in your own datasets.

Resources

  • [1] https://rdrr.io/cran/car/man/avPlots.html
  • [2] https://search.r-project.org/CRAN/refmans/car/html/avPlots.html
  • [3] https://www.geeksforgeeks.org/how-to-create-added-variable-plots-in-r/
  • [4] https://stackoverflow.com/questions/59150905/is-there-a-ggplot2-analogue-to-the-avplots-function-in-r
  • [6] https://www.rdocumentation.org/link/avPlots?package=car&version=2.0-0