molecalculator

Moles & Molar Mass

How to Test Normality of Data in R

Guide 01-8617 · Updated · Step by step

This guide shows you how to test whether your data follows a normal (Gaussian) distribution using R. You’ll learn both visual methods (histogram, Q-Q plot) and statistical tests (Shapiro-Wilk, Kolmogorov-Smirnov), and how to interpret their results. By the end, you’ll be able to run these checks on your own data and decide if normal-theory methods like t-tests or ANOVA are appropriate.

What You’ll Need

  • R installed (version 4.0 or later recommended).
  • RStudio (optional but helpful).
  • A dataset with a numeric variable you want to test. If you don’t have one, you can use the built-in iris dataset (e.g., iris$Sepal.Length).
  • Basic familiarity with R syntax (assigning variables, running functions).

Step-by-Step

  1. Load your data into R. If your data is in a CSV file, use data <- read.csv("yourfile.csv"). If it’s a built-in dataset, just call it (e.g., data(iris)). For this example, we’ll use iris$Sepal.Length.
  2. Check the variable type. Ensure your variable is numeric: class(iris$Sepal.Length) should return "numeric". If it’s a factor or character, convert it with as.numeric() after checking for non-numeric entries.
  3. Create a histogram. Use hist(iris$Sepal.Length, main="Histogram of Sepal Length", xlab="Sepal Length (cm)"). Look for a bell-shaped curve, roughly symmetric around the mean.
  4. Create a Q-Q plot. Use qqnorm(iris$Sepal.Length) and qqline(iris$Sepal.Length). In a Q-Q plot, if the points lie close to the straight diagonal line, the data is approximately normal.
  5. Run the Shapiro-Wilk test. Use shapiro.test(iris$Sepal.Length). This test is powerful for small to medium samples (n < 5000). The output gives a W statistic and a p-value.
  6. Interpret the p-value. Set your significance level (commonly α = 0.05). If p > 0.05, you fail to reject the null hypothesis, meaning the data does not significantly deviate from normality. If p ≤ 0.05, the data is significantly non-normal.
  7. For larger samples (n > 5000), use the Kolmogorov-Smirnov test. First, standardize your data: z <- (iris$Sepal.Length - mean(iris$Sepal.Length)) / sd(iris$Sepal.Length) (but note: for n < 5000, Shapiro is preferred). Then run ks.test(z, "pnorm"). The p-value interpretation is the same.
  8. Consider the Anderson-Darling test (optional). If you have the nortest package installed, use library(nortest) then ad.test(iris$Sepal.Length). This test is more sensitive to tails than Shapiro.
  9. Document your results. Report the test statistic, p-value, and your conclusion (e.g., “Shapiro-Wilk test indicated that Sepal Length is normally distributed, W = 0.98, p = 0.10”).

Common Mistakes

  • Using Shapiro-Wilk on huge datasets: It can be overly sensitive, flagging trivial deviations as significant. For n > 5000, rely on visual checks or use the Kolmogorov-Smirnov test with caution.
  • Ignoring outliers: Outliers can break normality. Always plot your data first to spot extreme values.
  • Confusing “non-normal” with “not useful”: Many tests (like t-tests) are robust to moderate non-normality, especially with large samples. The test is a guide, not a strict gate.
  • Forgetting to standardize for KS test: The Kolmogorov-Smirnov test compares to a standard normal distribution (mean 0, sd 1). If you don’t standardize, you’re testing against the wrong distribution.
  • Using a histogram alone: Histograms can be misleading with small samples. Always pair with a Q-Q plot and a formal test.

Tips

  • Always start with a histogram and Q-Q plot. They give you a quick visual sense and help you spot skewness, multimodality, or outliers.
  • For small samples (n < 30), the Shapiro-Wilk test is the best choice. It has good power even with small n.
  • If your data is not normal, consider transformations (log, square root, Box-Cox) before running parametric tests.
  • Remember that normality tests answer a specific question: “Is the data consistent with a normal distribution?” They don’t tell you if the deviation matters for your analysis.
  • If you have multiple groups, test normality within each group, not on the pooled data.
  • Use the ggplot2 package for more polished visuals: ggplot(iris, aes(sample=Sepal.Length)) + geom_qq() + geom_qq_line().

Troubleshooting

  • Error: “is not a numeric vector” – Your variable might be a factor or character. Convert it with as.numeric(as.character(variable)) after checking for any non-numeric entries.
  • Error: “sample size must be between 3 and 5000” – Shapiro-Wilk test cannot handle samples outside this range. For n < 3, you can’t test; for n > 5000, use the Kolmogorov-Smirnov test or rely on visual plots.
  • Warning: “ties should not be present” in KS test – This happens when your data has repeated values. The KS test assumes continuous data. If you have ties, consider using the ks.test with exact=TRUE (for small samples) or switch to Shapiro-Wilk (which handles ties better).
  • My p-value is exactly 0.05 – what now? This is a borderline case. Look at the Q-Q plot and histogram. If they look normal, you can proceed with normal-theory methods, but also consider running a non-parametric alternative as a sensitivity check.
  • My data is clearly skewed but the test says normal? This can happen with small samples where the test lacks power. Trust the visual evidence and the domain knowledge. The test is not infallible.

Frequently Asked Questions

What is the null hypothesis of the Shapiro-Wilk test?

The null hypothesis is that the data comes from a normal distribution. A p-value greater than 0.05 means you fail to reject this, so you treat the data as normal.

Can I use a normality test on any sample size?

Shapiro-Wilk is best for n between 3 and 5000. For larger samples, use the Kolmogorov-Smirnov test (with standardization) or rely on graphical methods, as tests become overly sensitive with big n.

Should I always test normality before a t-test?

It’s good practice, but remember that t-tests are fairly robust to non-normality, especially with sample sizes above 30. If you have a small sample and severe non-normality, consider a non-parametric test like the Wilcoxon test.

What does a Q-Q plot tell me that a histogram doesn’t?

A Q-Q plot shows deviations from normality more clearly, especially in the tails. It helps you see if the data is skewed or has heavy tails, which a histogram might hide.

How do I interpret the W statistic in Shapiro-Wilk?

W ranges from 0 to 1. Values close to 1 indicate the data is close to normal. The p-value is more important: if p > 0.05, the data is considered normal.

What if my data fails the normality test?

You can try transforming the data (e.g., log, square root), or use non-parametric tests like Mann-Whitney U or Kruskal-Wallis. Also, consider if the deviation is large enough to matter for your analysis.

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 →