friedmansTest

Performs Friedman’s test for a randomized complete block design.

Synopsis

friedmansTest (y)

Required Arguments

float y[[]] (Input)
Array of size nBlocks × nTreatments containing the observations. The first nTreatments positions of y[] contain the observations on treatments 1, 2, …, nTreatments in the first block. The second nTreatments positions contain the observations in the second block, etc., and so on.

Return Value

The Chi-squared approximation of the asymptotic p-value for Friedman’s two-sided test statistic.

Optional Arguments

fuzz, float (Input)

Constant used to determine ties. In the ordered observations, if |y[i]–y[i + 1]| is less than or equal to fuzz, then y[i] and y[i + 1] are said to be tied.

Default value is 0.0.

alpha, float (Input)

Critical level for multiple comparisons. alpha should be between 0 and 1 exclusive.

Default value is 0.05.

stat (Output)
An array of length 6 containing the Friedman statistics. Probabilities reported are computed under the appropriate null hypothesis.
i stat[i]
0 Friedman two-sided test statistic.
1 Approximate F value for stat[0].
2 Page test statistic for testing the ordered alternative that the median of treatment i is less than or equal to the median of treatment i + 1, with strict inequality holding for some i.
3 Asymptotic p-value for stat[0]. Chi-squared approximation.
4 Asymptotic p-value for stat[1]. F approximation.
5 Asymptotic p-value for stat[2]. Normal approximation.
sumRank (Output)
An array of length nTreatments containing the sum of the ranks of each treatment.
difference (Output)
Minimum absolute difference in two elements of sumRank to infer at the alpha level of significance that the medians of the corresponding treatments are different.

Description

Function friedmansTest may be used to test the hypothesis of equality of treatment effects within each block in a randomized block design. No missing values are allowed. Ties are handled by using the average ranks. The test statistic is the nonparametric analogue of an analysis of variance F test statistic.

The test proceeds by first ranking the observations within each block. Let A denote the sum of the squared ranks, i.e., let

\[A = \sum_{i=1}^{k} \sum_{j=1}^{b} \mathrm{Rank}\left(Y_{ij}\right)^2\]

where Rank(\(Y_{ij}\)) is the rank of the i-th observation within the j-th block, b = nBlocks is the number of blocks, and k = nTreatments is the number of treatments. Let

\[B = \frac{1}{b} \sum_{i=1}^{k} R_i^2\]

where

\[R_i = \sum_{j=1}^{b} \mathrm{Rank}\left(Y_{ij}\right)\]

The Friedman test statistic (stat[0]) is given by:

\[T = \frac{(k-1)\left(bB - b^2k(k+1)^2/4\right)}{A-bk(k+1)^2/4}\]

that, under the null hypothesis, has an approximate chi-squared distribution with k - 1 degrees of freedom. The asymptotic probability of obtaining a larger chi-squared random variable is returned in stat[3].

If the F distribution is used in place of the chi-squared distribution, then the usual oneway analysis of variance F-statistic computed on the ranks is used. This statistic, reported in stat[1], is given by

\[F = \frac{(b-1)T}{b(k-1)-T}\]

and asymptotically follows an F distribution with \((k-1)\) and \((b-1)(k-1)\) degrees of freedom under the null hypothesis. stat[4] is the asymptotic probability of obtaining a larger F random variable. (If \(A=B\), stat[0] and stat[1] are set to machine infinity, and the significance levels are reported as \(k!(k!)^b\), unless this computation would cause underflow, in which case the significance levels are reported as zero.) Iman and Davenport (1980) discuss the relative advantages of the chi-squared and F approximations. In general, the F approximation is considered best.

The Friedman T statistic is related both to the Kendall coefficient of concordance and to the Spearman rank correlation coefficient. See Conover (1980) for a discussion of the relationships.

If, at the α = alpha level of significance, the Friedman test results in rejection of the null hypothesis, then an asymptotic test that treatments i and j are different is given by: reject \(H_0\) if \(|R_i-R_j|\) > D, where

\[D = t_{1-\alpha/2} (2b(A-B))/((b-1)(k-1))^{1/2}\]

where t has \((b-1)(k-1)\) degrees of freedom. Page’s statistic (stat[2]) is used to test the same null hypothesis as the Friedman test but is sensitive to a monotonic increasing alternative. The Page test statistic is given by

\[Q = \sum_{i=1}^{k} jR_i\]

It is largest (and thus most likely to reject) when the \(R_i\) are monotonically increasing.

Assumptions

The assumptions in the Friedman test are as follows:

  1. The k-vectors of responses within each of the b blocks are mutually independent (i.e., the results within one block have no effect on the results within another block).
  2. Within each block, the observations may be ranked.

The hypothesis tested is that each ranking of the random variables within each block is equally likely. The alternative is that at least one of the treatments tends to have larger values than one or more of the other treatments. The Friedman test is a test for the equality of treatment means or medians.

Example

The following example is taken from Bradley (1968), page 127, and tests the hypothesis that 4 drugs have the same effects upon a person’s visual acuity.

Five subjects were used.

from __future__ import print_function
from numpy import *
from pyimsl.stat.friedmansTest import friedmansTest

y = array([[.39, .55, .33, .41],
           [.21, .28, .19, .16],
           [.73, .69, .64, .62],
           [.41, .57, .28, .35],
           [.65, .57, .53, .60]])
fuzz = .001
alpha = .05
sum_rank = []
stat = []
difference = []

pvalue = friedmansTest(y, sumRank=sum_rank,
                       stat=stat,
                       difference=difference)

print("\np value for Friedman's T = %f" % pvalue)
print("Friedman's T = ............  %4.2f" % stat[0])
print("Friedman's F = ............  %4.2f" % stat[1])
print("Page Test = ...............%5.2f" % stat[2])
print("Prob Friedman's T = .......  %7.5f" % stat[3])
print("Prob Friedman's F = .......  %7.5f" % stat[4])
print("Prob Page Test = ..........  %7.5f" % stat[5])
print("Sum of Ranks = ............  %4.2f %4.2f %4.2f %4.2f" %
      (sum_rank[0], sum_rank[1], sum_rank[2], sum_rank[3]))
print("Difference = ..............  %7.5f" % difference[0])

Output

p value for Friedman's T = 0.040566
Friedman's T = ............  8.28
Friedman's F = ............  4.93
Page Test = ...............111.00
Prob Friedman's T = .......  0.04057
Prob Friedman's F = .......  0.01859
Prob Page Test = ..........  0.98495
Sum of Ranks = ............  16.00 17.00 7.00 10.00
Difference = ..............  6.65638

The Friedman null hypothesis is rejected at the \(\alpha=.05\) while the Page null hypothesis is not. (A Page test with a monotonic decreasing alternative would be rejected, however.) Using sumRank and difference, one can conclude that treatment 3 is different from treatments 1 and 2, and that treatment 4 is different from treatment 2, all at the \(\alpha=.05\) level of significance.