# nestedRanksTest_Z: Calculates Z-score from ranks. In nestedRanksTest: Mann-Whitney-Wilcoxon Test for Nested Ranks

## Description

`nestedRanksTest_Z` is used by `nestedRanksTest` to calculate the Z-score for the ranks of responses `y` divided into two treatment levels.

## Usage

 `1` ```nestedRanksTest_Z(y, n1, n2) ```

## Arguments

 `y` Values to be ranked for the test. Its length must be equal to the sum of `n1` and `n2`. `n1` The first `n1` values in `y` belong to the first treatment level. `n2` The final `n2` values in `y` belong to the second treatment level.

## Details

Values across both treatments are ranked using the base R function `rank` with `ties.method = "average"`, which assigns tied values their average rank. The Mann-Whitney-Wilcoxon test statistic is computed from these ranks. Because the value of the statistic is sample-size dependent (between `-n1*n2` and `n1*n2`), it is scaled to be `[-1,+1]` by dividing by `n1*n2`.

The bottleneck for bootstrapping is calculation of ranks, so the most straightforward way to speed up `nestedRanksTest` would come from speeding up `rank`. Because of the checks performed prior to calling this routine, it should be sufficient to use a stripped-down function that simply does the equivalent of making an `.Internal` call, which is not allowed within package code. As of this writing, this is sufficient:

`rank_new <- function (x) .Internal(rank(x, length(x), "average"))`

For the example data this is 8-9 times faster than the base R `rank`, because it avoids error-checking overhead. For longer vectors, the advantage decreases such that at 10000 elements it is 20-30%.

## Value

The calculated Z-score

`nestedRanksTest`, `wilcox.test`