Skip to contents

A fixed-sample-size DesignFixed that first partitions subjects into blocks (strata) formed from covariates, then randomizes treatment independently within each block at probability prob_T (via the same per-block law as block_ra, drawn for all r draws at once in vectorized R). Blocking on a covariate removes its between-block variation from the treatment-effect comparison (comparisons are always within-block), improving precision relative to unblocked randomization whenever the blocking covariate(s) are prognostic of the outcome. See the section “How inference accounts for the blocks” for which inference routes respect the blocks and which do not. This differs from DesignFixedBlockedCluster, which randomizes whole clusters of subjects together within each block rather than subjects individually.

Block construction. Blocking keys are computed by private$get_strata_keys() (shared across blocking-structure designs): each column in strata_cols contributes a categorical key (continuous columns are discretized into preferred_num_bins_for_continuous_covariate quantile bins), and multiple columns are combined into one composite block key per subject; if strata_cols is NULL, all available covariate columns are used. B_target caps the number of resulting blocks by greedily adding strata_cols in order only while the running block count stays at or below the target (earlier columns take priority); exact_num_blocks = TRUE instead hard-fails if the greedy construction does not land on exactly B_target blocks. equal_block_sizes = TRUE (the default) additionally requires every block to have the same subject count, checked once at construction (via n %% B_target) if n and B_target are both already known, and again when the blocks are first formed (not when the subjects are added): at assign_w_to_all_subjects() when the design draws w, or, when w is supplied with w_precomputed, at the first call that uses the blocks (draw_ws_according_to_design(), a bootstrap or a randomization test or interval). With a supplied w on unequal strata, the estimate, the Wald interval and the jackknife interval still run without error; only those later calls stop. An explicit B_target cannot equalise the blocks of a categorical stratum, whose categories fix the block sizes; some downstream inference classes (InferenceIncidCMH, InferenceIncidExtendedRobins) require equal block sizes unconditionally, regardless of this flag. An explicit m (one block ID per subject) bypasses covariate-derived block construction entirely.

Within-block randomization and bootstrap. Within each block, treatment is assigned independently with block_ra's complete random assignment law (subject to rounding, prob_T of each block's subjects are treated; a fractional N * prob_T is rounded up with probability equal to its fractional part). All r draws are generated in one batched, vectorized pass in the main process from a single RNG stream, so results depend only on seed, never on set_num_cores() (which parallelizes the statistic, not the draws). draw_bootstrap_indices() resamples within each block by default (bootstrap_type = "within_blocks" or NULL, via stratified_bootstrap_indices_cpp()), or resamples whole blocks with replacement otherwise (via resample_group_rows_cpp()) — mirroring the block structure in the resampling scheme, analogous to the cluster-level bootstrap in DesignFixedBlockedCluster.

How inference accounts for the blocks

  • Block-aware: randomization inference (re-draws within blocks), the bootstrap (resamples within blocks), the jackknife, and the block-stratified tests InferenceIncidCMH and InferenceIncidExtendedRobins. The jackknife's automatic unit here is one subject (unit = "observation_within_block"): each replicate deletes one subject, and the variance pools the replicates' spread within blocks (the stratified jackknife of Wolter 2007, Introduction to Variance Estimation, 2nd ed., Sec. 4.5; formulas in InferenceJackknife). A block of one subject contributes nothing. The jackknife-Wald test and interval use the t distribution with \(\sum_h (n_h - 1)\) degrees of freedom (v1.0.6-TODO-231). Before EDI 1.0.6 a replicate deleted a whole block, which left only as many replicates as blocks (v1.0.6-TODO-4).

  • Not block-aware: the regression-type classes (OLS, Lin, robust, quantile, GLM, ordinal, survival regressions, ...) add no block fixed effects automatically; the blocks enter the model only if the blocking column is itself supplied as a covariate. There is currently no option to add them.

  • Unadjusted estimators. If the same proportion is treated in every block, an unadjusted pooled estimator (e.g. the mean difference or OLS) stays unbiased and its asymptotic SE is conservative: it does not remove the between-block variance, so intervals over-cover and power is lost.

  • Caveat: unequal allocation across blocks. The per-block rule treats floor(N * prob_T) or ceiling(N * prob_T) subjects, and floor when ceiling(N * prob_T) == N (and N > 1). With equal_block_sizes = TRUE and moderate prob_T the proportion treated is essentially the same in all blocks, but with equal_block_sizes = FALSE (or a user-supplied m) very small blocks combined with a large prob_T are treated at a lower proportion than large blocks (e.g. prob_T = 0.8: a block of 2 treats exactly 1 subject). If block size or membership is also related to the outcome, the pooled treated-versus-control comparison is then confounded by block (Simpson's paradox) and the estimate itself can be biased; adjust for blocks (include the blocking covariate(s) as factor covariates) in that case. A simulation (blocks of size 2, 20, 2, 20, block effects 2, 4, 6, 8, prob_T = 0.8, true effect 0) gave a mean OLS estimate of about 0.29.

References

Fisher, R. A. (1935). The Design of Experiments. Oliver and Boyd, for the original rationale for blocking in randomized experiments; Cochran, W. G., and Cox, G. M. (1957). Experimental Designs (2nd ed.), Wiley, for stratified (randomized block) design theory. See also randomized block design for orientation.

Super classes

Design -> DesignFixed -> DesignFixedBlocking

Methods

+ inherited public methods from DesignFixed
+ inherited public methods from Design


DesignFixedBlocking$new()

Initialize a fixed stratified-block randomized experimental design. Block construction and validation follow the rules described in the class documentation; see the parameter descriptions below for the greedy B_target/exact_num_blocks/equal_block_sizes contract.

Usage

DesignFixedBlocking$new(
  strata_cols = NULL,
  response_type,
  prob_T = 0.5,
  include_is_missing_as_a_new_feature = TRUE,
  n = NULL,
  preferred_num_bins_for_continuous_covariate = 2,
  B_target = NULL,
  exact_num_blocks = FALSE,
  equal_block_sizes = TRUE,
  m = NULL,
  verbose = FALSE,
  missingness_method = "impute",
  design_formula = ~.,
  seed = NULL
)

Arguments

strata_cols

A character vector of column names to use for stratification. If `NULL` (the default), all available covariate columns are used. Character and factor columns form one stratum per level. A numeric column that is integer-valued with at most 10 distinct values (for example a site code 1..4) is also treated as categorical, one stratum per value; other numeric columns are quantile-binned into `preferred_num_bins_for_continuous_covariate` bins. (Changed in 1.0.5: an integer site code used to be binned, e.g. 4 sites into 2 blocks.)

response_type

"continuous", "incidence", "proportion", "count", "survival", or "ordinal".

prob_T

Probability of treatment assignment.

include_is_missing_as_a_new_feature

Flag for missingness indicators.

n

The sample size. If NULL (default), n is set to nrow(X_all) when the subjects are added with add_all_subjects_to_experiment(); a given n must equal nrow(X_all). Assigning before the subjects are added needs n (a covariate-free design such as iBCRD or Bernoulli can then assign without X).

preferred_num_bins_for_continuous_covariate

The number of quantile bins to use for continuous strata. Default is 2.

B_target

The target number of blocks. Columns from `strata_cols` are added greedily in order, each column being included only if it does not push the total number of unique blocks beyond this target. For categorical covariates their natural levels are used; for continuous covariates `preferred_num_bins_for_continuous_covariate` quantile bins are used. Earlier columns are always preferred over later ones. Default: if you name `strata_cols` explicitly and leave `B_target` unset, it behaves like `B_target = NULL`: ALL named columns are used and the number of blocks is whatever their cross-classification gives (named strata are never silently dropped, e.g. at prime `n`). Note that with `equal_block_sizes = TRUE` this errors when the cells are not equal-sized (for example 2 sex cells at odd `n`); set `equal_block_sizes = FALSE`, or, for binned continuous strata, pass an explicit `B_target`. If `strata_cols = NULL` (all covariates available) the default is the largest divisor of `n` that is at most `floor(sqrt(n))` (so it always satisfies `equal_block_sizes = TRUE`); if `n` is not yet known it is resolved to `floor(sqrt(n))` when subjects are added. The same cap is used when `exact_num_blocks = TRUE` and `B_target` is unset. Set `B_target = NULL` to use all columns unconditionally. An explicitly supplied `B_target` that does not divide `n` still errors immediately when `equal_block_sizes = TRUE`. (Changed in 1.0.5: before, the sqrt-based cap also applied to named `strata_cols`.) Set `exact_num_blocks = TRUE` to hard fail if the final key construction does not produce exactly `B_target` blocks.

exact_num_blocks

Whether to require the greedy key construction to produce exactly `B_target` blocks. Default `FALSE`.

equal_block_sizes

Whether to require all blocks to have the same number of subjects. Default `TRUE`. When `TRUE` and both `n` and `B_target` are known at construction time, an error is raised immediately if `n` is not divisible by `B_target`. A second check fires when the blocks are first formed, not when the subjects are added: at `assign_w_to_all_subjects()` when the design draws `w`, or, for a `w_precomputed` assignment, at the first redraw, bootstrap or randomization call. If the covariate-based strata produce unequal block sizes the design errors there with a message suggesting `equal_block_sizes = FALSE`. This is the usual outcome when named `strata_cols` are used without `B_target` and the strata cells do not divide `n`. Set to `FALSE` to allow unequal blocks (note that `InferenceIncidCMH` and `InferenceIncidExtendedRobins` still require equal block sizes regardless).

m

Optional integer vector of explicit block identifiers, one per subject. If supplied, `n` must also be supplied and `length(m)` must equal `n`. The constructor then records this blocking structure immediately via `set_m()`, bypassing covariate-derived strata construction.

verbose

A flag for verbosity.

missingness_method

How to handle missing values in covariates.

design_formula

A formula object.

seed

Integer seed for reproducibility.

Returns

A new `DesignFixedBlocking` object


DesignFixedBlocking$clone()

The objects of this class are cloneable with this method.

Usage

DesignFixedBlocking$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

des = DesignFixedBlocking$new(n = 20, response_type = 'continuous',
  strata_cols = 'x2', equal_block_sizes = FALSE)
X = data.frame(x1 = rnorm(20), x2 = factor(rep(1:2, 10)))
des$add_all_subjects_to_experiment(X)
des$assign_w_to_all_subjects()