NAG Library Routine Document
g01ddf (test_shapiro_wilk)
1
Purpose
g01ddf calculates Shapiro and Wilk's $W$ statistic and its significance level for testing Normality.
2
Specification
Fortran Interface
Integer, Intent (In)  ::  n  Integer, Intent (Inout)  ::  ifail  Real (Kind=nag_wp), Intent (In)  ::  x(n)  Real (Kind=nag_wp), Intent (Inout)  ::  a(n)  Real (Kind=nag_wp), Intent (Out)  ::  w, pw  Logical, Intent (In)  ::  calwts 

C Header Interface
#include nagmk26.h
void 
g01ddf_ (const double x[], const Integer *n, const logical *calwts, double a[], double *w, double *pw, Integer *ifail) 

3
Description
g01ddf calculates Shapiro and Wilk's
$W$ statistic and its significance level for any sample size between
$3$ and
$5000$. It is an adaptation of the Applied Statistics Algorithm AS R94, see
Royston (1995). The full description of the theory behind this algorithm is given in
Royston (1992).
Given a set of observations
${x}_{1},{x}_{2},\dots ,{x}_{n}$ sorted into either ascending or descending order (
m01caf may be used to sort the data) this routine calculates the value of Shapiro and Wilk's
$W$ statistic defined as:
where
$\stackrel{}{x}=\frac{1}{n}{\displaystyle \sum _{1}^{n}}{x}_{i}$ is the sample mean and
${a}_{i}$, for
$i=1,2,\dots ,n$, are a set of ‘weights’ whose values depend only on the sample size
$n$.
On exit, the values of ${a}_{i}$, for $\mathit{i}=1,2,\dots ,n$, are only of interest should you wish to call the routine again to calculate ${\mathbf{w}}$ and its significance level for a different sample of the same size.
It is recommended that the routine is used in conjunction with a Normal
$\left(QQ\right)$ plot of the data. Routines
g01daf and
g01dbf
can be used to obtain the required Normal scores.
4
References
Royston J P (1982) Algorithm AS 181: the $W$ test for normality Appl. Statist. 31 176–180
Royston J P (1986) A remark on AS 181: the $W$ test for normality Appl. Statist. 35 232–234
Royston J P (1992) Approximating the Shapiro–Wilk's $W$ test for nonnormality Statistics & Computing 2 117–119
Royston J P (1995) A remark on AS R94: A remark on Algorithm AS 181: the $W$ test for normality Appl. Statist. 44(4) 547–551
5
Arguments
 1: $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput

On entry: the ordered sample values,
${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
 2: $\mathbf{n}$ – IntegerInput

On entry: $n$, the sample size.
Constraint:
$3\le {\mathbf{n}}\le 5000$.
 3: $\mathbf{calwts}$ – LogicalInput

On entry: must be set to .TRUE. if you wish
g01ddf to calculate the elements of
a.
calwts should be set to .FALSE. if you have saved the values in
a from a previous call to
g01ddf.
If in doubt, set
calwts equal to .TRUE..
 4: $\mathbf{a}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output

On entry: if
calwts has been set to .FALSE. then before entry
a must contain the
$n$ weights as calculated in a previous call to
g01ddf, otherwise
a need not be set.
On exit: the $n$ weights required to calculate ${\mathbf{w}}$.
 5: $\mathbf{w}$ – Real (Kind=nag_wp)Output

On exit: the value of the statistic, ${\mathbf{w}}$.
 6: $\mathbf{pw}$ – Real (Kind=nag_wp)Output

On exit: the significance level of ${\mathbf{w}}$.
 7: $\mathbf{ifail}$ – IntegerInput/Output

On entry:
ifail must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is
$0$.
When the value $\mathbf{1}\text{ or}\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit:
${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
${\mathbf{ifail}}=0$ or
$1$, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Errors or warnings detected by the routine:
 ${\mathbf{ifail}}=1$

On entry, ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{n}}\ge 3$.
 ${\mathbf{ifail}}=2$

On entry, ${\mathbf{n}}=\u2329\mathit{\text{value}}\u232a$.
Constraint: ${\mathbf{n}}\le 5000$.
 ${\mathbf{ifail}}=3$

On entry, all elements of
x are equal.
On entry, elements of
x not in order.
${\mathbf{x}}\left(\u2329\mathit{\text{value}}\u232a\right)=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{x}}\left(\u2329\mathit{\text{value}}\u232a\right)=\u2329\mathit{\text{value}}\u232a$,
${\mathbf{x}}\left(\u2329\mathit{\text{value}}\u232a\right)=\u2329\mathit{\text{value}}\u232a$.
 ${\mathbf{ifail}}=99$
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=399$
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
 ${\mathbf{ifail}}=999$
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
There may be a loss of significant figures for large $n$.
8
Parallelism and Performance
g01ddf is not threaded in any implementation.
The time taken by g01ddf depends roughly linearly on the value of $n$.
For very small samples the power of the test may not be very high.
The contents of the array
a should not be modified between calls to
g01ddf for a given sample size, unless
calwts is reset to .TRUE. before each call of
g01ddf.
The Shapiro and Wilk's
$W$ test is very sensitive to ties. If the data has been rounded the test can be improved by using Sheppard's correction to adjust the sum of squares about the mean. This produces an adjusted value of
${\mathbf{w}}$,
where
$\omega $ is the rounding width.
$WA$ can be compared with a standard Normal distribution, but a further approximation is given by
Royston (1986).
If
${\mathbf{n}}>5000$, a value for
w and
pw is returned, but its accuracy may not be acceptable. See
Section 4 for more details.
10
Example
This example tests the following two samples (each of size $20$) for Normality.
Sample
Number 
Data 
1 
$0.11$, $7.87$, $4.61$, $10.14$, $7.95$, $3.14$, $0.46$, $4.43$, $0.21$, $4.75$, $0.71$, $1.52$, $3.24$, $0.93$, $0.42$, $4.97$, $9.53$, $4.55$, $0.47$, $6.66$ 
2 
$1.36$, $1.14$, $2.92$, $2.55$, $1.46$, $1.06$, $5.27$, $1.11$, $3.48$, $1.10$, $0.88$, $0.51$, $1.46$, $0.52$, $6.20$, $1.69$, $0.08$, $3.67$, $2.81$, $3.49$ 
The elements of
a are calculated only in the first call of
g01ddf, and are reused in the second call.
10.1
Program Text
Program Text (g01ddfe.f90)
10.2
Program Data
Program Data (g01ddfe.d)
10.3
Program Results
Program Results (g01ddfe.r)