Skip to contents

In a sequential trial, subjects enroll one at a time over weeks or months, and each one is assigned to treatment or control on the day they enroll. The statistics of such designs are covered elsewhere: Matching on the Fly explains the Kapelner–Krieger designs, and Planning a Study covers power and sample size. This article is about operations. It covers what has to happen between the first enrollment and the final analysis when the trial is spread over many R sessions, probably several computers, and possibly several people:

  • creating the design before the first subject arrives;
  • assigning each arrival and saving the design to disk between enrollments;
  • making the allocation reproducible, so that an interrupted trial continues exactly as an uninterrupted one would have;
  • recording responses that arrive late or out of order;
  • what to do when some responses never arrive;
  • keeping an audit trail from which anyone can reproduce the allocation;
  • the final analysis.

It assumes the four steps of an EDI (Experimental Design and Inference) analysis (design, assign, record responses, infer) from Get started.

The sequential designs

EDI’s sequential designs all share the same interface: a $new() call before the trial, then one add_one_subject_to_experiment_and_assign() call per arrival and one add_one_subject_response() call per outcome. Everything in this article applies to all of them.

Class How each arrival is assigned
DesignSeqOneByOneBernoulli An independent coin with probability prob_T
DesignSeqOneByOneiBCRD Complete randomization run sequentially: exactly round(n * prob_T) treated at the end (requires n)
DesignSeqOneByOneRandomBlockSize Permuted blocks of randomly varying size, optionally within strata
DesignSeqOneByOneSPBR Stratified permuted blocks of fixed size
DesignSeqOneByOneEfron Efron’s biased coin toward the arm that is behind
DesignSeqOneByOneUrn Wei’s adaptive urn
DesignSeqOneByOneAtkinson Atkinson’s covariate-adjusted biased coin
DesignSeqOneByOnePocockSimon Pocock–Simon minimization over categorical strata
DesignSeqOneByOneKK14 Matching on the fly with a Mahalanobis distance
DesignSeqOneByOneKK21 Matching on the fly with weights learned from the responses so far
DesignSeqOneByOneKK21stepwise As KK21, with stepwise weights

The running example uses DesignSeqOneByOneKK21, because it is the one design whose assignments depend on the responses recorded so far, which makes the order of events part of the allocation. That is the hardest case for persistence and auditing; a trial run with any other design in the table is a special case of it.

The trial

Sixty patients enroll over about seven months, with three baseline covariates recorded at enrollment. The outcome is measured at a follow-up visit eight weeks later, give or take two weeks, so outcomes come back in a different order from the one in which patients enrolled, and for most of the trial the most recent patients’ outcomes are still unknown.

In a real trial the arrival dates, covariates and outcomes come from the clinic. Here they are simulated, all in advance, so that nothing below draws random numbers except the design itself. y0 is the outcome each patient would have under control; the treatment adds true_effect.

set.seed(20261117)
n = 60
X = data.frame(age_z = round(rnorm(n), 2), severity = round(rnorm(n), 2),
               biomarker = round(rnorm(n), 2))
true_effect = 0.5
y0 = 1.5 * X$severity + 0.8 * X$biomarker + rnorm(n)

day_enrolled = sort(round(cumsum(rexp(n, rate = 1 / 3.5))))   # one arrival every 3.5 days
day_outcome  = day_enrolled + round(runif(n, 42, 70))          # follow-up 6 to 10 weeks later
start = as.POSIXct("2027-01-04 09:00", tz = "UTC")

The trial’s history is a list of events, in calendar order. An enrollment is always before that patient’s outcome; on a day with both kinds of event, the enrollment goes first.

events = rbind(
  data.frame(day = day_enrolled, type = "enroll",  subject = seq_len(n)),
  data.frame(day = day_outcome,  type = "outcome", subject = seq_len(n))
)
events = events[order(events$day, events$type == "outcome", events$subject), ]
events$time = start + events$day * 86400
rownames(events) = NULL
head(events, 8)
#>   day   type subject                time
#> 1   4 enroll       1 2027-01-08 09:00:00
#> 2   7 enroll       2 2027-01-11 09:00:00
#> 3   9 enroll       3 2027-01-13 09:00:00
#> 4  28 enroll       4 2027-02-01 09:00:00
#> 5  29 enroll       5 2027-02-02 09:00:00
#> 6  30 enroll       6 2027-02-03 09:00:00
#> 7  36 enroll       7 2027-02-09 09:00:00
#> 8  36 enroll       8 2027-02-09 09:00:00

Before the first subject: create the design and save it

Everything that defines the allocation rule is fixed at construction: the class, the response type, the target sample size n, the design’s tuning arguments, and the seed. Write these into the protocol.

  • Set n. The Kapelner–Krieger designs need it, since their burn-in (t_0_pct * n subjects) and their matching threshold depend on it, and EDI’s fixed-sample analysis assumes that the number of subjects was fixed before enrollment and not chosen by watching the outcomes. The design does not stop enrollment for you, but it refuses subject n + 1 with an error.
  • Fix the covariate list. Every arrival must be a one-row data frame with the same columns, of the same types, measured before assignment. A value whose type differs from its column’s (a number that arrives as a character string, say) is converted to the column’s type; if it cannot be ("1.7a" for a numeric column), the subject is refused with an error naming the column and the design is left as it was, so you can correct the value and add the subject again. A missing covariate value is allowed: it is imputed (see the missingness_method argument).
  • Set seed. With a seed, the design seeds each assignment from the seed and the subject’s enrollment number, and puts the session’s random number stream back afterwards. So the seed fixes the whole allocation (given the covariates and, for KK21, the outcomes known at each assignment), whatever else the R session does in between, and the seed is saved inside the design object. Without a seed (seed = NULL), assignments draw from the session’s random number stream, and an exact restart would need that stream’s state (.Random.seed) saved and restored around every assignment; see Reproducibility.

The trial state lives in one file: the design object and the event log. saveRDS() stores the whole R6 object, including the assignments, the responses, the match structure and KK21’s weight history; there is no special save function. Each save writes a temporary file first and then renames it over the old one, so a crash during a save cannot leave a half-written trial file.

save_trial = function(state, file) {
  tmp = paste0(file, ".tmp")
  saveRDS(state, tmp)
  invisible(file.rename(tmp, file))    # replaces the old file only once the new one is complete
}

trial_seed = 7041
trial_file = file.path(tempdir(), "trial.rds")

des = DesignSeqOneByOneKK21$new(response_type = "continuous", n = n, seed = trial_seed)
save_trial(list(
  design = des,              # carries the seed
  log    = data.frame(time = start[0], event = character(0), subject = integer(0),
                      value = numeric(0))
), trial_file)
rm(des)

In practice the file goes somewhere that is backed up, and the copy made after each enrollment is worth keeping (a dated file name, or a version-controlled directory), so that a mistaken entry can be undone by going back one file.

Each arrival: load, assign, save

Two small functions do all the bookkeeping. enroll() loads the trial, assigns the new patient, appends a line to the log, and writes the file back. record_outcome() does the same for an outcome. Neither touches R’s random number generator: the seeded design draws each assignment from its own seed and leaves the session’s stream as it found it, so other work in the same R session neither affects the trial nor is affected by it.

log_event = function(state, time, event, subject, value) {
  state$log = rbind(state$log, data.frame(time = time, event = event,
                                          subject = subject, value = value))
  state
}

enroll = function(file, x_new, time) {
  state = readRDS(file)
  w = state$design$add_one_subject_to_experiment_and_assign(x_new)
  state = log_event(state, time, "assign", state$design$get_t(), w)
  save_trial(state, file)
  w
}

record_outcome = function(file, subject, y, time) {
  state = readRDS(file)
  state$design$add_one_subject_response(subject, y)
  state = log_event(state, time, "outcome", subject, y)
  save_trial(state, file)
  invisible(y)
}

Now run the trial, one event at a time. Every call reads the file from disk and writes it back, as it would if each enrollment happened on a different day in a different R session. The runif() line stands in for whatever else the session does between enrollments.

w_assigned = rep(NA_integer_, n)
for (e in seq_len(nrow(events))) {
  i = events$subject[e]
  if (events$type[e] == "enroll") {
    w_assigned[i] = enroll(trial_file, X[i, , drop = FALSE], events$time[e])
  } else {
    record_outcome(trial_file, i, y0[i] + true_effect * w_assigned[i], events$time[e])
  }
  invisible(runif(10))    # other work in the session
}
state = readRDS(trial_file)
c(enrolled = state$design$get_t(), treated = sum(state$design$get_w()),
  pairs = max(state$design$get_block_ids()))
#> enrolled  treated    pairs 
#>       60       31       25

The subject index t that add_one_subject_response() takes is the enrollment order, so the trial’s own patient identifiers need a lookup table to t; here they coincide.

Saving and restoring is exact

Because each assignment is seeded from the trial’s seed and the subject’s enrollment number, stopping and restarting the trial makes no difference to the allocation. To check, rerun the same events in a single session, holding the design in memory and never saving it:

uninterrupted = function(events, seed, skip_outcomes = integer(0)) {
  d = DesignSeqOneByOneKK21$new(response_type = "continuous", n = n, seed = seed)
  for (e in seq_len(nrow(events))) {
    i = events$subject[e]
    if (events$type[e] == "enroll") {
      d$add_one_subject_to_experiment_and_assign(X[i, , drop = FALSE])
    } else if (!(i %in% skip_outcomes)) {
      d$add_one_subject_response(i, y0[i] + true_effect * d$get_w()[i])
    }
  }
  d
}
des_mem = uninterrupted(events, trial_seed)
c(same_assignments = identical(des_mem$get_w(), state$design$get_w()),
  same_pairs = identical(des_mem$get_block_ids(), state$design$get_block_ids()))
#> same_assignments       same_pairs 
#>             TRUE             TRUE

Both runs give the same 60 assignments and the same pairs.

Nor does the session’s other work matter. Here is the same trial with the design kept in memory and the runif() calls sharing R’s random number generator between assignments:

d_shared = DesignSeqOneByOneKK21$new(response_type = "continuous", n = n, seed = trial_seed)
for (e in seq_len(nrow(events))) {
  i = events$subject[e]
  if (events$type[e] == "enroll") {
    d_shared$add_one_subject_to_experiment_and_assign(X[i, , drop = FALSE])
  } else {
    d_shared$add_one_subject_response(i, y0[i] + true_effect * d_shared$get_w()[i])
  }
  invisible(runif(10))
}
sum(d_shared$get_w() != state$design$get_w())    # assignments that differ
#> [1] 0

None differ. This needs the seed: with seed = NULL the design draws from the session’s stream, and the other work in the session would change the assignments that follow it. That allocation would be just as valid a draw from the design, so the trial’s validity does not depend on the seed. What is lost without one is the ability to show afterwards that the allocation is the one the seed produces.

The design records the EDI version it was created under. If a design saved under one major version of EDI is reloaded under another, the first assignment after the reload gives a warning. Within a trial, keep the EDI version fixed, and record it:

state$design$get_edi_version_created()
#> [1] "1.0.5"

Outcomes that arrive late or out of order

add_one_subject_response(t, y) records the outcome of the subject enrolled t-th, whenever it arrives; t must be a subject who has already enrolled. With n set, the outcomes can come in any order, which they did here:

outcome_order = state$log$subject[state$log$event == "outcome"]
head(outcome_order, 15)
#>  [1]  2  1  3  6  4  9 11  5 12  7  8 14 10 13 15

Recording an outcome a second time replaces the first value with a warning (“Overwriting previous response for t = …”). Use this to correct a data-entry error, and log the correction like any other event.

What late outcomes do to KK21. Every design except KK21 and KK21stepwise ignores the outcomes while assigning, so for them the outcomes only need to be recorded before the analysis. KK21 is different: at each assignment it estimates its covariate weights from the outcomes recorded at that moment. Until at least \(2(p+2)\) outcomes are in (\(p = 3\) covariates here, so 10), it cannot estimate the weights and uses the KK14 rule instead. Here is how many outcomes were known at each assignment, and the first assignment at which KK21 used weights:

ev_log = state$log
known_at_assignment = sapply(which(ev_log$event == "assign"),
                             function(k) sum(ev_log$event[seq_len(k)] == "outcome"))
iw = state$design$get_iteration_weights()
first_weighted = which(!vapply(seq_len(n), function(t) t > length(iw) || is.null(iw[[t]]),
                               logical(1)))[1]
c(burn_in_ends_after = floor(0.35 * n), first_weighted_assignment = first_weighted,
  outcomes_known_then = known_at_assignment[first_weighted])
#>        burn_in_ends_after first_weighted_assignment       outcomes_known_then 
#>                        21                        26                        15
known_at_assignment[c(20, 30, 40, 50, 60)]
#> [1]  2 21 28 30 35

For comparison, the same 60 patients with each outcome known as soon as the patient is assigned (as if the outcome were immediate):

immediate = events[events$type == "enroll", ]
immediate = immediate[rep(seq_len(n), each = 2), ]
immediate$type = rep(c("enroll", "outcome"), n)
des_immediate = uninterrupted(immediate, trial_seed)
iw_imm = des_immediate$get_iteration_weights()
which(!vapply(seq_len(n), function(t) t > length(iw_imm) || is.null(iw_imm[[t]]),
              logical(1)))[1]
#> [1] 22

With immediate outcomes, KK21 weights from the first assignment after the burn-in. With the follow-up delay it started at subject 26, and every assignment used fewer outcomes than there were subjects: when the last patient enrolled, only 35 of the earlier 59 outcomes were known. The longer the follow-up relative to the enrollment period, the larger this gap.

Two practical consequences:

  • The order of events is part of the allocation. For KK21, the same patients, outcomes and seed give a different allocation if the outcomes are recorded at different times. To reproduce a KK21 allocation, you need the order in which enrollments and outcomes were entered, not just the data. The audit log below records it.
  • Record outcomes as soon as they are final. An outcome that sits in a case report form for a month is a month during which KK21 matches on less information. Do not hold outcomes back and enter them in a batch. If the follow-up is so long that only a few outcomes will be known before enrollment ends, KK21 will spend most of the trial applying the KK14 rule, and KK14 is the simpler choice.

The audit trail

The log saved with the trial has one line per event: when it happened, what it was, which subject, and the assignment or the outcome.

head(state$log, 10)
#>                   time  event subject value
#> 1  2027-01-08 09:00:00 assign       1     1
#> 2  2027-01-11 09:00:00 assign       2     0
#> 3  2027-01-13 09:00:00 assign       3     1
#> 4  2027-02-01 09:00:00 assign       4     0
#> 5  2027-02-02 09:00:00 assign       5     0
#> 6  2027-02-03 09:00:00 assign       6     1
#> 7  2027-02-09 09:00:00 assign       7     1
#> 8  2027-02-09 09:00:00 assign       8     1
#> 9  2027-02-10 09:00:00 assign       9     0
#> 10 2027-02-10 09:00:00 assign      10     0
table(state$log$event)
#> 
#>  assign outcome 
#>      60      60

Together with the protocol’s design arguments, the seed, the covariate rows exactly as they were entered, and the EDI and R versions, this log is enough for anyone to reproduce the allocation. The replay below starts a fresh design with the same seed and walks through the log, entering each enrollment and each logged outcome in the recorded order, and checks every assignment against the log as it goes. Nothing in the replay depends on the trial file.

replay = function(log, X, seed) {
  d = DesignSeqOneByOneKK21$new(response_type = "continuous", n = nrow(X), seed = seed)
  for (k in seq_len(nrow(log))) {
    i = log$subject[k]
    if (log$event[k] == "assign") {
      w = d$add_one_subject_to_experiment_and_assign(X[i, , drop = FALSE])
      if (w != log$value[k]) stop("assignment of subject ", i, " does not match the log")
    } else {
      d$add_one_subject_response(i, log$value[k])
    }
  }
  d
}
des_replayed = replay(state$log, X, trial_seed)
identical(des_replayed$get_w(), state$design$get_w())
#> [1] TRUE

The replay reproduces every assignment. Keep the covariate rows in a format that stores numbers exactly (an .rds file, or a CSV written with full precision): a covariate rounded on export changes the distances the design computed, and with them possibly the matches. A full account of where EDI draws random numbers, including in randomization tests and the bootstrap, is in Reproducibility.

When some outcomes never arrive

Patients withdraw, move away or miss their follow-up visit. EDI’s inference classes need an outcome for every enrolled subject: building one on a design with a missing outcome stops with an error. To see this, rerun the trial with the outcomes of two patients never recorded, as if they had been lost to follow-up:

lost = c(12, 41)
des_lost = uninterrupted(events, trial_seed, skip_outcomes = lost)
des_lost$check_experiment_completed()
#> [1] FALSE
tryCatch(InferenceContinKKOLSOneLik$new(des_lost, verbose = FALSE),
         error = function(e) conditionMessage(e))
#> [1] "This experiment is incomplete as all responses aren't recorded yet."

The same check refuses an analysis when fewer than n subjects enrolled. EDI has no way to shrink n after the fact or to analyze a design with missing outcomes, so the choices are made outside the design object:

  • Time-to-event outcomes. A patient lost to follow-up is censored at their last contact: record add_one_subject_response(t, y_L = last_contact, y_R = Inf) on a design with response_type = "survival". The survival analyses handle this, under the usual assumption that censoring is independent of the event time. See the survival cookbook.
  • Other outcomes: get the outcome. Every effort to ascertain the outcome (a phone call, a registry, a late visit) is worth more than any analysis fix.
  • A complete-case analysis, as a secondary analysis. Enter the completers’ covariates, realized assignments and outcomes into an analysis-only design, as in Analyzing an Experiment You Already Ran, and use an asymptotic, covariate-adjusted analysis:
keep = setdiff(seq_len(n), lost)
w_all = des_lost$get_w()
y_all = des_lost$get_y()
des_cc = DesignFixedBernoulli$new(n = length(keep), response_type = "continuous",
                                  verbose = FALSE)
des_cc$add_all_subjects_to_experiment(X[keep, ])
des_cc$assign_w_to_all_subjects(w_precomputed = w_all[keep])
des_cc$add_all_subject_responses(y_all[keep])
inf_cc = InferenceContinOLS$new(des_cc, verbose = FALSE)
inf_cc$compute_estimate()
#> [1] 0.1430928
inf_cc$compute_asymp_confidence_interval(alpha = 0.05)
#>       2.5%      97.5% 
#> -0.4250036  0.7111892

A complete-case analysis is unbiased only if dropping out is unrelated to the outcome given the covariates and the treatment, which no data can confirm. It also discards the matched-pair structure: a pair that lost one member is no longer a pair. And the analysis design is not the design that made the assignments, so its randomization test and bootstrap would resample from the wrong mechanism; use only its asymptotic results. Report it as what it is, together with the number lost in each arm. Imputing the missing outcomes and then entering them as if they were observed is worse: the analysis would treat imputed values as data and understate its uncertainty.

The final analysis

When the last subject has enrolled and the last outcome is in, load the trial file once more, confirm that it is complete, and analyze the design object as usual. The recommended analysis for a continuous outcome under a KK design is InferenceContinKKOLSOneLik, described in Matching on the Fly:

final = readRDS(trial_file)
des_final = final$design
des_final$check_experiment_completed()
#> [1] TRUE

inf = InferenceContinKKOLSOneLik$new(des_final, verbose = FALSE)
inf$num_cores = 1L
inf$compute_estimate()
#> [1] 0.06387557
inf$compute_asymp_confidence_interval(alpha = 0.05)
#>       2.5%      97.5% 
#> -0.5737922  0.7015434
inf$compute_asymp_two_sided_pval()
#> [1] 0.8392853

The randomization test redraws assignments from the design’s own mechanism. Give the inference object its own seed so that the p-value can be reproduced; set_seed() does not disturb the session’s random number stream.

inf$set_seed(1)
inf$compute_rand_two_sided_pval(r = 200, show_progress = FALSE)
#> [1] 0.8756219

The same steps apply to every other design and outcome type; the cookbooks list the analyses for each outcome type (for example the continuous cookbook), and InferenceSuite runs all of the analyses that apply to a design.

The sequential designs have no interim analysis or stopping rule: the analysis is run once, after n subjects. Looking at the treatment effect partway through and stopping when it looks good invalidates the fixed-sample p-values and intervals above.

Checklist

Before the first subject:

  1. Fix the design class and its arguments, n, the response type, the covariate list and the seed in the protocol, and record the EDI and R versions.
  2. Create the design with its seed and save it to a backed-up file.

For each arrival:

  1. Load the trial file, call add_one_subject_to_experiment_and_assign(), log the event with its time, and write the file back.

For each outcome:

  1. Call add_one_subject_response() with the subject’s enrollment index as soon as the outcome is final, log it, and save.

At the end:

  1. Check check_experiment_completed(), replay the log to confirm the allocation, and run the prespecified analysis once.

Where to go next