
Kapelner and Krieger's (2021) Outcome-Weighted Sequential Matching-on-the-Fly Design
Source:R/design_seq_one_by_one_KK21.R
DesignSeqOneByOneKK21.RdA DesignSeqOneByOneKK14 extension that
replaces KK14's unweighted Mahalanobis matching distance with a
response-weighted squared distance: at each assignment, a per-covariate
weight vector is re-estimated from the responses observed so far (weighting each
covariate by the estimated strength of its association with the response, e.g. an
absolute standardized regression coefficient), and the new subject is matched to its
nearest reservoir subject under that weighted distance rather than the raw
(unweighted) Mahalanobis distance KK14 uses. Weighting the match by outcome
association means matching effort is spent preferentially on prognostic
covariates (those that actually explain outcome variance) rather than equally on
every covariate, which is intended to improve estimator efficiency beyond what
outcome-agnostic matching achieves. The morrison argument is reserved for
Morrison's (2025) alternative calibration of the matching-acceptance threshold; it is
not implemented yet, and morrison = TRUE gives a warning and changes nothing.
Weight estimation. compute_weights() dispatches on
response_type to a corresponding kk21_*_weights_cpp() backend that fits
a per-covariate simple regression of the response on that covariate (given all
responses observed so far) and returns the absolute t-statistic (coefficient over its
standard error) as that covariate's weight: OLS for "continuous", logistic for
"incidence", negative-binomial (or, if count_use_speedup = TRUE, OLS on
log(y + 1)) for "count", beta regression (or OLS on the logit scale if
proportion_use_speedup = TRUE) for "proportion", Weibull/lognormal/
log-logistic AFT (or OLS on log(y) if
survival_use_speedup_for_no_censoring = TRUE and there is no censoring yet)
for "survival", and proportional-odds (or OLS on the numeric-coerced level if
ordinal_use_speedup = TRUE) for "ordinal". (The proportion speedup's logit
is infinite at a response of exactly 0 or 1, so when any response so far is 0 or 1,
every response is first compressed to \((y(n - 1) + 0.5) / n\), where \(n\) is the
number of responses in the regression. This is the standard transformation of
Smithson and Verkuilen (2006) for proportions that include the extremes (restated in
Cribari-Neto and Zeileis 2010, p. 3). It maps \([0, 1]\) affinely onto
\([0.5/n, 1 - 0.5/n]\), so the order of the responses is kept and the logits lie
within \(\pm\log(2n - 1)\), instead of a clamp to machine epsilon that turned a 0
or 1 into a \(\pm 36\) outlier dominating the OLS t-statistics. When every response
is strictly inside \((0, 1)\) the plain logit is used.) Weights are normalized to
sum to 1 and cached per-iteration in private$iteration_weights (retrievable
via get_iteration_weights()); the *_use_speedup flags trade weight
accuracy for speed by substituting a fast continuous-regression proxy for the
response-type-appropriate GLM/AFT/ordinal fit on every single assignment call.
How much weight accuracy do the speedups cost? A weight-level simulation
(October 2026; eight standard-normal covariates with decaying true coefficients,
\(n\) from 15 to 250, count, proportion and ordinal responses) found the loss to be
negligible. Across all three response types the fast OLS-on-transform weights were as
close to the GLM's own large-sample target (its normalized weights at
\(n = 20000\)) as the GLM's finite-sample weights themselves were: the
total-variation distance between the normalized fast and GLM weight vectors was 0.02 to
0.16, versus 0.1 to 0.43 for each method's own sampling noise, and the two large-sample
weight vectors differed by under 2% of total weight mass. The two weight vectors
selected the same nearest reservoir match 75 to 98% of the time, and the disagreements
were near-ties between two reservoir candidates. Running the full design end to end
(\(n = 200\), 150 replications per setting, with DesignSeqOneByOneKK14 as the
unweighted reference) confirmed this downstream: the share of the true linear
predictor's variance remaining inside matched pairs was indistinguishable between the
fast and GLM weights for count (0.192 vs 0.191), sparse count with about 70% zeros
(0.187 vs 0.188), proportion (0.147 vs 0.148) and four-level ordinal (0.181 vs 0.183)
responses, with Monte Carlo standard errors near 0.004 and the same number of matched
pairs (about 90 of 200) under both. That within-pair imbalance is the design-side term
in the matched-pairs estimator's variance, so it is the channel through which the
weights affect power; power itself was not simulated, because a gap this small (under
2% of a term that is itself only part of the total variance) is far below what a
direct power simulation of feasible size could resolve. The approximation error is
therefore swamped by the estimation noise both methods share, which is why the
*_use_speedup flags default to TRUE.
Weighted matching test. The weighted squared distance from the new subject
to every reservoir subject is computed
(compute_weighted_sqd_distances_cpp()), and the match is accepted only if the
minimum weighted distance falls below the private$compute_lambda() quantile of
a bootstrapped reference distribution of weighted pairwise distances among
subjects enrolled so far (compute_bootstrapped_weighted_sqd_distances_cpp(),
num_boot resamples) — a nonparametric, simulation-based acceptance threshold,
in contrast to KK14's closed-form F-distribution threshold. As in KK14, an accepted
match receives the opposite treatment of its match; a rejected (or empty-reservoir)
draw is randomized and added to the reservoir.
Fallback to KK14. Before enough responses have accumulated to fit the
weight-estimation regressions reliably (fewer than 2 * (ncol(X) + 2)
non-missing responses — two observations per regression parameter, at minimum
ncol(X) + 2), assign_wt() falls back to the inherited (unweighted) KK14
assignment rule entirely, via super$assign_wt().
References
Kapelner, A., and Krieger, A. M. (2014). "Matching on-the-fly: Sequential
allocation with higher power and efficiency." Biometrics, 70(2), 378-388,
doi:10.1111/biom.12148
, for the base matching-on-the-fly algorithm this class
extends with outcome-weighted distances; Kapelner, A., and Krieger, A. M. (2023). "A matching procedure for sequential
experiments that iteratively learns which covariates improve power."
Biometrics, 79(1), 216-229 (published online 2021), doi:10.1111/biom.13561
. See also
Morrison, T. (2025), "Reservoir Designs for Online Paired Experiments",
arXiv:2505.17247, for the alternative threshold calibration the (not yet
implemented) morrison argument is reserved for. Smithson, M., and Verkuilen, J.
(2006). "A better lemon squeezer? Maximum-likelihood regression with beta-distributed
dependent variables." Psychological Methods, 11(1), 54-71,
doi:10.1037/1082-989X.11.1.54
, for the \((y(n - 1) + 0.5) / n\) compression of 0/1
responses in the proportion speedup; restated in Cribari-Neto, F., and Zeileis, A.
(2010). "Beta regression in R." Journal of Statistical Software, 34(2), 1-24,
doi:10.18637/jss.v034.i02
.
Super classes
Design -> DesignSeqOneByOne -> DesignSeqOneByOneKK14 -> DesignSeqOneByOneKK21
Methods
+ inherited public methods from DesignSeqOneByOneKK14
DesignSeqOneByOneKK14$add_all_subject_matched_pair_ids()DesignSeqOneByOneKK14$assert_blocking_design()DesignSeqOneByOneKK14$assert_equal_block_sizes()DesignSeqOneByOneKK14$assert_matching_design()DesignSeqOneByOneKK14$get_block_ids()DesignSeqOneByOneKK14$get_cmh_se_w_mat()DesignSeqOneByOneKK14$get_matching_cluster_ids()DesignSeqOneByOneKK14$inject_cmh_se_w_mat()DesignSeqOneByOneKK14$is_a_kk_matching_capable()DesignSeqOneByOneKK14$is_blocking_design()DesignSeqOneByOneKK14$is_complete_blocking_design()DesignSeqOneByOneKK14$is_matching_design()DesignSeqOneByOneKK14$set_m()DesignSeqOneByOneKK14$summarize_blocks()+ inherited public methods from DesignSeqOneByOne
+ inherited public methods from Design
Design$add_all_subject_responses()Design$add_one_subject_response()Design$any_censoring()Design$applicable_inference_class_names()Design$assert_all_responses_recorded()Design$assert_all_subjects_arrived()Design$assert_even_allocation()Design$assert_fixed_sample()Design$capabilities()Design$check_experiment_completed()Design$duplicate()Design$get_X()Design$get_X_imp()Design$get_X_raw()Design$get_design_formula()Design$get_edi_version_created()Design$get_effective_dead()Design$get_effective_time()Design$get_missingness_method()Design$get_n()Design$get_ordinal_levels()Design$get_original_ordinal_levels()Design$get_prob_T()Design$get_response_type()Design$get_response_type_original()Design$get_t()Design$get_w()Design$get_y()Design$get_y_L()Design$get_y_R()Design$get_y_original()Design$has_general_censoring()Design$incompatible_inference_classes_due_to_design_structure()Design$is_a_bernoulli_capable()Design$is_a_cluster_capable()Design$is_fixed_sample_size()Design$overwrite_all_subject_assignments()Design$prepare_for_resampling_replay()Design$randomization_family()Design$supports()Design$supports_randomization_draw()Design$supports_resampling()Design$supports_resampling_replay()Design$transform_y()Design$unavailable_inference_classes_due_to_missing_packages()Design$warm_all_subject_data_cache()
DesignSeqOneByOneKK21$new()
Initialize a matching-on-the-fly sequential experimental design which matches based on
Kapelner and Krieger (2021).
There is no prob_T argument: allocation is always 1:1
(prob_T = 0.5) because each matched pair adds one treated and one control subject.
Usage
DesignSeqOneByOneKK21$new(
response_type,
include_is_missing_as_a_new_feature = TRUE,
n = NULL,
verbose = FALSE,
lambda = NULL,
t_0_pct = NULL,
morrison = FALSE,
p = NULL,
num_boot = NULL,
count_use_speedup = TRUE,
proportion_use_speedup = TRUE,
survival_use_speedup_for_no_censoring = TRUE,
ordinal_use_speedup = TRUE,
missingness_method = "impute",
design_formula = ~.,
seed = NULL,
...
)Arguments
response_typeThe data type of response values which must be one of the following: "continuous", "incidence", "proportion", "count", "survival", "ordinal". This package will enforce that all added responses via the
add_one_subject_responsemethod will be of the appropriate type.include_is_missing_as_a_new_featureIf missing data is present in a variable, should we include another dummy variable for its missingness in addition to imputing its value? If the feature is type factor, instead of creating a new column, we allow missingness to be its own level. The default is
TRUE.nThe sample size (if fixed). Default is
NULLfor not fixed.verboseA flag indicating whether messages should be displayed to the user. Default is
FALSE.lambdaThe quantile cutoff of the subject distance distribution for determining matches. If unspecified, default is 10%.
t_0_pctThe percentage of total sample size n where matching begins. If unspecified, default is 35%. With
n = NULLthere is no planned sample size, sot_0_pctis not used: matching begins once the covariate model matrix exists (more than p + 2 subjects have arrived).morrisonReserved for Morrison's (2025) alternative matching-threshold calibration, which is not implemented yet: the value is recorded but no code path reads it, and
morrison = TRUEgives a warning and runs the standardlambda/t_0_pctschedule. Default isFALSE.pReserved for the Morrison calibration (the number of covariate features); recorded but not used.
num_bootthe number of bootstrap samples taken to approximate the subject-distance distribution. Default is 500.
count_use_speedupShould we speed up the estimation of the weights in the response = count case via a continuous regression on log(y + 1) instead of a negative binomial regression each time? This is at the expense of the weights being less accurate. Default is
TRUE. See the How much weight accuracy do the speedups cost? paragraph of the Details section for simulation evidence that the accuracy lost is negligible.proportion_use_speedupShould we speed up the estimation of the weights in the response = proportion case via a continuous regression on log(y / (1 - y)) instead of a beta regression each time? This is at the expense of the weights being less accurate. Default is
TRUE. If any response so far is exactly 0 or 1, all responses are first compressed to \((y(n - 1) + 0.5) / n\) (Smithson and Verkuilen 2006; see Details). See the How much weight accuracy do the speedups cost? paragraph of the Details section for simulation evidence that the accuracy lost is negligible.survival_use_speedup_for_no_censoringShould we speed up the estimation of the weights in the response = survival case via a continuous regression on log(y) instead of a Weibull AFT regression each time, but only when there is no censoring in the data collected so far? This is at the expense of the weights being less accurate when censoring is present. Default is
TRUE.ordinal_use_speedupShould we speed up the estimation of the weights in the response = ordinal case via a continuous regression on the ordinal levels coerced to numeric instead of a proportional odds model each time? This is at the expense of the weights being less accurate. Default is
TRUE. See the How much weight accuracy do the speedups cost? paragraph of the Details section for simulation evidence that the accuracy lost is negligible.missingness_methodHow to handle missing values in covariates.
design_formulaA formula object.
seedInteger seed for reproducibility.
...Extra arguments passed to the
DesignSeqOneByOneKK14superclass.
Examples
seq_des = DesignSeqOneByOneKK21$new(n = 6, response_type = "continuous")
seq_des$add_one_subject_to_experiment_and_assign(data.frame(x = rnorm(1)))DesignSeqOneByOneKK21$get_iteration_weights()
Retrieve the full history of normalized covariate weight
vectors computed by compute_weights() across every assignment call
so far (see class documentation), keyed by subject index t, for
inspecting how the outcome-informed weighting evolved as data accrued.
DesignSeqOneByOneKK21$get_covariate_weights()
Retrieve the normalized covariate weight vector from the most recent assignment call (see class documentation for how weights are estimated).
DesignSeqOneByOneKK21$assign_wt()
Draw the next subject's treatment assignment via the KK21 outcome-weighted matching-on-the-fly rule (see class documentation): falls back to unweighted KK14 matching if too few responses have accumulated to estimate covariate weights, otherwise re-estimates weights, matches to the nearest reservoir subject under the weighted distance if it clears the bootstrapped acceptance threshold (assigning the opposite treatment), or randomizes into the reservoir otherwise.
Examples
set.seed(1)
n = 30
seq_des = DesignSeqOneByOneKK21$new(n = n, response_type = 'continuous')
# Responses must arrive as subjects enroll: the weights are estimated from the
# responses seen so far, and weighted matching starts once the burn-in
# (t_0_pct = 35\% of n) is over and 2 * (p + 2) responses are in.
for (t in 1 : n) {
x_t = data.frame(x1 = rnorm(1), x2 = rnorm(1))
seq_des$add_one_subject_to_experiment_and_assign(x_t)
seq_des$add_one_subject_response(t, 2 * x_t$x1 + rnorm(1))
}
seq_des$get_covariate_weights() # x1 (prognostic) should dominate
#> x1 x2
#> 0.91734842 0.08265158
length(seq_des$get_iteration_weights()) # one weight vector per weighted assignment
#> [1] 30
## ------------------------------------------------
## Method `DesignSeqOneByOneKK21$new()`
## ------------------------------------------------
# \donttest{
seq_des = DesignSeqOneByOneKK21$new(n = 6, response_type = "continuous")
seq_des$add_one_subject_to_experiment_and_assign(data.frame(x = rnorm(1)))
#> [1] 0
# }