# NAG CL Interfaceg01ddc (test_​shapiro_​wilk)

Settings help

CL Name Style:

## 1Purpose

g01ddc calculates Shapiro and Wilk's $W$ statistic and its significance level for testing Normality.

## 2Specification

 #include
 void g01ddc (Integer n, const double x[], Nag_Boolean calc_wts, double a[], double *w, double *pw, NagError *fail)
The function may be called by the names: g01ddc, nag_stat_test_shapiro_wilk or nag_shapiro_wilk_test.

## 3Description

g01ddc 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 (m01cac may be used to sort the data) this function calculates the value of Shapiro and Wilk's $W$ statistic defined as:
 $W= (∑i=1naixi) 2 ∑i=1n (xi-x¯) 2 ,$
where $\overline{x}=\frac{1}{n}\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 function again to calculate ${\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 $\left(Q-Q\right)$ plot of the data. Function g01dac can be used to obtain the required Normal scores.

## 4References

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 non-normality 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

## 5Arguments

1: $\mathbf{n}$Integer Input
On entry: $n$, the sample size.
Constraint: $3\le {\mathbf{n}}\le 5000$.
2: $\mathbf{x}\left[{\mathbf{n}}\right]$const double Input
On entry: the ordered sample values, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
3: $\mathbf{calc_wts}$Nag_Boolean Input
On entry: must be set to Nag_TRUE if you wish g01ddc to calculate the elements of a.
calc_wts should be set to Nag_FALSE if you have saved the values in a from a previous call to g01ddc.
If in doubt, set calc_wts equal to Nag_TRUE.
4: $\mathbf{a}\left[{\mathbf{n}}\right]$double Input/Output
On entry: if calc_wts has been set to Nag_FALSE then before entry a must contain the $n$ weights as calculated in a previous call to g01ddc, otherwise a need not be set.
On exit: the $n$ weights required to calculate ${\mathbf{w}}$.
5: $\mathbf{w}$double * Output
On exit: the value of the statistic, ${\mathbf{w}}$.
6: $\mathbf{pw}$double * Output
On exit: the significance level of ${\mathbf{w}}$.
7: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALL_ELEMENTS_EQUAL
On entry, all elements of x are equal.
NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_INT_ARG_GT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\le 5000$.
NE_INT_ARG_LT
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 3$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_NON_MONOTONIC
On entry, elements of x not in order. ${\mathbf{x}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$, ${\mathbf{x}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$, ${\mathbf{x}}\left[⟨\mathit{\text{value}}⟩\right]=⟨\mathit{\text{value}}⟩$.

## 7Accuracy

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

## 8Parallelism and Performance

g01ddc is not threaded in any implementation.

The time taken by g01ddc 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 g01ddc for a given sample size, unless calc_wts is reset to Nag_TRUE before each call of g01ddc.
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}}$,
 $WA=W ∑ x(i) - x¯ 2 {∑i=1n x(i)=x¯ 2 - n-1 12 ω2} ,$
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.

## 10Example

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 g01ddc, and are re-used in the second call.

### 10.1Program Text

Program Text (g01ddce.c)

### 10.2Program Data

Program Data (g01ddce.d)

### 10.3Program Results

Program Results (g01ddce.r)