Skip to contents

Abstract class for randomization-based inference.

Randomization p-values count the observed allocation as one of the \(B\) finite reference draws, \((1 + b)/(1 + B)\) per tail (two-sided: the smaller tail doubled, capped at 1), so they are exact and never 0; the realized allocation is never also one of the reference draws; see compute_rand_two_sided_pval().

For survival outcomes with left- or interval-censoring, a nonzero delta shifts the censoring bounds y_L and y_R of the treated exactly as it shifts exact times: multiplicatively by \(e^{\delta}\) under the default "log" transform, so bounds of 0 and Inf stay where they are.

The p-value always comes from randomization draws of the assignment vector \(w\) from the design, for every response type including incidence on matched-pair and Bernoulli designs. (Before EDI 1.0.5 such incidence designs instead returned the Zhang exact combined test, which does not use the draws and was markedly more conservative; it is still available explicitly through the type = "Zhang" argument of classes that offer it.) Non-finite reference statistics are dropped (for any na.rm) and \(B\) above is the number of finite ones. At least \(\max(10, \lfloor r/10 \rfloor)\) finite reference statistics are required (10% of the draws, and never fewer than 10), otherwise NA is returned. A nonzero delta is not supported for incidence responses. delta is a shift of the responses, not a value of the estimand: delta = 0 is the sharp null of no effect for every class, including ratio estimands (risk ratio 1, log risk ratio 0).

References

Monte Carlo randomization p-value (v1.2.0-TODO-207; see [PhipsonSmyth2010] in REFERENCES.md): Phipson, B. and Smyth, G. K. (2010). Permutation P-values should never be zero: calculating exact P-values when permutations are randomly drawn. Statistical Applications in Genetics and Molecular Biology, 9(1), Article 39, doi:10.2202/1544-6115.1585 ; also arXiv:1603.05766. The same rule is implemented by permp() in the CRAN package statmod (https://CRAN.R-project.org/package=statmod; help page https://search.r-project.org/CRAN/refmans/statmod/html/permp.html). Hemerik, J. and Goeman, J. (2017). Exact testing with random permutations. TEST, doi:10.1007/s11749-017-0571-1 ; arXiv:1411.7565. Howes, M. (2023). Markov chain Monte Carlo significance tests. arXiv:2310.04924. Fischer, L. and Ramdas, A. (2025). Sequential Monte-Carlo testing by betting. Journal of the Royal Statistical Society Series B, qkaf014, doi:10.1093/jrsssb/qkaf014 ; arXiv:2401.07365. Zeng, Z., Adjaho, C., Bucarey, A., Qin, C., Zhang, R., Hoban, P., Johari, R. and Wager, S. (2026). Sequentially-rerandomized switchback experiments. arXiv:2604.02489. Coppock, A. Randomization inference with ri2 (vignette of the CRAN package ri2, https://CRAN.R-project.org/package=ri2; https://alexandercoppock.com/ri2/articles/ri2_vignette.html).

Super classes

Inference -> InferenceResamplingCore -> InferenceRand

Methods

+ inherited public methods from Inference


InferenceRand$approximate_randomization_distribution_beta_hat_T()

Computes the randomization distribution of the treatment effect estimate under the sharp null.

Usage

InferenceRand$approximate_randomization_distribution_beta_hat_T(
  r = 501,
  delta = 0,
  transform_responses = "none",
  show_progress = TRUE,
  permutations = NULL,
  debug = FALSE,
  zero_one_logit_clamp = .Machine$double.eps
)

Arguments

r

Number of randomization vectors. Default 501.

delta

The null difference. Default 0.

transform_responses

Type of transformation. Default "none". Allowed values: "none" (the default), "log" and "logit". Here "none" is an additive shift for every response type (unlike compute_rand_two_sided_pval(), which first resolves "none" to "logit" for proportion and "log" for count and survival responses). "log" multiplies the treated responses by \(e^\delta\) (counts are then rounded); "logit" adds \(\delta\) on the logit scale (see zero_one_logit_clamp). The value is not validated: the shared shift code treats any other string as an additive shift. With delta = 0 no response is shifted, so the value does not change the result.

show_progress

Show progress bar. Default TRUE.

permutations

Pre-computed permutations. Default NULL. NULL (the default) draws them from the design. Otherwise a list with element w_mat, a numeric \(n \times r\) matrix of 0/1 assignment vectors (one column per draw, rows in subject order, 1 = treatment) with at least r columns, and optionally m_mat, a matching \(n \times r\) matrix of block/match ids per draw (NULL otherwise). A bare matrix is not accepted. The draws are not validated (row count, 0/1 coding and column count are not checked), so they must come from the design's own randomization mechanism for the test to be valid.

debug

If TRUE, return a list with the distribution values and per-iteration diagnostics including error messages, warning messages, counts of each, and summary proportions for iterations with errors, warnings, and illegal (non-finite) values. Runs serially. Default FALSE.

zero_one_logit_clamp

Clamping distance from the 0/1 boundaries used by the "logit" response transform: responses are clamped to [zero_one_logit_clamp, 1 - zero_one_logit_clamp] before taking the logit, so exact 0 and 1 values stay finite. Default .Machine$double.eps.

Returns

When debug = FALSE (default), a numeric vector of length r. When debug = TRUE, a list with: values, errors (list of character vectors, one per iteration), warnings (list of character vectors, one per iteration), num_errors, num_warnings, prop_iterations_with_errors, prop_iterations_with_warnings, and prop_illegal_values.


InferenceRand$supports_rand_pval_for_incidence()

Whether compute_rand_two_sided_pval() is actually usable on this instance right now – FALSE exactly when it would stop(): an incidence-response instance with no custom randomization statistic and a design not eligible for design-randomization-based incidence inference (see private$should_use_design_randomization_for_incidence()). TRUE for every other case, including every non-incidence response type. Public, self-contained (only reads already-set instance state, no side effects), so InferenceSuite can check this before attempting the sentinel instead of relying on the stop() being silently swallowed into a pval = NA "ok" row – the single source of truth for both this check and compute_rand_two_sided_pval()'s own guard, so the two can never drift apart.

Usage

InferenceRand$supports_rand_pval_for_incidence()

Returns

A single logical.


InferenceRand$compute_rand_two_sided_pval()

Computes a randomization-based p-value.

Usage

InferenceRand$compute_rand_two_sided_pval(
  r = 501,
  delta = 0,
  transform_responses = "none",
  na.rm = TRUE,
  show_progress = TRUE,
  permutations = NULL,
  zero_one_logit_clamp = .Machine$double.eps,
  alternative = "two.sided"
)

Arguments

r

Number of randomization vectors.

delta

Null difference.

transform_responses

Transformation. Allowed values: "none" (the default), "log" and "logit". "none" resolves by response type: an additive shift for continuous responses, "logit" for proportion and "log" for count and survival. "log" multiplies the treated responses by \(e^\delta\) (counts are then rounded); "logit" adds \(\delta\) on the logit scale (see zero_one_logit_clamp). The value is not validated: the shared shift code treats any other string as an additive shift. With delta = 0 no response is shifted, so the value does not change the result.

na.rm

Drop non-finite draws. Non-finite reference statistics (draws whose statistic is NA, NaN or infinite) are always dropped silently and the p-value is computed over the finite draws only; na.rm = FALSE does not change this (it must still be a flag). See Details for the minimum number of finite draws required.

show_progress

Show progress.

permutations

Pre-computed permutations. NULL (the default) draws them from the design. Otherwise a list with element w_mat, a numeric \(n \times r\) matrix of 0/1 assignment vectors (one column per draw, rows in subject order, 1 = treatment) with at least r columns, and optionally m_mat, a matching \(n \times r\) matrix of block/match ids per draw (NULL otherwise). A bare matrix is not accepted. The draws are not validated (row count, 0/1 coding and column count are not checked), so they must come from the design's own randomization mechanism for the test to be valid. Random draws are used as they are. A full enumeration of the assignment space contains the realized allocation, which the rule below already counts once, so one column identical to the realized assignment is removed before counting (the p-value is then the exact enumeration p-value, \(b/N\) over all \(N\) allocations). That happens when the list has enumeration = TRUE (an error if the realized allocation is not among the first r columns), or, with enumeration absent, when the first r columns are pairwise distinct, all have the realized number of treated units, number choose(n, n_T) and m_mat is NULL (the whole complete-randomization support); enumeration = FALSE turns it off.

zero_one_logit_clamp

Clamping distance from the 0/1 boundaries used by the "logit" response transform: responses are clamped to [zero_one_logit_clamp, 1 - zero_one_logit_clamp] before taking the logit, so exact 0 and 1 values stay finite. Default .Machine$double.eps.

alternative

One of "two.sided" (default), "greater" or "less". Let \(B\) be the number of finite reference statistics \(T_0\), \(b_{\ge}\) the number with \(T_0 \ge t\) and \(b_{\le}\) the number with \(T_0 \le t\). The observed allocation counts as one draw, so each tail is \((1 + b)/(1 + B)\). The default doubles the smaller tail and caps at 1, \(\min(1, 2(1 + \min(b_{\ge}, b_{\le}))/(1 + B))\), which assumes a signed, roughly symmetric statistic; its smallest value is \(2/(B + 1)\). For a non-negative, one-tailed-by-construction statistic (a squared difference, a chi-square or omnibus statistic, an absolute value) the doubled smaller tail is wrong – it counts the uninformative lower tail as evidence – so pass "greater", which returns the upper-tail p-value \((1 + b_{\ge})/(1 + B)\) (no doubling, smallest value \(1/(B + 1)\)); "less" returns \((1 + b_{\le})/(1 + B)\). This is the exact Monte Carlo p-value of Phipson and Smyth (2010): with \(B\) independent draws from the design the test rejects a true sharp null with probability at most \(\alpha\), and the p-value is never 0. (Before EDI 1.0.5 the rule was \(b/B\) with a floor of \(2/B\), whose size was above \(\alpha\) by about \(1/B\); v1.2.0-TODO-207.) A tie with \(t\) (within a relative tolerance) counts in both tails. The realized allocation is never counted twice: when the draws come from the design's own seed (no inference seed, or the same value), the first draw of that stream, which a seeded fixed design makes equal to its realized allocation, is skipped, and a supplied full enumeration has its copy removed (see permutations; v1.2.0-TODO-263). A later random draw that equals the realized allocation by chance is kept, as the rule requires.

Returns

Randomization p-value.


InferenceRand$clone()

The objects of this class are cloneable with this method.

Usage

InferenceRand$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.