molecalculator

Moles & Molar Mass

How to Check Normality of Residuals in R

Guide 01-10758 · Updated · Step by step

This guide walks you through the essential methods to check whether the residuals from a linear regression model in R are normally distributed. You’ll learn how to use visual plots (histogram, Q-Q plot) and formal statistical tests (Shapiro-Wilk, Kolmogorov-Smirnov, Anderson-Darling), and how to interpret the results. Before starting, ensure you have R installed and a fitted regression model object (e.g., from lm()).

What You’ll Need

  • R (version 3.6 or later) and RStudio (optional but recommended).
  • A fitted linear regression model, created with lm(). For example: model <- lm(y ~ x1 + x2, data = mydata).
  • The car package for the Anderson-Darling test and some enhanced plots. Install with install.packages("car").
  • Base R functions (no extra packages) for the histogram, Q-Q plot, and Shapiro-Wilk test.

Step-by-Step

  1. Extract the residuals from your model. Use resid(model) or model$residuals. Store them in a variable: res <- resid(model).
  2. Create a histogram of the residuals. Use hist(res, main="Histogram of Residuals", xlab="Residuals", col="lightblue"). Look for a bell-shaped curve. If it looks roughly symmetric and bell-shaped, normality is plausible.
  3. Create a Q-Q plot (quantile-quantile plot). Use qqnorm(res) followed by qqline(res). The points should fall approximately along the straight line. Systematic deviations (e.g., S-curve, heavy tails) indicate non-normality.
  4. Run the Shapiro-Wilk test for a formal check. Use shapiro.test(res). The null hypothesis is that the residuals are normally distributed. If the p-value is less than your significance level (usually 0.05), you reject normality.
  5. Run the Kolmogorov-Smirnov test as an alternative. Use ks.test(res, "pnorm", mean(res), sd(res)). Note that this test is less powerful for small samples, and you must specify the mean and standard deviation of your residuals.
  6. Run the Anderson-Darling test (from the car package) for a more robust check. Use car::qqPlot(res) to get a Q-Q plot with confidence bands, and car::ad.test(res) for the test. The qqPlot also gives a visual check with pointwise confidence envelopes.
  7. Interpret the results. Combine visual and statistical evidence. If the histogram is bell-shaped, the Q-Q plot points are near the line, and the p-values of the tests are above 0.05, you can assume normality. If any of these are violated, consider transformations (e.g., log, square root) or robust regression methods.

Common Mistakes

  • Using the Shapiro-Wilk test on a very large sample (e.g., n > 5000). The test becomes overly sensitive to minor deviations that are practically irrelevant. In such cases, rely more on visual plots.
  • Forgetting that the Kolmogorov-Smirnov test requires specifying the parameters (mean and sd). Using the default ks.test(res, "pnorm") without parameters is invalid because it tests against a standard normal, not your residual distribution.
  • Checking normality of the raw data instead of the residuals. The assumption is about the errors, not the response variable.
  • Ignoring the shape of the Q-Q plot and only looking at the p-value. A p-value > 0.05 does not guarantee normality; the plot may still show clear outliers or skewness.
  • Using a histogram with too few bins, which can hide the distribution shape. Defaults are often fine, but you can adjust with breaks=.

Tips

  • Always start with visual checks before running formal tests. Plots give you a sense of the magnitude and type of deviation (e.g., skewness, heavy tails).
  • For small samples (n < 30), the Shapiro-Wilk test is recommended. For larger samples, the Anderson-Darling test is often preferred.
  • If you have multiple models, compare their residual plots side by side using par(mfrow=c(1,2)).
  • Consider using the performance package's check_normality() function for a quick automated summary, but always verify with plots.
  • Remember that normality of residuals is not required for the coefficients to be unbiased; it is mainly needed for valid confidence intervals and p-values in small samples. With large samples, the Central Limit Theorem often makes normality less critical.

Troubleshooting

  • If the Shapiro-Wilk test returns an error about the sample size: The test requires a sample size between 3 and 5000. If you have more than 5000 residuals, use a random subset or rely on the Anderson-Darling test.
  • If the Q-Q plot points are not on the line: This indicates non-normality. Try transforming the response variable (e.g., log(y)) or the predictors. Alternatively, consider using robust standard errors (e.g., sandwich package) or a different model family (e.g., generalized linear models).
  • If the histogram looks skewed: Check for outliers or influential points. You can use plot(model) to see residual vs. fitted plots and identify outliers. Removing or down-weighting outliers may help.
  • If the Anderson-Darling test gives a p-value of exactly 0: This often happens with large samples. The test is significant, but the deviation may be small. Look at the Q-Q plot to see if the deviation is practically meaningful.
  • If you get an error that the car package is not installed: Run install.packages("car") and then load it with library(car).

Frequently Asked Questions

Why do I need to check normality of residuals?

Normality of residuals is an assumption of linear regression for making valid inferences (p-values, confidence intervals) and for certain prediction intervals. If the residuals are not normal, your p-values may be unreliable, especially with small sample sizes.

What if my residuals are not normally distributed?

You can try transforming the response variable (e.g., log, square root, Box-Cox), adding interaction terms, or using a different model like generalized linear models (GLM) or robust regression. Sometimes, with large samples, the Central Limit Theorem ensures that the coefficient estimates are approximately normal even if residuals are not.

Is the Shapiro-Wilk test better than the Kolmogorov-Smirnov test?

For normality testing, the Shapiro-Wilk test is generally more powerful for small to moderate sample sizes (n < 2000). The Kolmogorov-Smirnov test is less powerful because it is not specifically designed for normality; it requires estimating parameters from the data, which reduces its power.

Can I check normality using the built-in plot() function on my model?

Yes, plot(model) produces four plots, including a Q-Q plot of residuals. That is a quick visual check. However, for a formal test, you need to run a statistical test like Shapiro-Wilk.

What does a p-value greater than 0.05 mean?

It means you fail to reject the null hypothesis of normality. This suggests that the residuals are consistent with a normal distribution, but it does not prove they are exactly normal. Always combine with visual inspection.

Should I worry about normality if my sample size is large (e.g., n=1000)?

With large samples, the normality assumption becomes less critical for the validity of confidence intervals and p-values due to the Central Limit Theorem. However, severe non-normality can still affect prediction intervals and the efficiency of estimates. It's good practice to check, but you might tolerate minor deviations.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this section

The whole Moles & Molar Mass section →