How to Create a Residual Plot in R
Last Updated :
02 Jun, 2022
In this article, we will be looking at a step-wise procedure to create a residual plot in the R programming language.
Residual plots are often used to assess whether or not the residuals in regression analysis are normally distributed and whether or not they exhibit heteroscedasticity.
Let's create a residual plot in R programming language.
Step 1: Fit regression model
Under this step, we will fit a regression model using the iris data set which is the in-built dataset from the rstudio as the response variable and Sepal.Length and Sepal.Width as explanatory variables using the lm()) function and then further calling the resid() function passed this regression model passed to get the list of residuals of the model.
R
# load the dataset
data("iris")
# select Target attribute and
# Predictor attribute
Y<- iris[,"Sepal.Width"]
X<- iris[,"Sepal.Length"]
# fit a regression model
model <- lm(Y~X)
# get list of residuals
res <- resid(model)
res
Output:
Step 2: Produce residual vs. fitted plot
In this step, we are plotting a scatter plot of the residual of the modal vs filtered model to visually detect heteroscedasticity – e.g. a systematic change in the spread of residuals over a range of values.
R
# produce residual vs. fitted plot
plot(fitted(model), res)
# add a horizontal line at 0
abline(0,0)
Output:
Step 3: Produce a Q-Q plot
Here, we are plotting a Q-Q plot using the qqnorm() function, for determining if the residuals follow a normal distribution. If the data values in the plot fall along a roughly straight line at a 45-degree angle using the qqline() function passed with the required parameters, then the data is normally distributed. And in the output, the residuals tend to stray from the line quite a bit near the tails, which indicates that they’re not normally distributed.
R
# create Q-Q plot for residuals
qqnorm(res)
# add a straight diagonal line
# to the plot
qqline(res)
Output:
Step 4: Produce a density plot
In this step, we plot density plots to visually check whether or not the residuals are normally distributed. If the plot is roughly bell-shaped, then the residuals likely follow a normal distribution and as compared with the output the density plot roughly follows a bell shape, which ensures that the residuals are more normally distributed.
R
Output:
Similar Reads
How to create a reusable plot_ly function in R In data visualization, reusability and consistency are crucial for maintaining clarity and efficiency. Plotly is the powerful library in the R for creating interactive plots. By encapsulating the plotting logic into the reusable functions. We can streamline the plotting process and it can ensure uni
5 min read
How to Create a Forest Plot in R? In this article, we will discuss how to create a Forest Plot in the R programming language. A forest plot is also known as a blobbogram. It helps us to visualize estimated results from a certain number of studies together along with the overall results in a single plot. It is extensively used in med
4 min read
How to Create a Log-Log Plot in R? In this article, we will discuss how to create a Log-Log plot in the R Programming Language. A log-log plot is a plot that uses logarithmic scales on both the axes i.e., the x-axis and the y-axis.We can create a Log-Log plot in the R language by following methods. Log-Log Plot in Base R: To create a
2 min read
How to Create Added Variable Plots in R? In this article, we will discuss how to create an added variable plot in the R Programming Language. The Added variable plot is an individual plot that displays the relationship between a response variable and one predictor variable in a multiple linear regression model while controlling for the pre
3 min read
How to Create and Visualise Volcano Plot in R In R, a volcano plot is commonly used in bioinformatics and genomics to visualize differential expression analysis results. It displays fold change on the x-axis and statistical significance on the y-axis, typically represented as -log10(p-value). Volcano plots provide a concise way to identify sign
6 min read
How to plot excel data in R? Plotting graph in R using an excel file, we need an excel file with two-column in it, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be discussing the approach
2 min read