Skip to contents

Runs a randomization test (and, via compute_rand_confidence_interval(), a randomization confidence interval) for an arbitrary user-supplied statistic, without writing an Inference subclass. The statistic function is called once per permutation as fn(y, w, dead) – plain numeric/integer vectors: the current (possibly permuted) response, treatment assignment, and event indicator. It must return one scalar (a value of any other length is an error). For maximum speed, supply custom_randomization_statistic_cpp instead: C++ source defining a function of (NumericVector y, IntegerVector w) or (NumericVector y, IntegerVector w, IntegerVector dead) returning a scalar double, using the same convention as DesignFixedOptimal's custom_objective. The source is compiled with Rcpp::cppFunction(), which adds its own // [[Rcpp::export]] tag in front of the first function in the source, so the source must contain exactly one function, the statistic: no helper functions before it (not even a forward declaration or an inline helper; the first one would be taken as the statistic and fail the argument check) and no // [[Rcpp::export]] line of your own (the function would be exported twice and the compile fails). Put helper logic in a lambda inside the statistic.

Only a randomization test and randomization confidence interval are available – there is no package point-estimator, Wald path, or bootstrap machinery on this class, so no other action needs disabling.

The confidence interval inverts the randomization test over the shift \(\delta\) (see below for its scale by response type), whatever the statistic's own units (a t statistic, or -10 * mean difference, give the same interval). The search is centred on the observed difference in mean response on the shift scale, not on the statistic's observed value. The statistic must respond to the shift (be monotone in \(\delta\)); if the test rejects every \(\delta\) tried the CI errors instead of returning a rejected band.

Draws on which the statistic returns NA, NaN or an infinite value are dropped silently (whatever na.rm is) and the p-value is computed over the finite draws; at least \(\max(10, \lfloor r/10 \rfloor)\) finite draws are required, otherwise the p-value is NA. See InferenceRand$compute_rand_two_sided_pval().

The p-value rule. With \(B\) finite draws, \(b_{\ge}\) of them at least and \(b_{\le}\) at most the observed statistic, the observed allocation counts as one draw: the default two-sided p-value doubles the smaller tail, \(\min(1, 2(1 + \min(b_{\ge}, b_{\le}))/(1 + B))\), smallest value \(2/(B + 1)\); alternative = "greater" gives \((1 + b_{\ge})/(1 + B)\), smallest \(1/(B + 1)\) (use it for a statistic that is already an absolute value or a squared or omnibus measure). The p-value is exact (a true sharp null is rejected with probability at most \(\alpha\)) and never 0 (Phipson and Smyth 2010). The realized allocation is never also one of the \(B\) draws: a seeded design's replay of it as the first draw is skipped, and a supplied full enumeration has its copy removed. The randomization CI inverts the two-sided rule.

The scale of delta by response type. A nonzero delta tests the sharp null that treatment moves every subject's response by \(\delta\) on the transform_responses scale. The shift is removed from the observed treated units and re-imposed on each draw's treated units on that same scale, and the statistic always sees responses on their own scale. Under the default transform_responses = "none":

  • continuous: additive, \(y_i(1) = y_i(0) + \delta\); compute_rand_confidence_interval() is an interval for the additive effect.

  • survival: \(\delta\) is a log time ratio, \(y_i(1) = y_i(0) e^\delta\) (censoring times are scaled alike, event indicators unchanged); compute_rand_confidence_interval() is an interval for \(\delta\), so exponentiate its bounds for an interval for the time ratio \(e^\delta\).

  • count: \(\delta\) is a log rate ratio, the treated counts multiplied by \(e^\delta\) and rounded to integers.

  • proportion: \(\delta\) is a shift on the logit scale (responses clamped by zero_one_logit_clamp).

  • incidence: a nonzero delta is refused.

transform_responses = "log" or "logit" selects the multiplicative or logit-scale shift explicitly (on continuous responses too, which then must be positive, or in (0, 1), for the null to make sense). The randomization CI is refused for incidence, count and proportion responses (its bounds would not be on the scale of this class's raw-scale estimate); on those types use the p-value, at delta = 0 or at a chosen delta.

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 -> InferenceCustomRand -> InferenceRandCustom

Methods

+ inherited public methods from InferenceCustomRand
+ inherited public methods from Inference


InferenceRandCustom$new()

Initialize.

Usage

InferenceRandCustom$new(
  des_obj,
  custom_randomization_statistic_function = NULL,
  custom_randomization_statistic_cpp = NULL,
  verbose = FALSE
)

Arguments

des_obj

See class description.

custom_randomization_statistic_function

See class description.

custom_randomization_statistic_cpp

See class description.

verbose

See class description.


InferenceRandCustom$fit()

Calls the user-supplied statistic on the observed data.

Usage

InferenceRandCustom$fit(estimate_only = FALSE)

Arguments

estimate_only

Unused; present for the `fit()` contract.


InferenceRandCustom$clone()

The objects of this class are cloneable with this method.

Usage

InferenceRandCustom$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.