swtest
statistics: h = swtest (x)
statistics: h = swtest (x, name, value)
statistics: [h, p] = swtest (…)
statistics: [h, p, swstat, critval] = swtest (…)
Shapiro-Wilk hypothesis test of composite normality.
h = swtest (x) performs the Shapiro-Wilk test of the null
hypothesis that the sample in the vector x comes from a normal
distribution with unknown mean and variance, against the alternative that it
does not come from a normal distribution. The result h is 1 if the
test rejects the null hypothesis at the 5% significance level, and 0
otherwise. x must be a vector of real values; NaN values are
treated as missing and removed.
The test statistic is $$ W = \frac{\left( \sum_{i=1}^n a_i x_{(i)} \right)^2} {\sum_{i=1}^n (x_i - \bar{x})^2}, $$ where the weights a, applied to the sorted sample, are derived from the expected values of the order statistics of a standard normal sample of size n. W lies in (0,1], and small values of W are evidence against normality.
The following Name-Value pairs are supported:
| Name | Value |
|---|---|
'Alpha' | The significance level, a scalar in the range (0,1). The default is 0.05. |
'Method' | The test to perform:
'shapiro-wilk' (default) or 'shapiro-francia'. |
Source Code: swtest
'shapiro-wilk' computes the weights and the p-value with Royston’s
algorithm AS R94, which approximates the weights of Shapiro and Wilk and
transforms W to a normal deviate. It is the algorithm used by R’s
shapiro.test, and it accepts samples of 3 to 5000 values. For
n = 3 the p-value is exact.
'shapiro-francia' performs the Shapiro-Francia test instead, whose
statistic W' is the squared correlation between the sorted sample and
the approximate expected normal order statistics
norminv (((1:n) - 3/8) / (n + 1/4)). Its p-value is Royston’s
normal approximation for W', as in sf.test of R’s
nortest package, and it accepts samples of 5 to 5000 values. The
Shapiro-Francia test is known to be more powerful than the Shapiro-Wilk test
against leptokurtic alternatives. The test is never selected automatically;
the method used is the one requested.
[h, p] = swtest (…) also returns the p-value
p of the test, the probability of observing a statistic as small as
swstat under the null hypothesis.
[h, p, swstat, critval] = swtest (…)
also returns the test statistic swstat, W or W', and the
critical value critval at significance level alpha, obtained by
inverting the same approximation. The null hypothesis is rejected when
swstat < critval, which is the same as
p < alpha.
A sample whose values are all equal has no defined statistic and is refused,
as is a sample holding an infinite value. Samples of more than 5000 values
are refused, since Royston’s approximations are calibrated up to that size;
use adtest or jbtest for larger samples.
MATLAB has no Shapiro-Wilk test, so swtest is specific to Octave.
References:
See also: adtest, jbtest, kstest, lillietest
Source Code: swtest
Test whether a sample departs from normality
x = [148 154 158 160 161 162 166 170 182 195 236]; [h, p, W] = swtest (x)
h = 1 p = 6.7038e-03 W = 0.7888
The Shapiro-Francia test on the same sample
x = [148 154 158 160 161 162 166 170 182 195 236]; [h, p, W] = swtest (x, 'Method', 'shapiro-francia')
h = 1 p = 7.3476e-03 W = 0.7714