The Shapiro-Wilk’s test or Shapiro test is a normality test in frequentist statistics. The null hypothesis of Shapiro’s test is that the population is distributed normally. It is among the three tests for normality designed for detecting all kinds of departure from normality. If the value of p is equal to or less than 0.05, then the hypothesis of normality will be rejected by the Shapiro test. On failing, the test can state that the data will not fit the distribution normally with 95% confidence. However, on passing, the test can state that there exists no significant departure from normality. This test can be done very easily in R programming.
Shapiro-Wilk’s Test Formula
Suppose a sample, say x1,x2…….xn, has come from a normally distributed population. Then according to the Shapiro-Wilk’s tests null hypothesis test

where,
- x(i) : it is the ith smallest number in the given sample.
- mean(x) : ( x1+x2+……+xn) / n i.e the sample mean.
- ai : coefficient that can be calculated as (a1,a2,….,an) = (mT V-1)/C . Here V is the covariance matrix, m and C are the vector norms that can be calculated as C= || V-1 m || and m = (m1, m2,……, mn ).
Implementation in R
To perform the Shapiro Wilk Test, R provides shapiro.test() function.
Syntax:
shapiro.test(x)
Parameter:
x : a numeric vector containing the data values. It allows missing values but the number of missing values should be of the range 3 to 5000.
Let us see how to perform the Shapiro Wilk’s test step by step.
- Step 1: At first install the required packages. The two packages that are required to perform the test are dplyr. The dplyr package is needed for efficient data manipulation. One can install the packages from the R console in the following way:
install.packages("dplyr")
- Step 2: Now load the installed packages into the R Script. It can be done by using the library() function in the following way.
- Step 3: The most important task is to select a proper data set. Here let’s work with the ToothGrowth data set. It is an in-built data set in the R library.
R
library ( "dplyr" )
my_data <- ToothGrowth
|
One can also create their own data set. For that first prepare the data, then save the file and then import the data set into the script. The file can include using the following syntax:
data <- read.delim(file.choose()) ,if the format of the file is .txt
data <- read.csv(file.choose()), if the format of the file is .csv
- Step 4: Now select a random number using the set.seed() function. Following which we start displaying an output sample of 10 rows chosen randomly using the sample_n() function of the dplyr package. This is how we check our data.
R
library ( "dplyr" )
my_data <- ToothGrowth
set.seed (1234)
dplyr:: sample_n (my_data, 10)
|
Output:
len supp dose
1 11.2 VC 0.5
2 8.2 OJ 0.5
3 10.0 OJ 0.5
4 27.3 OJ 2.0
5 14.5 OJ 1.0
6 26.4 OJ 2.0
7 4.2 VC 0.5
8 15.2 VC 1.0
9 14.5 OJ 0.5
10 26.7 VC 2.0
- Step 5: At last perform the Shapiro Wilk’s test using the shapiro.test() function.
R
library ( "dplyr" )
my_data <- ToothGrowth
set.seed (1234)
dplyr:: sample_n (my_data, 10)
shapiro.test (my_data$len)
|
Output:
> dplyr::sample_n(my_data, 10)
len supp dose
1 11.2 VC 0.5
2 8.2 OJ 0.5
3 10.0 OJ 0.5
4 27.3 OJ 2.0
5 14.5 OJ 1.0
6 26.4 OJ 2.0
7 4.2 VC 0.5
8 15.2 VC 1.0
9 14.5 OJ 0.5
10 26.7 VC 2.0
> shapiro.test(my_data$len)
Shapiro-Wilk normality test
data: my_data$len
W = 0.96743, p-value = 0.1091
From the output obtained we can assume normality. The p-value is greater than 0.05. Hence, the distribution of the given data is not different from normal distribution significantly.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Jul, 2020
Like Article
Save Article