
Analyzing an Experiment You Already Ran
Source:vignettes/articles/analyzing-an-existing-experiment.Rmd
analyzing-an-existing-experiment.RmdEvery other EDI vignette starts with EDI assigning treatment. Many analyses start later than that. The experiment is over, treatment was assigned by someone else (a randomization list, a sealed-envelope system, an online platform), and what you have is a data frame: one row per subject, some covariates, a 0/1 treatment column and an outcome.
EDI (Experimental Design and Inference) analyzes that data frame with the same classes it uses everywhere else. The only change is in step 2 of the usual four-step pattern (see Get started): instead of letting the design draw the assignment, you hand it the assignment you already have.
-
Construct the
Designthat matches how treatment was actually assigned, with the samen,prob_Tand blocks as the real experiment. -
Supply the observed treatment vector with
assign_w_to_all_subjects(w_precomputed = w). -
Record the responses with
add_all_subject_responses(). -
Construct an
Inferenceclass (or anInferenceSuite) and compute.
Why the design class matters
A point estimate, a Wald interval or a likelihood-ratio test only looks at the data you have. For those, the design class mostly affects how the covariates are stored.
Randomization inference is different. A randomization test asks: if
treatment had no effect, how unusual is the observed statistic among all
the assignments the experiment could have produced? EDI answers
by redrawing w many times from the design object’s own
randomization law, so that law has to be the one that was really used.
The bootstrap is affected in the same way for blocked designs, because
it resamples within blocks. If you declare complete randomization for an
experiment that was randomized within sites, the reference distribution
includes assignments that could never have happened, and the p-value is
wrong.
So before writing any code, find out from the protocol or the randomization log:
| What was done | EDI design | What to set |
|---|---|---|
| A fixed number treated, all such allocations equally likely (“complete randomization”) | DesignFixediBCRD |
prob_T = n_T / n, so round(n * prob_T) is
the number actually treated |
| An independent coin flip per subject; the number treated was random | DesignFixedBernoulli |
prob_T = the coin’s probability (often 0.5), not the
realized fraction |
| Complete randomization within strata (site, sex, …) | DesignFixedBlocking |
strata_cols naming the stratum column(s), or
m with one block ID per subject |
| Pairs formed first, one of each pair treated | DesignFixedBinaryMatch |
see its help page |
| Not randomized (treatment chosen by subjects or clinicians) | ObservationalDesign |
no prob_T; randomization inference is unavailable |
The remaining fixed designs in the reference
index all have
assign_w_to_all_subjects(w_precomputed = ) too. If the
experiment used a covariate-adaptive or response-adaptive
sequential procedure (Efron’s biased coin, Pocock-Simon
minimization, KK matching on the fly), this article does not apply:
EDI’s sequential designs assign each subject as they arrive and have no
argument for supplying a recorded assignment.
A finished multi-site trial
Here is the kind of data frame this article is about. Eighty patients
were enrolled at four sites, twenty per site. At each site, ten were
assigned to treatment by complete randomization. BMI was not recorded
for a few patients. The outcome is a continuous score. The data are
simulated, but the analysis below only uses the data frame
trial, exactly as if it had been read from a file.
set.seed(20261117)
n = 80
trial = data.frame(
site = rep(c("A", "B", "C", "D"), each = 20),
age = round(rnorm(n, 50, 10)),
bmi = round(rnorm(n, 28, 4), 1),
smoker = rbinom(n, 1, 0.3)
)
trial$treated = unlist(lapply(1:4, function(s) sample(rep(0:1, each = 10))))
trial$bmi[sample(n, 6)] = NA
site_effect = c(A = 0, B = 6, C = 12, D = -6)
trial$score = 20 + 3 * trial$treated + 0.2 * trial$age + site_effect[trial$site] +
rnorm(n, sd = 4)
head(trial)
#> site age bmi smoker treated score
#> 1 A 50 31.1 1 0 33.20496
#> 2 A 51 23.6 0 1 29.75801
#> 3 A 40 36.0 0 0 30.67978
#> 4 A 52 31.0 0 1 30.67670
#> 5 A 45 22.8 0 0 26.93614
#> 6 A 65 23.6 0 1 29.96384
table(trial$site, trial$treated)
#>
#> 0 1
#> A 10 10
#> B 10 10
#> C 10 10
#> D 10 10Treatment was randomized within site, so the matching design is
DesignFixedBlocking with strata_cols = "site".
The covariate data frame passed to the design holds only the covariates:
no treatment column and no outcome.
X = trial[, c("site", "age", "bmi", "smoker")]
des = DesignFixedBlocking$new(n = n, response_type = "continuous", strata_cols = "site",
prob_T = 0.5, verbose = FALSE)
des$add_all_subjects_to_experiment(X)
des$assign_w_to_all_subjects(w_precomputed = trial$treated)
des$add_all_subject_responses(trial$score)
all(des$get_w() == trial$treated)
#> [1] TRUEIt is worth checking that the design reproduces the real
randomization law. Each column of
draw_ws_according_to_design() is one assignment the design
could have produced; every one should treat ten patients at each site,
as the real trial did.
Estimate, interval and p-value
InferenceContinOLS regresses the outcome on treatment
and the covariates.
inf = InferenceContinOLS$new(des, verbose = FALSE)
inf$num_cores = 1L
est = inf$compute_estimate()
ci = inf$compute_asymp_confidence_interval(alpha = 0.05)
p_wald = inf$compute_asymp_two_sided_pval()
est; ci; p_wald
#> [1] 1.962395
#> 2.5% 97.5%
#> 0.435784 3.489006
#> [1] 0.01249339The estimated treatment effect is 1.96 points, with a 95% Wald interval from 0.44 to 3.49.
Randomization test
The randomization test redraws w within sites
r times, recomputes the statistic for each draw, and
compares the observed statistic with that reference distribution. It
needs no assumption about the distribution of the outcome. A real
analysis would use a larger r.
inf$set_seed(1)
p_rand = inf$compute_rand_two_sided_pval(r = 500, show_progress = FALSE)
p_rand
#> [1] 0.01996008The bootstrap also follows the blocks: by default it resamples
patients within each site. The interval below is the percentile type.
The default type, BCa, takes its acceleration from the jackknife on the
design’s automatic unit, which for DesignFixedBlocking
deletes one patient at a time within site, so it works with four sites
too and gives a finite interval close to the percentile one.
inf$set_seed(1)
inf$compute_bootstrap_confidence_interval(alpha = 0.05, B = 501, type = "percentile",
show_progress = FALSE)
#> 2.5% 97.5%
#> 0.4851443 3.3449949Every applicable analysis
InferenceSuite runs every inference class that applies
to this design and response type. As in the cookbooks, the call below
runs only the asymptotic methods and gives each class at most 15
seconds; drop methods for the full report, including the
randomization and bootstrap procedures, which takes minutes.
suite = InferenceSuite$new(des)
res = suite$run_all_inference(screen = TRUE, plots = FALSE, num_cores = 1L,
methods = c("wald", "score", "lik_ratio"),
max_secs_per_class = 15)
#> inference cov estimand est se pval pval method status
#> class mod
#> ===========================================================================================
#> Classes 0/11 [ 0% ] Status: Estimating...Avg Δ mean Δ 2.39 1.80 1.88e-01 wald ok
#> Classes 1/11 [=== 9% ] Estimated Time Left: 1sAvg Δ Pooled … mean Δ 2.39 1.80 1.88e-01 wald ok
#> Classes 2/11 [====== 18% ] Estimated Time Left: 1sWilcox HL shift 2.51 1.88 2.20e-01 wald ok
#> Classes 3/11 [========= 27% ] Estimated Time Left: 1sLin ~. mean Δ 2.10 0.736 5.84e-03 wald ok
#> Classes 4/11 [============ 36% ] Estimated Time Left: 1sLin ~. mean Δ 2.10 0.736 7.70e-03 score ok
#> Classes 5/11 [============== 45% ] Estimated Time Left: 1sLin ~. mean Δ 2.10 0.736 7.70e-03 lik_ratio ok
#> Classes 6/11 [============== 54% ] Estimated Time Left: 1sOLS ~. mean Δ 1.96 0.766 1.25e-02 wald ok
#> Classes 7/11 [============== 63% == ] Estimated Time Left: 1sOLS ~. mean Δ 1.96 0.766 1.04e-02 score ok
#> Classes 8/11 [============== 72% ===== ] Estimated Time Left: 0sOLS ~. mean Δ 1.96 0.766 1.04e-02 lik_ratio ok
#> Classes 9/11 [============== 81% ======== ] Estimated Time Left: 0sMedian Regr ~. median ef… 0.617 0.966 5.25e-01 wald ok
#> Classes 10/11 [============== 90% =========== ] Estimated Time Left: 0sRobust Regr ~. mean Δ 1.80 0.790 2.59e-02 wald ok
#> Classes 11/11 [============= 100% ==============] Estimated Time Left: 0s-------------------------------------------------------------------------------------------
#> Status: Completed in 1s.
#>
#> Estimand: HL shift (1 inferences) : p = 0.2199
#> Estimand: mean Δ (9 inferences) : p = 0.0120
#> Estimand: median effect (1 inferences): p = 0.5249
#>
#> Combined evidence against the sharp null across 3 estimands
#> (11 inferences, weighting = uniform within estimand):
#> p = 0.0343Covariates
Choosing which covariates enter the model
By default, an inference class adjusts for every covariate in the
design’s covariate matrix. Character and factor columns become dummy
variables, so site enters as three indicators:
head(des$get_X())
#> siteB siteC siteD age bmi smoker bmi_is_missing
#> 1 0 0 0 50 31.1 1 0
#> 2 0 0 0 51 23.6 0 0
#> 3 0 0 0 40 36.0 0 0
#> 4 0 0 0 52 31.0 0 0
#> 5 0 0 0 45 22.8 0 0
#> 6 0 0 0 65 23.6 0 0The model_formula argument of an inference class chooses
a different adjustment set. It is applied to the design’s covariates
(after imputation, see below), and the treatment term is added for
you.
inf_age_site = InferenceContinOLS$new(des, model_formula = ~ age + site, verbose = FALSE)
inf_age_site$num_cores = 1L
inf_age_site$compute_estimate()
#> [1] 1.965167
inf_age_site$compute_asymp_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> 0.4988818 3.4314521Choose the adjustment set before looking at the results, ideally from the analysis plan. Picking the covariates that give the smallest p-value invalidates the p-value.
Missing covariate values
Six patients have no BMI. Two design arguments control what happens:
-
missingness_methoddecides how the gaps are handled."impute"(the default) fills them in by random-forest imputation (missRanger) from the other covariates only, never the outcome or the treatment, so an imputed value cannot carry part of the treatment effect; it is seeded, so the same data always impute the same way."drop_column"drops any covariate column with a missing value;"error"stops so that nothing is changed silently. -
include_is_missing_as_a_new_feature(defaultTRUE) adds a 0/1 indicator for each covariate with missing values. That is thebmi_is_missingcolumn above.
Under the defaults, the model above used imputed BMI values plus the indicator. To leave BMI out of the analysis instead:
des_drop = DesignFixedBlocking$new(n = n, response_type = "continuous", strata_cols = "site",
prob_T = 0.5, missingness_method = "drop_column",
verbose = FALSE)
des_drop$add_all_subjects_to_experiment(X)
des_drop$assign_w_to_all_subjects(w_precomputed = trial$treated)
des_drop$add_all_subject_responses(trial$score)
colnames(des_drop$get_X())
#> [1] "siteB" "siteC" "siteD" "age" "smoker"
inf_drop = InferenceContinOLS$new(des_drop, verbose = FALSE)
inf_drop$num_cores = 1L
inf_drop$compute_estimate()
#> [1] 1.994814Missing outcomes are a different problem.
add_all_subject_responses() needs a value for every
subject, so subjects lost to follow-up have to be handled before the
data reach EDI (or, for a time-to-event outcome, recorded as censored,
as in the next section).
A survival outcome with censoring
The veteran data in the survival package come from a
randomized trial of two chemotherapy regimens for lung cancer:
trt is the regimen (1 standard, 2 test), time
is survival in days, and status is 0 for a censored
patient.
The data set’s documentation says the trial was randomized but not
how. Here we assume complete randomization with the observed number on
the test regimen. In a real analysis this is the point to check the
original report or the protocol. With DesignFixediBCRD, the
number treated in every redraw is round(n * prob_T), so
setting prob_T to the observed fraction reproduces the
observed count.
Censored patients are recorded as the interval
(y_L, Inf): ys is NA for them,
and y_Ls/y_Rs are NA for patients
whose death time was observed.
w_vet = as.integer(vet$trt == 2)
cens = vet$status == 0
des_vet = DesignFixediBCRD$new(n = nrow(vet), response_type = "survival",
prob_T = mean(w_vet), verbose = FALSE)
des_vet$add_all_subjects_to_experiment(vet[, c("celltype", "karno", "diagtime", "age", "prior")])
des_vet$assign_w_to_all_subjects(w_precomputed = w_vet)
des_vet$add_all_subject_responses(
ys = ifelse(cens, NA, vet$time),
y_Ls = ifelse(cens, vet$time, NA),
y_Rs = ifelse(cens, Inf, NA)
)
colSums(des_vet$draw_ws_according_to_design(3)) # each redraw treats sum(w_vet) patients
#> [1] 68 68 68A Cox model adjusted for the baseline covariates gives the log hazard ratio of the test regimen, a likelihood-ratio interval and p-value, and a randomization test.
inf_vet = InferenceSurvivalCoxPHRegr$new(des_vet, verbose = FALSE)
inf_vet$num_cores = 1L
log_hr = inf_vet$compute_estimate()
ci_vet = inf_vet$compute_lik_ratio_confidence_interval(alpha = 0.05)
log_hr; ci_vet
#> [1] 0.2899359
#> 2.5% 97.5%
#> -0.1157235 0.6979687
inf_vet$compute_lik_ratio_two_sided_pval()
#> [1] 0.1612444
inf_vet$set_seed(1)
inf_vet$compute_rand_two_sided_pval(r = 500, show_progress = FALSE)
#> [1] 0.1836327The estimated hazard ratio is 1.34, with a 95% interval from 0.89 to
2.01: the data do not distinguish the two regimens. More classes and
procedures for this response type are in
vignette("cookbook-survival").
Pitfalls
Declaring the wrong design
The design you declare has to be the one that generated
w. To see what goes wrong otherwise, analyze the multi-site
trial a second time, declaring complete randomization over all 80
patients and ignoring the sites. The unadjusted difference in means
(InferenceAllSimpleAverageDiff) shows the effect most
clearly, because it does not adjust for site.
des_wrong = DesignFixediBCRD$new(n = n, response_type = "continuous", prob_T = 0.5,
verbose = FALSE)
des_wrong$add_all_subjects_to_experiment(X)
des_wrong$assign_w_to_all_subjects(w_precomputed = trial$treated)
des_wrong$add_all_subject_responses(trial$score)
rand_p = function(d) {
i = InferenceAllSimpleAverageDiff$new(d, verbose = FALSE)
i$num_cores = 1L
i$set_seed(1)
i$compute_rand_two_sided_pval(r = 1000, show_progress = FALSE)
}
p_right = rand_p(des)
p_wrong = rand_p(des_wrong)
c(blocked_by_site = p_right, ignoring_sites = p_wrong)
#> blocked_by_site ignoring_sites
#> 0.007992008 0.179820180Both analyses use the same data and the same statistic. Declared correctly, the randomization p-value is 0.008; ignoring the sites, it is 0.18. Draws that ignore the sites can put most of the treated patients in the high-scoring site C, so the reference distribution is far wider than the one the real randomization could produce, and the test loses power. A mismatch between the declared and the real design can also make a test reject too often; either way, its p-value no longer means what it says.
Three related mistakes to check for:
-
Complete randomization with the wrong
prob_T.DesignFixediBCRDalways treatsround(n * prob_T)subjects in a redraw and does not compare that with thewyou supply. If 30 of 80 were treated and you leaveprob_T = 0.5, every redraw treats 40. Setprob_T = mean(w)and checkcolSums()of a few draws, as above. -
Bernoulli versus complete randomization. If each
subject’s assignment was an independent coin flip, use
DesignFixedBernoulliwith the coin’s probability; its redraws let the number treated vary, as it did in the real experiment. -
Data that were not randomized. If treatment was
chosen rather than randomized, no design reproduces the assignment
mechanism.
ObservationalDesignaccepts the data and supports estimates and the plain nonparametric bootstrap, but randomization inference refuses to run, and the estimate is causal only under assumptions about confounding that EDI cannot check.
Stratified and blocked experiments
-
Name the strata. For
DesignFixedBlocking, pass the stratum column(s) asstrata_cols. Character and factor columns form one block per level. A numeric column may instead be cut into quantile bins, which almost certainly does not match the real strata, so convert numeric stratum codes withfactor()first (?DesignFixedBlockinggives the exact rule). If the strata are not columns ofX, or you want full control, passm, one block ID per subject. -
Unequal block sizes.
equal_block_sizes = TRUEis the default. With strata of different sizes the design then errors, but not when the data are added, and not when the recordedwis supplied: the error comes the first time the blocks are used, at a redraw (draw_ws_according_to_design()), a bootstrap or a randomization test or interval. The estimate, the Wald interval and the jackknife run before that without complaint, so a partial analysis can look fine. Real trials rarely have equal strata; setequal_block_sizes = FALSE. -
Different allocation ratios across strata. The
design’s redraws treat
floor(N * prob_T)orceiling(N * prob_T)subjects in a block of sizeN. If the real trial used a different ratio in different strata, the redraws do not reproduce it. Checkdraw_ws_according_to_design()against the observed counts per stratum before trusting a randomization test. -
Blocks and the regression model. Randomization
tests, the bootstrap and the jackknife respect the blocks. Regression
classes such as
InferenceContinOLSdo not add block effects on their own; the blocks enter the model only if the stratum column is one of the covariates, assiteis here. If the proportion treated differs across strata, adjusting for the strata is required for an unbiased estimate; see?DesignFixedBlocking.
Where to go next
- The four-step pattern and the main design and inference classes: Get started.
- One complete analysis per response type: continuous, incidence, count, proportion, survival and ordinal.
- Which procedures to trust at your sample size: Procedure trust.
- Seeds and reproducible randomization tests: Reproducibility.