Visualizing OLS Linear Regression Assumptions in R

[This article was first published on R in Trevor French on Medium, 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.

While most of the time it’s sufficient to programmatically validate your model assumptions, sometimes it’s helpful to visualize them. Here are a few quick ways you can do just that.

Linearity

Visualizing Linearity

Linearity is likely the easiest assumption to visualize as you can simply use the following code snippet to quickly create a scatterplot.

Visualizing linearity scatterplot

Additionally, you can alter the appearance of your points by using the “pch”, “cex”, and “col” options. PCH stands for Plot Character and will adjust the symbol used for your points. The available point shapes are listed in the image below.

Generated with ggpubr::show_point_shapes()

The “cex” option allows you to adjust the symbol size. The default value is 1. If you were to change the value to .75, for example, the plot symbol would be scaled down the 3/4 of the default size. The “col” option allows you to adjust the color of your plot symbols.

Scatterplot with formatted points

You can adjust the axes with the “xlab”, “ylab”, “xaxt”, and “yaxt” options (amongst other available options). In the following example we will remove the axes altogether.

Visualizing linearity without axes

Finally, you can add a trend line by creating a model and adding the fitted values to the graph. We’ll also adjust the line width and color with the “lwd” and “col” parameters, respectively.

Linear trendline

Alternatively, you can enrich your data with limits by using the predict function as shown below.

Visualize upper and lower limits

Multicollinearity

Visualizing multicollinearity

The first way you can visualize multicollinearity is through a plot matrix via the “pairs” function. You can test this out on the “mtcars” dataset as follows:

Plot matrix

The second way you can visualize this is through a correlation plot. First, install the “corrplot” library then use the “corrplot” function.

Autocorrelation

Visualizing Autocorrelation

To visualize autocorrelation, you can create an autocorrelation plot via the acf function in the stats library.

Here’s an example of a plot with data that does contain autocorrelation:


Visualizing OLS Linear Regression Assumptions in R was originally published in Trevor French on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: R in Trevor French on Medium.

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)