Learn R Programming

rankdifferencetest (version 2025.12.4)

srt_ranks: Ranks for the signed-rank test.

Description

Computes ranks of the absolute differences and the signed-rank test statistic \(W^+\). The returned list is designed to be reused by higher-level signed-rank functions.

Usage

srt_ranks(x, call, warn_wd = FALSE)

Value

list

Arguments

x

(named list)
A named list which contains a vector of paired differences named "diffs". Typically the object returned by srt_data().

call

(call or named list)
A call or named list which contains the arguments from the parent function srt() or rdt(). The minimum list of arguments required for srt_ranks() are:

  • mu

    • Scalar numeric (-Inf, Inf). Under the null hypothesis, diffs is assumed to be symmetric around mu.

  • zero_method

    • Scalar character c("wilcoxon", "pratt"). String for zero handling.

  • digits_rank

    • Scalar numeric (0, Inf]. Controls ranking precision.

Next in line, srt_method() needs:

  • [[1L]]

    • as.name() of calling function

    • One of quote(srt), quote(srt2), quote(rdt), or quote(rdt2)

  • distribution

    • Scalar character: c("auto", "exact", "asymptotic")

    • The method used to calculate the p-value.

warn_wd

(Scalar logical: c(FALSE, TRUE))
Used for

  1. Midpoint algorithm 'W+(d)' in confidence interval test inversion.

  2. Recalculating ranks after removing mu-shift. If TRUE return a warning and exit with value wplus = 0.

Details

Consider a numeric vector of paired differences \(x = (x_1, \dots, x_n)\). After removing NA values, let \(\mathcal{I}_0 = \{i : x_i = 0\}\) and \(\mathcal{I}_{\neq 0} = \{i : x_i \neq 0\}\).

Zero handling

For the Wilcoxon method (zero_method = "wilcoxon"), zeros are removed prior to ranking, so the ranking is performed on \(\{x_i : i \in \mathcal{I}_{\neq 0}\}\) only.

For the Pratt method (zero_method = "pratt"), zeros are retained when computing ranks, but their corresponding ranks do not contribute to the signed-rank sum \(W^+\).

Ranking

Ranks are assigned to \(|x_i|\) using average-tie handling. The argument digits_rank controls rounding for ranking only. Ranks are computed from \(|\mathrm{signif}(x_i, \text{digits\_rank})|\) when digits_rank is finite, and from \(|x_i|\) otherwise. This rounding may induce ties, which changes both the values of the averaged ranks and the variance of the statistic in asymptotic procedures.

The next function in the 'srt pipeline', srt_method(), calculates the number of ties among the ranks (n_ties).

Wilcoxon signed-Rank statistic

Let \(r_i\) denote the absolute-value rank of the \(i\)-th observation after applying the chosen zero handling and ranking precision. The sum of positive ranks is

$$W^+ = \sum_{i : x_i > 0} r_i,$$

which is the canonical Wilcoxon signed-rank statistic used for both exact and asymptotic inference.

Examples

Run this code
library(rankdifferencetest)

# Synthetic paired differences with zeros and ties
set.seed(1)
diffs <- c(rnorm(8, mean = 0.3), 0, 0, 0, round(rnorm(8, mean = -0.2), 1))
x <- list(diffs = diffs)

# Wilcoxon zero method: zeros dropped before ranking
call <- list(mu = 0, zero_method = "wilcoxon", digits_rank = Inf)
cw <- rankdifferencetest:::srt_ranks(x, call)
cw$ranks
cw$wplus

# Pratt zero method: zeros retained for ranking
call <- list(mu = 0, zero_method = "pratt", digits_rank = Inf)
cp <- rankdifferencetest:::srt_ranks(x, call)
cp$wplus
cp$n_signed

# Induce ties via ranking precision
call <- list(mu = 0, zero_method = "wilcoxon", digits_rank = 1)
ctied <- rankdifferencetest:::srt_ranks(x, call)
ctied$ranks

Run the code above in your browser using DataLab