This guide shows you how to test whether a dataset follows a normal (Gaussian) distribution using R. You’ll learn to run the Shapiro-Wilk test, the Kolmogorov-Smirnov test, and create Q-Q plots. You need R installed with the stats package (base R) and, optionally, ggplot2 for advanced plotting.
What You’ll Need
- R (version 3.5 or later) installed on your computer.
- A dataset or vector of numeric values to test.
- Optional: the
ggplot2package for custom Q-Q plots (install.packages("ggplot2")if not already installed).
Step-by-Step
- Load your data into R. If your data is in a CSV file, use
data <- read.csv("yourfile.csv"). For a simple vector, typex <- c(1.2, 2.3, 3.1, ...). Ensure the data is numeric and contains no missing values (remove NA withna.omit()). - Visualize the distribution (optional but recommended). Create a histogram:
hist(x, breaks=30, col="lightblue"). Also make a density plot:plot(density(x)). A bell-shaped curve suggests normality, but this is not definitive. - Run the Shapiro-Wilk test. This is the most powerful test for normality in R. Use
shapiro.test(x). The output gives a W statistic and a p-value. If the p-value is greater than 0.05, you cannot reject the null hypothesis that the data is normal (i.e., it is likely normal). - Run the Kolmogorov-Smirnov test. For larger samples (n > 50), the Shapiro-Wilk test can be unreliable. Use
ks.test(x, "pnorm", mean(x), sd(x)). This compares your data to a normal distribution with the same mean and standard deviation. Again, a p-value > 0.05 suggests normality. - Create a Q-Q plot. Use
qqnorm(x)followed byqqline(x, col="red"). If the points lie roughly along the straight red line, the data is approximately normal. For a ggplot version:ggplot(data.frame(x), aes(sample=x)) + stat_qq() + stat_qq_line(). - Interpret the results together. No single test is perfect. Combine the p-values from the tests with the visual Q-Q plot. If the p-values are high and the Q-Q plot looks linear, you can assume normality for most statistical procedures.
Common Mistakes
- Using the wrong test for small samples. The Kolmogorov-Smirnov test has low power for small samples; prefer Shapiro-Wilk for n < 50.
- Ignoring missing values. Tests will fail if your vector contains NA. Always clean data first.
- Misinterpreting the p-value. A p-value > 0.05 does not prove normality; it only means you lack evidence to reject it. For small samples, even non-normal data can pass.
- Testing the wrong variable. For regression, you should test the residuals, not the raw dependent variable. Use
resid(model).
Tips
- For large datasets (n > 5000), even tiny deviations from normality become significant. In such cases, rely more on the Q-Q plot than the p-value.
- If your data is not normal, consider transformations (log, square root, Box-Cox) before applying parametric tests.
- Use
set.seed(123)before generating random data to make your results reproducible. - For a quick visual check, use
car::qqPlot(x)from thecarpackage, which adds confidence bands.
Troubleshooting
- Error: “is.numeric(x) is not TRUE” – Your data may be a data frame or a factor. Convert with
x <- as.numeric(unlist(x)). - Error in shapiro.test: “sample size must be between 3 and 5000” – Shapiro-Wilk only works for n between 3 and 5000. For larger samples, use
ks.test()or the Anderson-Darling test from thenortestpackage. - KS test gives a warning about ties – This happens when you have repeated values. Use
ks.test(x, "pnorm", mean(x), sd(x), ties.method="average")or consider the Shapiro-Wilk test instead. - Q-Q plot looks curved but p-value is high – This can occur with moderate sample sizes. Trust the plot more than the p-value; consider a transformation.
- Data is not numeric (e.g., contains characters) – Check with
str(x). Convert usingas.numeric()after removing non-numeric entries.
Frequently Asked Questions
What is the null hypothesis of the Shapiro-Wilk test?
The null hypothesis is that the data come from a normal distribution. A p-value > 0.05 means you fail to reject this, so you assume normality.
Can I use the Kolmogorov-Smirnov test for any sample size?
It works for any size, but it is less powerful than Shapiro-Wilk for small samples. For n < 50, prefer Shapiro-Wilk.
What if my data has outliers? Does that affect normality tests?
Outliers can heavily influence both the test statistic and the p-value. They often cause a rejection of normality. Consider whether the outlier is a data entry error or a genuine extreme value.
Is it enough to just look at a histogram?
No. Histograms can be misleading, especially with small samples. Always combine visual checks with formal tests.
What does a Q-Q plot tell me that a histogram doesn’t?
A Q-Q plot shows deviations from normality in the tails more clearly. It’s easier to spot skewness or heavy tails.
Do I need to check normality for every statistical test?
No. Tests like t-tests and ANOVA assume normality of residuals, but many non-parametric alternatives exist. Check normality when the test requires it, and consider the Central Limit Theorem for large samples.
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
- ug to ppb Calculator: Convert Micrograms per Liter1.00 ppb
- Grams to Moles Chemistry: How to Convert0.2000 mol/L
- Convert ug/l to ppb Calculator
- CPP Calculator Ontario: How CPP Is Calculated162.12 $/month
- Organic Chemistry Reaction Calculator: How It Works
- Grams to Moles to Molecules: How to Convert0.2000 mol/L