Table of Contents
Creating partial residual plots in R allows for a visual representation of the relationship between a predictor variable and the response variable in a linear regression model. To create these plots, the first step is to fit the linear regression model using the lm() function. Then, the residuals are calculated for each observation and plotted against the predictor variable of interest, while holding all other predictor variables constant. This allows for the identification of any non-linear relationships or outliers in the data. By following a few simple steps, partial residual plots in R can provide valuable insights into the relationships between variables in a linear regression model.
Create Partial Residual Plots in R
is a statistical method we can use to understand the relationship between multiple predictor variables and a .
However, one of the key of multiple linear regression is that there exists a linear relationship between each predictor variable and the response variable.
If this assumption is violated, then the results of the regression model can be unreliable.
One way to check this assumption is to create a partial residual plot, which displays the of one predictor variable against the response variable.
The following example shows how to create partial residual plots for a regression model in R.
Example: How to Create Partial Residual Plots in R
Suppose we fit a regression model with three predictor variables in R:
#make this example reproducible set.seed(0) #define response variable y <- c(1:1000) #define three predictor variables x1 <- c(1:1000)*runif(n=1000) x2 <- (c(1:1000)*rnorm(n=1000))^2 x3 <- (c(1:1000)*rnorm(n=1000))^3 #fit multiple linear regression model model <- lm(y~x1+x2+x3))
We can use the crPlots() function from the car package in R to create partial residual plots for each predictor variable in the model:
library(car) #create partial residual plots crPlots(model)

The blue line shows the expected residuals if the relationship between the predictor and response variable was linear. The pink line shows the actual residuals.
If the two lines are significantly different, then this is evidence of a nonlinear relationship.
From the plots above we can see that the residuals for both x2 and x3 appear to be nonlinear.
This violates the assumption of linearity for multiple linear regression. One way to fix this issue is to use a square root or cubic transformation on the predictor variables:
library(car) #fit new model with transformed predictor variables model_transformed <- lm(y~x1+sqrt(x2)+log10(x3^(1/3))) #create partial residual plots for new model crPlots(model_transformed)

The predictor variable x3 is still somewhat nonlinear so we may decide to try another transformation or possibly drop the variable from the model altogether.
Additional Resources
The following tutorials explain how to create other common plots in R:
Cite this article
stats writer (2024). How to create partial residual plots in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-create-partial-residual-plots-in-r-2/
stats writer. "How to create partial residual plots in R?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-to-create-partial-residual-plots-in-r-2/.
stats writer. "How to create partial residual plots in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-create-partial-residual-plots-in-r-2/.
stats writer (2024) 'How to create partial residual plots in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-create-partial-residual-plots-in-r-2/.
[1] stats writer, "How to create partial residual plots in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How to create partial residual plots in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
