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_nonpar_test_friedman (g08ae)


    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example


nag_nonpar_test_friedman (g08ae) performs the Friedman two-way analysis of variance by ranks on k related samples of size n.


[fr, p, ifail] = g08ae(x, 'k', k, 'n', n)
[fr, p, ifail] = nag_nonpar_test_friedman(x, 'k', k, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
At Mark 22: k was made optional


The Friedman test investigates the score differences between k matched samples of size n, the scores in the ith sample being denoted by
(Thus the sample scores may be regarded as a two-way table with k rows and n columns.) The hypothesis under test, H0, often called the null hypothesis, is that the samples come from the same population, and this is to be tested against the alternative hypothesis H1 that they come from different populations.
The test is based on the observed distribution of score rankings between the matched observations in different samples.
The test proceeds as follows
(a) The scores in each column are ranked, rij denoting the rank within column j of the observation in row i. Average ranks are assigned to tied scores.
(b) The ranks are summed over each row to give rank sums ti=j=1nrij, for i=1,2,,k.
(c) The Friedman test statistic F is computed, where
F=12nkk+1 i=1kti-12nk+12.  
nag_nonpar_test_friedman (g08ae) returns the value of F, and also an approximation, p, to the significance of this value. (F approximately follows a χk-12 distribution, so large values of F imply rejection of H0). H0 is rejected by a test of chosen size α if p<α. The approximation p is acceptable unless k=4 and n<5, or k=3 and n<10, or k=2 and n<20; for k=3​ or ​4, tables should be consulted (e.g., Siegel (1956)); for k=2 the Sign test (see nag_nonpar_test_sign (g08aa)) or Wilcoxon test (see nag_nonpar_test_wilcoxon (g08ag)) is in any case more appropriate.


Siegel S (1956) Non-parametric Statistics for the Behavioral Sciences McGraw–Hill


Compulsory Input Parameters

1:     xldxn – double array
ldx, the first dimension of the array, must satisfy the constraint ldxk.
xij must be set to the value, xij, of observation j in sample i, for i=1,2,,k and j=1,2,,n.

Optional Input Parameters

1:     k int64int32nag_int scalar
Default: the first dimension of the array x.
k, the number of samples.
Constraint: k2.
2:     n int64int32nag_int scalar
Default: the second dimension of the array x.
n, the size of each sample.
Constraint: n1.

Output Parameters

1:     fr – double scalar
The value of the Friedman test statistic, F.
2:     p – double scalar
The approximate significance, p, of the Friedman test statistic.
3:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:
On entry,n<1.
On entry,ldx<k.
On entry,k1.
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.


For estimates of the accuracy of the significance p, see nag_stat_prob_chisq (g01ec). The χ2 approximation is acceptable unless k=4 and n<5, or k=3 and n<10, or k=2 and n<20.

Further Comments

The time taken by nag_nonpar_test_friedman (g08ae) is approximately proportional to the product nk.
If k=2, the Sign test (see nag_nonpar_test_sign (g08aa)) or Wilcoxon test (see nag_nonpar_test_wilcoxon (g08ag)) is more appropriate.


This example is taken from page 169 of Siegel (1956). The data relates to training scores of three matched samples of 18 rats, trained under three different patterns of reinforcement.
function g08ae_example

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

x = [1, 2, 1, 1, 3, 2, 3, 1, 3, 3, 2, 2, 3, 2, 2.5, 3, 3, 2;
     3, 3, 3, 2, 1, 3, 2, 3, 1, 1, 3, 3, 2, 3, 2.5, 2, 2, 3;
     2, 1, 2, 3, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1,   1, 1, 1];

fprintf('Friedman test\n\n');
mtitle = 'Data values';
matrix = 'General';
diag   = ' ';
[ifail] = x04ca( ...
                 matrix, diag, x, mtitle);

[fr, p, ifail] = g08ae(x);

fprintf('\nTest statistic       %6.3f\n', fr);
fprintf('Degrees of freedom   %6d\n', size(x,1)-1);
fprintf('Significance         %6.3f\n', p);

g08ae example results

Friedman test

 Data values
             1          2          3          4          5          6          7
 1      1.0000     2.0000     1.0000     1.0000     3.0000     2.0000     3.0000
 2      3.0000     3.0000     3.0000     2.0000     1.0000     3.0000     2.0000
 3      2.0000     1.0000     2.0000     3.0000     2.0000     1.0000     1.0000

             8          9         10         11         12         13         14
 1      1.0000     3.0000     3.0000     2.0000     2.0000     3.0000     2.0000
 2      3.0000     1.0000     1.0000     3.0000     3.0000     2.0000     3.0000
 3      2.0000     2.0000     2.0000     1.0000     1.0000     1.0000     1.0000

            15         16         17         18
 1      2.5000     3.0000     3.0000     2.0000
 2      2.5000     2.0000     2.0000     3.0000
 3      1.0000     1.0000     1.0000     1.0000

Test statistic        8.583
Degrees of freedom        2
Significance          0.014

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