hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_stat_test_shapiro_wilk (g01dd)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


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


[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:
At Mark 23: a was made optional, calwts is no longer an input parameter; output parameters were reordered


nag_stat_test_shapiro_wilk (g01dd) 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 x1,x2,,xn 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 statistic defined as:
W= i=1naixi 2 i=1n xi-x- 2 ,  
where x- = 1n 1n xi  is the sample mean and ai , for i=1,2,,n , are a set of ‘weights’ whose values depend only on the sample size n.
On exit, the values of ai, for i=1,2,,n, are only of interest should you wish to call the function again to calculate 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 Q-Q 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.


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


Compulsory Input Parameters

1:     xn – double array
The ordered sample values, xi, for i=1,2,,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, the sample size.
Constraint: 3n5000.
2:     an – double array
If supplied, a must contain the n weights as calculated in a previous call to nag_stat_test_shapiro_wilk (g01dd), otherwise a need not be set.

Output Parameters

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

Error Indicators and Warnings

Errors or warnings detected by the function:
On entry,n<3.
On entry,n>5000.
On entry,the elements in x are not in ascending or descending order or are all equal.
An unexpected error has been triggered by this routine. Please contact NAG.
Your licence key may have expired or may not have been installed correctly.
Dynamic memory allocation failed.


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

Further Comments

The time taken by nag_stat_test_shapiro_wilk (g01dd) 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 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 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,
WA=W xi - x- 2 i=1n xi=x- 2 - n-1 12 ω2 ,  
where ω is the rounding width. WA can be compared with a standard Normal distribution, but a further approximation is given by Royston (1986).
If n>5000, a value for w and pw is returned, but its accuracy may not be acceptable. See References for more details.


This example tests the following two samples (each of size 20) for Normality.
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 nag_stat_test_shapiro_wilk (g01dd), and are re-used in the second call.
function g01dd_example

fprintf('g01dd example results\n\n');

x(:,1) = [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];
x(:,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];

calwts = true;
n1 = int64(1);
% Loop over data sets
for j=1:size(x,2)

  % sort the data
  [x, ifail] = m01ca( ...
                      x, n1, 'Ascending');
  % Calculate test statistic (using weights 'a' in subsequent calls)
  if j==1
    [w, pw, ifail, a] = g01dd( ...
    [w, pw, ifail, a] = g01dd( ...
                               x(:,j), 'a', a);

  % Display results
  fprintf('For sample number %d value of W statistic = %8.4f\n', j, w);
  fprintf('                      Significance level : %8.4f\n\n', pw);


g01dd example results

For sample number 1 value of W statistic =   0.9417
                      Significance level :   0.2579

For sample number 2 value of W statistic =   0.8857
                      Significance level :   0.0225

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015