This guide shows you how to check whether your data follows a normal (Gaussian) distribution using R and RStudio. You’ll learn to run the Shapiro-Wilk test, the Kolmogorov-Smirnov test, and to create Q-Q plots and histograms. Testing for normality is often a prerequisite for choosing between parametric tests (like t-tests and ANOVA) and non-parametric alternatives (like Mann-Whitney U or Kruskal-Wallis). By the end, you’ll be able to interpret the results and decide if your data meets the normality assumption.
What You’ll Need
- R and RStudio installed on your computer.
- A dataset loaded into R (either built-in, imported from CSV, or created manually).
- Basic familiarity with R syntax (assigning variables, using functions).
Step-by-Step
- Load or create your data. For demonstration, we’ll use the built-in
irisdataset. In your R console, type:data(iris). This loads the dataset. We’ll test theSepal.Lengthcolumn for normality. - Extract the numeric vector. Assign the column to a variable:
sepal_len <- iris$Sepal.Length. This isolates the numeric values you want to test. - Visual inspection – histogram. Create a histogram to see the shape:
hist(sepal_len, main="Histogram of Sepal Length", xlab="Sepal Length (cm)"). Look for a bell-shaped curve. If the histogram looks roughly symmetric and bell-shaped, normality is plausible. - Visual inspection – Q-Q plot. A Q-Q plot compares your data quantiles to theoretical normal quantiles. Run:
qqnorm(sepal_len)andqqline(sepal_len, col="red"). If the points closely follow the straight red line, the data is approximately normal. Systematic deviations (e.g., S-shape, curves) suggest non-normality. - Shapiro-Wilk test. This is the most powerful test for normality, especially for small samples (n < 50). Run:
shapiro.test(sepal_len). The output gives a W statistic and a p-value. If the p-value is greater than 0.05, you fail to reject the null hypothesis that the data is normally distributed (i.e., normality is assumed). If p < 0.05, the data significantly deviates from normality. - Kolmogorov-Smirnov test (for larger samples). For n > 50, you might use the KS test, but note it’s less powerful and requires specifying the mean and SD. Run:
ks.test(sepal_len, "pnorm", mean=mean(sepal_len), sd=sd(sepal_len)). Interpret the p-value similarly: p > 0.05 suggests normality. - Check for multiple groups (if applicable). If you’re testing normality within groups (e.g., for each iris species), use
by(iris$Sepal.Length, iris$Species, shapiro.test)to run the test separately for each group. - Record and interpret results. Write down the test statistic and p-value. Combine the statistical test with the visual plots. If both agree (e.g., p > 0.05 and plots look normal), conclude normality holds. If they disagree, rely on the Shapiro-Wilk test for small samples and the plots for large ones.
Common Mistakes
- Using the Shapiro-Wilk test on very large datasets (n > 5000) – it becomes overly sensitive to minor deviations that are practically irrelevant.
- Ignoring outliers – a single extreme value can break normality. Always inspect boxplots and histograms first.
- Testing the wrong variable – ensure you’re testing the numeric variable, not the factor or ID column.
- Misinterpreting the p-value – a p-value > 0.05 does not prove normality; it only means we cannot rule it out.
- Forgetting to specify mean and sd in the KS test – the default is standard normal (mean=0, sd=1), which is almost always wrong for real data.
Tips
- Always combine a statistical test with a visual check. Numbers alone can mislead, especially with large samples.
- For small samples (n < 30), the Shapiro-Wilk test is recommended; for larger samples, consider the Anderson-Darling test (from the
nortestpackage) or rely more on Q-Q plots. - If your data is not normal, consider transformations (log, square root, Box-Cox) before switching to non-parametric tests.
- Use the
ggplot2package for more polished plots:ggplot(iris, aes(sample=Sepal.Length)) + geom_qq() + geom_qq_line(). - Remember that many tests (like t-tests) are robust to mild deviations from normality, especially with large samples due to the Central Limit Theorem.
Troubleshooting
- Error: ‘x’ must be numeric. This means your variable is not numeric. Convert it using
as.numeric(), but first check for character strings or factors. - Shapiro test fails with “sample size must be between 3 and 5000”. Your sample is too large. Use the KS test or the Anderson-Darling test instead.
- KS test gives a warning about ties. The KS test assumes continuous data; ties (repeated values) can occur with discrete data. Consider using the Shapiro-Wilk test or add small jitter:
jitter(your_data, amount=0.001). - Plots look weird. Check for missing values (NA). Remove them with
na.omit()or usecomplete.cases(). - You get different p-values each time you run the test. This can happen if you’re using a random seed or if your data has random components. Ensure your data is fixed.
Frequently Asked Questions
What is the null hypothesis of the Shapiro-Wilk test?
The null hypothesis is that the data comes from a normally distributed population. A p-value less than 0.05 rejects this, indicating the data is not normal.
Can I use the Shapiro-Wilk test for any sample size?
No, it’s designed for sample sizes between 3 and 5000. For larger samples, use the Kolmogorov-Smirnov test (with estimated parameters) or the Anderson-Darling test.
What if my data has outliers? Should I remove them before testing?
Outliers can heavily influence normality tests. Investigate whether they are errors or genuine values. If they are true observations, consider robust methods or transformations instead of simply deleting them.
Is it necessary to test normality before every t-test?
It’s good practice, especially for small samples. However, for large samples (n > 30), the t-test is robust to non-normality due to the Central Limit Theorem. Still, checking is safer.
What does a Q-Q plot tell me that a histogram doesn’t?
A Q-Q plot highlights deviations in the tails more clearly than a histogram. It shows exactly how your data deviates (e.g., heavy tails, skewness) relative to the normal distribution.
Can I rely solely on the p-value to decide normality?
No. Always combine the p-value with visual inspection. With large samples, even trivial deviations become significant, so the plots are often more informative.
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
- Normality to g/L Converter & Formula29.220 g/L
- Normality to meq/mL Conversion Calculator1.0000 meq/mL
- Normality Test Q-Q Plot: How to Read It
- Normality Test Results: How to Analyze Them
- Normality Slay the Spire?
- Normality Test for Paired t Test: How to Check