Visualizing OLS Linear Regression Assumptions in R
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
Linearity is likely the easiest assumption to visualize as you can simply use the following code snippet to quickly create a 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.
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.
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.
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.
Alternatively, you can enrich your data with limits by using the predict function as shown below.
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:
The second way you can visualize this is through a correlation plot. First, install the “corrplot” library then use the “corrplot” function.
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.
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.