grayscale photo of people walking on sidewalk

The Shapiro-Wilk Test

The Shapiro-Wilk test is a statistical method used to determine whether a dataset follows a normal distribution. Developed by Samuel Sanford Shapiro and Martin Wilk in 1965, this test is widely employed in various fields of research to assess the normality of data, which is a crucial assumption for many statistical analyses

Theory Behind the Shapiro-Wilk Test

Null and Alternative Hypotheses

The Shapiro-Wilk test operates under the following hypotheses:

  • Null Hypothesis (H0): The data is normally distributed.
  • Alternative Hypothesis (H1): The data is not normally distributed.

Test Statistic Calculation

The test statistic W is calculated using the formula:

Mathematical Expression
\( W = \frac{\left(\sum_{i=1}^{n}a_{i}x_{(i)}\right)^{2}}{\sum_{i=1}^{n}(x_{i}-\overline{x})^{2}} \)
  • x(i) is the i-th order statistic (the i-th smallest number in the sample),
  • x̄ is the sample mean,
  • ai are coefficients derived from the expected values of order statistics of a standard normal distribution.

Interpretation of Results

The interpretation of the Shapiro-Wilk test results is based on the p-value:

  • p-value ≤ 0.05: Reject the null hypothesis, indicating that the data is likely not normally distributed.
  • p-value > 0.05: Fail to reject the null hypothesis, suggesting that the data may be normally distributed.

Practical Applications of the Shapiro-Wilk Test

Importance in Statistical Analysis

Normality is a key assumption for many parametric statistical tests, such as t-tests and ANOVA. The Shapiro-Wilk test helps determine whether these tests are appropriate for the data at hand. If the data is not normally distributed, non-parametric alternatives may be necessary.

Limitations

While the Shapiro-Wilk test is powerful for detecting deviations from normality, it has limitations:

  • Large Sample Sizes: The test may detect trivial deviations from normality with large sample sizes, leading to unnecessary rejection of the null hypothesis.
  • Small Sample Sizes: The test may lack power to detect significant deviations from normality with small sample sizes.

Complementary Methods

To ensure accurate conclusions about normality, it is advisable to use the Shapiro-Wilk test in conjunction with graphical methods like Q-Q plots and histograms, as well as measures of skewness and kurtosis.

Performing the Shapiro-Wilk Test in Python

Python’s scipy.stats module provides a convenient function to perform the Shapiro-Wilk test. Here’s how you can use it:

    
      import numpy as np
      from scipy import stats
      import matplotlib.pyplot as plt

      # Generate a normally distributed dataset
      np.random.seed(0)
      normal_data = np.random.normal(loc=0, scale=1, size=100)

      # Generate a non-normally distributed dataset (e.g., exponential)
      non_normal_data = np.random.exponential(scale=1, size=100)

      # Perform Shapiro-Wilk test on both datasets
      normal_stat, normal_p = stats.shapiro(normal_data)
      non_normal_stat, non_normal_p = stats.shapiro(non_normal_data)

      print(f"Normal Data: W-statistic = {normal_stat}, p-value = {normal_p}")
      print(f"Non-Normal Data: W-statistic = {non_normal_stat}, p-value = {non_normal_p}")

      # Plot histograms for visual inspection
      plt.figure(figsize=(10, 5))

      plt.subplot(1, 2, 1)
      plt.hist(normal_data, bins=20, alpha=0.7, color='blue', edgecolor='black')
      plt.title('Normally Distributed Data')

      plt.subplot(1, 2, 2)
      plt.hist(non_normal_data, bins=20, alpha=0.7, color='red', edgecolor='black')
      plt.title('Non-Normally Distributed Data')

      plt.tight_layout()
      plt.show()
    
  

This code generates both a normally distributed dataset and a non-normally distributed dataset, performs the Shapiro-Wilk test on each, and plots histograms for visual inspection.

Conclusion

The Shapiro-Wilk test is a valuable tool for assessing the normality of data, which is essential for selecting appropriate statistical methods in research. By understanding its application and limitations, researchers can make informed decisions about data analysis and interpretation.


Citations:

[1]: https://en.wikipedia.org/wiki/Shapiro–Wilk_test

[2]: https://builtin.com/data-science/shapiro-wilk-test

[3]: https://guides.library.lincoln.ac.uk/mash/statstest/shapiro-wilk

[4]: https://www.kaggle.com/general/432129