Steve's Data Tips and Tricks 2023-10-04 22:00:00

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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
To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)