File Exchange

## Shapiro-Wilk and Shapiro-Francia normality tests.

version 1.1.0.0 (8.83 KB) by
Shapiro-Wilk & Shapiro-Francia parametric hypothesis test of composite normality.

Updated 18 Jun 2014

View Version History

Shapiro-Wilk parametric hypothesis test of composite normality, for sample size 3<= n <= 5000. Based on Royston R94 algorithm.
This test also performs the Shapiro-Francia normality test for platykurtic samples.

### Cite As

Ahmed BenSaïda (2020). Shapiro-Wilk and Shapiro-Francia normality tests. (https://www.mathworks.com/matlabcentral/fileexchange/13964-shapiro-wilk-and-shapiro-francia-normality-tests), MATLAB Central File Exchange. Retrieved .

Javier Valdes

Ivan Rojkov

Felipe Viana

Thanks for the submission! I changed as proposed by Philipp Dehnen.

Aidan Starr

Edward Soares

So I've been using this script (thanks Ahmed) for a while now, testing for normality with small sample sizes (n < 10). Works great, but it seems to report a false positive rate of .06 when the target I use to test is .05. I am doing large scale randomizations, and so it's pretty consistent whether I use 4 <= n <= 10 or 25 <= n <= 35. When I forced the SW test, it gave me the correct FP rate of .05. I fear that there is some sort of weird issue with the SF test.

Lars reports a similar issue (Mar 2008). I tried to see if it was the kurtosis calculation (see Luis Dec 2010) but it didn't fix the issue.

So I recommend forcing SW and avoiding the slightly higher FP rate. Just my opinion.

Edward Soares

Philipp Dehnen

Mindaugas: to add the option to force always wilk intead of francia you can easily change the following:

line 1:
function [H, pValue, W] = swtest(x, alpha, wilk)
line 124:
if kurtosis(x) > 3 && ~wilk

Now you should be able to force wilk test by set wilk in the input argument to true.
Wilk must be logical, if you want you can add further error checking lines on this.

Mindaugas

It would be nice to implement testing normality not only for vectors, but also for 2D (or even for arrays with more dimensions), like 'ttest' with 'Dim' option.

Mindaugas

Thanks, but you must add option to force using Shapiro-Wilk always

Peipei Liu

Wanna try this code.

Johannes

Thanks for this implementation. I use it a lot in my current project.
I found a problem for samples which are uniform distributed, e.g.:

swtest(ones(1,6))

fails with:
Error using erfc
Input must be real and full.

I traced this back to line 211

W = (weights' * x) ^2 / ((x - mean(x))' * (x - mean(x)));

W can become NaN of Inf if the sample is uniform. I'm not sure how to treat this correctly. Just check if W is NaN/Inf and if so reject the null hypothesis?

Hassan Naseri

Ahmed BenSaïda

- the kurtosis (line 119) does not modify the power of the test, it's barely used to help choosing between Shapiro-Wilk and Shapiro-Francia method. Moreover, it's better to use the sample kurtosis 'kurtosis(x)'.
- When posing x=norminv((1:9)/10)), x here is not normally distributed, it represents the inverse of the CDF which is not normal by definition. So if you want to test its power you can compute x=normrnd(mu, sigma, n, 1), where you can choose the size of your sample (n) and perform the test.

Willem-Jan de Goeij

Can you explain the following?
x is normally distributed.
If I perform a 2 tailed test, your function rejects the null hypothesis.

x = norminv((1:9)/10);
[h,p,w]=swtest(x,0.05,0)
h =
1
p =
0.0028
w =
0.9925

Sav Deb

Sorry, what is the usefulness of tail option? When to use 1,0 or -1 value?
Thank you for help

Jannick

rui

i am unable to test the code with my data, and i don't know why.

Ricardo Luis

I tested this code but I have a doubt. In line 119, the kurtosis computation seems to be for a population (kurtosis(x)) and not for a sample (kurtosis(x,0)). So, in line 119 shouldn't it be "if kurtosis(x,0)> 3" (flag=0)?
Thanks.

steven pav

I tested this code for sample sizes as small as 4, and as large as 4096. at all levels tested, the p-values were fairly uniform. Thus it appears this test maintains the nominal rejection rate very well. (the Anderson-Darling test has similarly good performance for small sample sizes.)

Jared Lou

Has anyone Tried to Validate this other than LARS?

Raghav

thanks for implementing the code for Shapiro-Wilk and Shapiro-Francia normality tests.

NIZAR OUARTI

good comments,easy to use and reference of the algorythm is present.

Lars Hoffmann

I have the impression, that this implementation is more liberal than it should be according to literature. On 1000 runs at 0.1-level with various sample sizes I calculated an average empirical alpha of 0.11.

Jim Rohlf

Sorry, the file seems ok. My problem was that I had an out-of-date copy of the distchck.m file on my computer.

Jim Rohlf

I just tried it on some test data (n=16) and it crashed because the value of the variable newSWstatistic was imaginary.

Gleb Tcheslavski

##### MATLAB Release Compatibility
Created with R14
Compatible with any release
##### Platform Compatibility
Windows macOS Linux