PDF version (NAG web site
, 64-bit version, 64-bit version)
NAG Toolbox: nag_stat_test_shapiro_wilk (g01dd)
Purpose
nag_stat_test_shapiro_wilk (g01dd) calculates Shapiro and Wilk's statistic and its significance level for testing Normality.
Syntax
[
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 |
Description
nag_stat_test_shapiro_wilk (g01dd) calculates Shapiro and Wilk's
statistic and its significance level for any sample size between
and
. 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
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
statistic defined as:
where
is the sample mean and
, for
, are a set of ‘weights’ whose values depend only on the sample size
.
On exit, the values of , for , are only of interest should you wish to call the function again to calculate 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
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 test for normality Appl. Statist. 31 176–180
Royston J P (1986) A remark on AS 181: the test for normality Appl. Statist. 35 232–234
Royston J P (1992) Approximating the Shapiro–Wilk's 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 test for normality Appl. Statist. 44(4) 547–551
Parameters
Compulsory Input Parameters
- 1:
– double array
-
The ordered sample values,
, for .
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the arrays
x,
a. (An error is raised if these dimensions are not equal.)
, the sample size.
Constraint:
.
- 2:
– double array
-
If supplied,
a must contain the
weights as calculated in a previous call to
nag_stat_test_shapiro_wilk (g01dd), otherwise
a need not be set.
Output Parameters
- 1:
– double scalar
-
The value of the statistic, .
- 2:
– double scalar
-
The significance level of .
- 3:
– int64int32nag_int scalar
unless the function detects an error (see
Error Indicators and Warnings).
- 4:
– double array
-
The weights required to calculate .
Error Indicators and Warnings
Errors or warnings detected by the function:
-
-
-
-
-
-
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.
Accuracy
There may be a loss of significant figures for large .
Further Comments
The time taken by nag_stat_test_shapiro_wilk (g01dd) depends roughly linearly on the value of .
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
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
,
where
is the rounding width.
can be compared with a standard Normal distribution, but a further approximation is given by
Royston (1986).
If
, a value for
w and
pw is returned, but its accuracy may not be acceptable. See
References for more details.
Example
This example tests the following two samples (each of size ) for Normality.
Sample
Number |
Data |
1 |
, , , , , , , , , , , , , , , , , , , |
2 |
, , , , , , , , , , , , , , , , , , , |
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.
Open in the MATLAB editor:
g01dd_example
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);
for j=1:size(x,2)
[x, ifail] = m01ca( ...
x, n1, 'Ascending');
if j==1
[w, pw, ifail, a] = g01dd( ...
x(:,j));
else
[w, pw, ifail, a] = g01dd( ...
x(:,j), 'a', a);
end
fprintf('For sample number %d value of W statistic = %8.4f\n', j, w);
fprintf(' Significance level : %8.4f\n\n', pw);
end
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)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015