Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_stat_test_shapiro_wilk (g01dd)

## Purpose

nag_stat_test_shapiro_wilk (g01dd) calculates Shapiro and Wilk's W$W$ statistic and its significance level for testing Normality.

## Syntax

[w, pw, ifail, a] = g01dd(x, 'n', n, 'a', a)
[w, pw, ifail, a] = nag_stat_test_shapiro_wilk(x, 'n', n, 'a', a)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: a is now optional, calwts is no longer an input parameter, output parameters have been reordered
.

## Description

nag_stat_test_shapiro_wilk (g01dd) calculates Shapiro and Wilk's W$W$ statistic and its significance level for any sample size between 3$3$ and 5000$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 x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$ sorted into either ascending or descending order (nag_sort_realvec_sort (m01ca) may be used to sort the data) this function calculates the value of Shapiro and Wilk's W$W$ statistic defined as:
W = (
 (n ) ∑ aixii = 1 2
)/( ∑ i = 1n (xi − x)2),
$W= (∑i=1naixi) 2 ∑i=1n (xi-x-) 2 ,$
where x = 1/n 1n xi $\stackrel{-}{x}=\frac{1}{n}\sum _{1}^{n}{x}_{i}$ is the sample mean and ai ${a}_{i}$, for i = 1,2,,n $i=1,2,\dots ,n$, are a set of ‘weights’ whose values depend only on the sample size n$n$.
On exit, the values of ai${a}_{i}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$, are only of interest should you wish to call the function again to calculate w${\mathbf{w}}$ and its significance level for a different sample of the same size.
It is recommended that the function is used in conjunction with a Normal (QQ)$\left(Q-Q\right)$ plot of the data. Functions nag_stat_normal_scores_exact (g01da) and nag_stat_normal_scores_approx (g01db) can be used to obtain the required Normal scores.

## References

Royston J P (1982) Algorithm AS 181: the W$W$ test for normality Appl. Statist. 31 176–180
Royston J P (1986) A remark on AS 181: the W$W$ test for normality Appl. Statist. 35 232–234
Royston J P (1992) Approximating the Shapiro–Wilk's W$W$ test for non-normality Statistics & Computing 2 117–119
Royston J P (1995) A remark on AS R94: A remark on Algorithm AS 181: the W$W$ test for normality Appl. Statist. 44(4) 547–551

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint 3n5000$3\le {\mathbf{n}}\le 5000$.
The ordered sample values, xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the arrays x, a. (An error is raised if these dimensions are not equal.)
n$n$, the sample size.
Constraint: 3n5000$3\le {\mathbf{n}}\le 5000$.
2:     a(n) – double array
If supplied, a must contain the n$n$ weights as calculated in a previous call to nag_stat_test_shapiro_wilk (g01dd), otherwise a need not be set.

None.

### Output Parameters

1:     w – double scalar
The value of the statistic, w${\mathbf{w}}$.
2:     pw – double scalar
The significance level of w${\mathbf{w}}$.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).
4:     a(n) – double array
The n$n$ weights required to calculate w${\mathbf{w}}$.

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 3${\mathbf{n}}<3$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, n > 5000${\mathbf{n}}>5000$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, the elements in x are not in ascending or descending order or are all equal.

## Accuracy

There may be a loss of significant figures for large n$n$.

The time taken by nag_stat_test_shapiro_wilk (g01dd) depends roughly linearly on the value of n$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 nag_stat_test_shapiro_wilk (g01dd) for a given sample size, unless calwts is reset to true before each call of nag_stat_test_shapiro_wilk (g01dd).
The Shapiro and Wilk's W$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 w${\mathbf{w}}$,
WA = W ( ∑ x(i) − x 2 )/(
 (n ) ∑ x(i) = x2 − (n − 1)/12ω2i = 1
) ,
$WA=W ∑ x(i) - x- 2 { ∑i=1n x(i)=x- 2 - n-1 12 ω2 } ,$
where ω$\omega$ is the rounding width. WA$WA$ can be compared with a standard Normal distribution, but a further approximation is given by Royston (1986).
If n > 5000${\mathbf{n}}>5000$, a value for w and pw is returned, but its accuracy may not be acceptable. See Section [References] for more details.

## Example

```function nag_stat_test_shapiro_wilk_example
x = [0.11;
0.21;
0.42;
0.46;
0.47;
0.71;
0.93;
1.52;
3.14;
3.24;
4.43;
4.55;
4.61;
4.75;
4.97;
6.66;
7.87;
7.95;
9.53;
10.14];
[w, pw, ifail, a] = nag_stat_test_shapiro_wilk(x)
```
```

w =

0.9005

pw =

0.0421

ifail =

0

a =

0.4734
0.3217
0.2557
0.2083
0.1686
0.1336
0.1015
0.0713
0.0423
0.0140
0
0
0
0
0
0
0
0
0
0

```
```function g01dd_example
x = [0.11;
0.21;
0.42;
0.46;
0.47;
0.71;
0.93;
1.52;
3.14;
3.24;
4.43;
4.55;
4.61;
4.75;
4.97;
6.66;
7.87;
7.95;
9.53;
10.14];
[w, pw, ifail, a] = g01dd(x)
```
```

w =

0.9005

pw =

0.0421

ifail =

0

a =

0.4734
0.3217
0.2557
0.2083
0.1686
0.1336
0.1015
0.0713
0.0423
0.0140
0
0
0
0
0
0
0
0
0
0

```