# Equivalence of formulations for Friedman Test?

I'm trying to implement a Friedman test, and came across two sites giving methods for its implementation, namely, Wikipedia (perma-linking for posterity) and the NIST. At first glance, the general setup of the methods listed appears the same -- computing the ranks of each block-by-block row and combining them with a sort of distance statistic.

Upon closer inspection, though, I am not seeing how to reconcile the two given methods.

# Wikipedia

$X$ : data matrix ($n\times k$), $R$ : corresponding within-row rank matrix, then $\bar{r}_j$, vector of column means and $\bar{r}$, overall matrix mean given by:

$$\bar{r}_j = \frac1n \sum_{i = 1}^n r_{i,j}$$

$$\bar{r} = \frac1{nk} \sum_{i = 1}^n \sum_{j = 1}^k r_{i,j}$$

Then the test statistic $Q$ is given by $Q = \frac{SS_t}{SS_e}$, where:

$$SS_t = n \sum_{j = 1}^n \left(\bar{r}_j -\bar{r}\right)^2$$

$$SS_e = \frac1{n(k-1)}\sum_{i=1}^n\sum_{j=1}^k\left(r_{i,j}-\bar{r}\right)^2$$

# NIST

$R$ is again the matrix of ranks. $R_j$ is the vector of column sums.

The test statistic is $T_1$ given by:

$$\frac{12}{nk(k+1)}\sum_{j=1}^k\left(R_j - \frac{n(k+1)}2\right)^2$$

(and an adjustment is given for the case of ties).

These formulas look wildly different, in particular in the fraction on the outside of the sum: $n^2(k-1)$ for the Wikipedia approach, but $\frac{12}{nk(k+1)}$ for the NIST approach. The latter appears closer (at a glance) to the formulas used in the original paper of Friedman, so I'm inclined to trust that, but there do appear to be some creeping typos on that page.

The NIST formula also looks similar to that used in the R implementation (stats:::friedman.test.default):

STATISTIC <- ((12 * sum((colSums(r) - n * (k + 1)/2)^2))/(n *
k * (k + 1) - (sum(unlist(lapply(TIES, function(u) {
u^3 - u


Is the Wikipedia page wrong? Or is there an equivalence I'm just missing?

[Note that the R help gives Hollander and Wolfe [1] as a reference so that would be one place to look for more details.]

Each set of block-ranks contains the numbers $1$ to $k$, so the average rank, $\bar{r}$ in each block will be $(k+1)/2$ (ties within a block won't change that average either, but I will work on a "no ties" basis, since it will affect later calculations). Consequently, the average rank across all blocks will also be $(k+1)/2$.

Similarly, $SS_e$, is purely a function of $n$ and $k$. For a particular block, $i$,

$$\sum_{j=1}^k\left(r_{i,j}-\bar{r}\right)^2=k(k-1)(k+1)/12$$

Hence the double sum in $SS_e$ is $nk(k-1)(k+1)/12$ and so $SS_e$ itself is $k(k+1)/12$

What remains then is to show that $\frac{1}{n}\sum_{j=1}^k\left(R_j - \frac{n(k+1)}2\right)^2=n \sum_{j = 1}^n \left(\bar{r}_j -\frac{(k+1)}{2}\right)^2$.

Taking the RHS

\begin{eqnarray} n\sum_{j = 1}^n \left(\bar{r}_j -\frac{(k+1)}{2}\right)^2&=&n\sum_{j = 1}^n \left(\frac{1}{n}R_j -\frac{1}{n}\frac{n(k+1)}{2}\right)^2\\ &=&n(\frac{1}{n})^2\sum_{j = 1}^n \left(R_j -\frac{n(k+1)}{2}\right)^2\\ &=&\frac{1}{n}\sum_{j = 1}^n \left(R_j -\frac{n(k+1)}{2}\right)^2\end{eqnarray}

So when there are no ties the two formulas are algebraically identical.

[1] Myles Hollander and Douglas A. Wolfe (1973),
Nonparametric Statistical Methods.
New York: John Wiley & Sons. Pages 139-146.

• so the Wikipedia formula is robust to ties? or no? the NIST page gives a separate formula for ties, wiki doesnt. Commented Mar 17, 2016 at 2:12
• @Michael Note that both the numerator and denominator of Q will be impacted by ties and in the same direction, so that will reduce the effect of ties on the distribution, but that's not quite the same as saying either this statistic or the NIST one for ties are really robust to them. Both should correctly account for the effect of ties on the variance of $SS_t$, but that's not the same thing as having the distribution be chi-square. Ultimately the problem is that ties make the statistic "more discrete" (in a particular sense) and the chi-square approximation can be poor when ties are heavy. Commented Mar 17, 2016 at 2:21
• If you have heavy ties it makes me wonder about what kind of data you have (are they counts, for example?). If your data are heavily tied you can always go to an exact permutation test (whether on the ranks or on the original data) Commented Mar 17, 2016 at 2:23
• thanks for the insight, still trying to wrap my head around this test. in fact the data are sort of counts -- there are 7 groups but they can only take values 0, 1 or 2, so ties are ubiquitous. Commented Mar 17, 2016 at 2:53
• @Michael You may be better with a different analysis (or you might keep on with this approach but use a permutation test rather than an asymptotic approximation). What are the 0, 1 and 2? "sort of counts" is a bit unclear. Commented Mar 17, 2016 at 4:56