EDI (Experimental Design and Inference) is an R package for two-arm randomized experiments. It provides randomization designs, both fixed (every subject is known before anyone is assigned) and sequential (subjects arrive and are assigned one at a time). It supports six response types, and for each design and response type it offers inference procedures that respect how treatment was actually assigned.
This page is a short tour. It shows the pattern every analysis follows, helps you pick a response type and a design, runs one small example end to end, and points you to the more detailed vignettes.
The four-step pattern
Every EDI analysis has the same four steps:
-
Construct a
Designfornsubjects and aresponse_type. - Assign treatment: all at once for a fixed design, or one subject at a time for a sequential design.
- Record responses.
-
Construct an
Inferenceclass on the completed design and call itscompute_*methods, or hand the design toInferenceSuiteto run every applicable procedure at once.
Designs and inference classes are R6 classes, so you create them with
$new() and call methods with $.
Choosing your response type
The response_type argument of every design constructor
takes one of six strings. Pick the one that matches what you record for
each subject. Each has a cookbook vignette that runs the full workflow
for that type.
response_type |
What each subject’s response looks like | A typical inference class | Cookbook |
|---|---|---|---|
"continuous" |
A real number (blood pressure, test score) | InferenceContinOLS |
vignette("cookbook-continuous") |
"incidence" |
A 0/1 outcome (event happened or not) | InferenceIncidExactFisher |
vignette("cookbook-incidence") |
"count" |
A non-negative integer (number of visits, seizures) | InferenceCountNegBin |
vignette("cookbook-count") |
"proportion" |
A fraction in (0, 1) (adherence rate, fraction of tissue affected) | InferencePropBetaRegr |
vignette("cookbook-proportion") |
"survival" |
A time to event, either exact or censored | InferenceSurvivalCoxPHRegr |
vignette("cookbook-survival") |
"ordinal" |
An ordered level coded 1, 2, ..., K (a Likert
rating) |
InferenceOrdinalPropOddsRegr |
vignette("cookbook-ordinal") |
Inference class names follow the response type:
InferenceContin*, InferenceIncid*,
InferenceCount*, InferenceProp*,
InferenceSurvival* and InferenceOrdinal*. The
classes in this table are the ones used in the tour below; each
response type has several more, and the cookbooks show other choices.
The "incidence" and "proportion" types are
different: an incidence response is a single 0 or 1 per subject, while a
proportion response is itself a fraction for each subject.
Choosing a design
A fixed design needs every subject’s covariates before assignment. A sequential design assigns each subject on arrival, using only what has been seen so far. EDI has many designs of each kind (see the reference index); a few representative ones are below.
| Design | Kind | How it assigns treatment | When to use it |
|---|---|---|---|
DesignFixedBernoulli |
Fixed | An independent coin flip per subject; the number treated is random | The simplest baseline |
DesignFixediBCRD |
Fixed | Complete randomization: exactly round(n * prob_T)
treated, every such allocation equally likely |
You want a fixed number of treated subjects |
DesignFixedBlocking |
Fixed | Forms blocks from covariates (strata_cols) and
randomizes within each block |
A few prognostic covariates, such as site or sex |
DesignFixedBinaryMatch |
Fixed | Pairs subjects by an optimal covariate matching, then randomizes within each pair | Many covariates, all subjects known up front |
DesignSeqOneByOneEfron |
Sequential | Efron’s biased coin: favors the under-represented arm (default probability 2/3); ignores covariates | Keep arm sizes balanced during enrollment |
DesignSeqOneByOneKK21 |
Sequential | Matches each arrival to a close earlier unmatched subject, with covariates weighted by their estimated association with the response | Covariate balance during sequential enrollment |
For a fixed design you call
add_all_subjects_to_experiment(),
assign_w_to_all_subjects() and
add_all_subject_responses(). For a sequential design you
call add_one_subject_to_experiment_and_assign() and
add_one_subject_response() once per subject;
vignette("cookbook-continuous") shows that loop.
A small example
EDI is on CRAN. Not evaluated here: the vignette builds inside an already-installed package.
install.packages("EDI")
# or the development version: remotes::install_github("kapelner/EDI", subdir = "R/EDI")We simulate 50 subjects with two covariates and a continuous response, and use complete randomization.
library(EDI)
#> Welcome to EDI v1.0.5
set.seed(20261117)
n = 50
X = data.frame(
age = round(rnorm(n, 45, 12)),
bmi = round(rnorm(n, 27, 4), 1)
)
true_effect = 2Steps 1 to 3: construct the design, assign treatment, record responses.
des = DesignFixediBCRD$new(n = n, response_type = "continuous", verbose = FALSE)
des$add_all_subjects_to_experiment(X)
des$assign_w_to_all_subjects()
w = des$get_w() # 0/1 treatment vector
table(w)
#> w
#> 0 1
#> 25 25
y = 10 + true_effect * w + 0.1 * X$age - 0.3 * X$bmi + rnorm(n, sd = 3)
des$add_all_subject_responses(y)Step 4: construct an inference class. InferenceContinOLS
regresses the response on treatment and the covariates. The estimate is
the coefficient on treatment, with a Wald confidence interval and
p-value.
inf = InferenceContinOLS$new(des, verbose = FALSE)
inf$num_cores = 1L
inf$compute_estimate()
#> [1] 2.831957
inf$compute_asymp_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> 1.132588 4.531327
inf$compute_asymp_two_sided_pval()
#> [1] 0.001600495The randomization test makes no distributional assumption. It redraws
treatment r times from the same design and compares the
observed statistic with that reference distribution.
set_seed() makes the result reproducible. A real analysis
would use a larger r.
inf$set_seed(1)
inf$compute_rand_two_sided_pval(r = 200, show_progress = FALSE)
#> [1] 0.009950249Every applicable analysis: InferenceSuite
InferenceSuite finds every inference class compatible
with the design and response type, runs each applicable procedure, and
prints a results table with a combined p-value. By default it runs every
method, including the resampling ones at their full replicate counts,
which takes minutes. To keep this vignette fast, the call below runs
only the asymptotic methods and limits each class to 15 seconds. Drop
the methods argument for the complete report.
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 Δ 3.47 1.00 1.17e-03 wald ok
#> Classes 1/11 [=== 9% ] Estimated Time Left: 5sAvg Δ Pooled … mean Δ 3.47 1.00 1.17e-03 wald ok
#> Classes 2/11 [====== 18% ] Estimated Time Left: 3sWilcox HL shift 3.16 1.05 2.47e-03 wald ok
#> Classes 3/11 [========= 27% ] Estimated Time Left: 2sLin ~. mean Δ 2.78 0.899 3.40e-03 wald ok
#> Classes 4/11 [============ 36% ] Estimated Time Left: 1sLin ~. mean Δ 2.78 0.899 1.09e-03 score ok
#> Classes 5/11 [============== 45% ] Estimated Time Left: 1sLin ~. mean Δ 2.78 0.899 1.09e-03 lik_ratio ok
#> Classes 6/11 [============== 54% ] Estimated Time Left: 1sOLS ~. mean Δ 2.83 0.844 1.60e-03 wald ok
#> Classes 7/11 [============== 63% == ] Estimated Time Left: 0sOLS ~. mean Δ 2.83 0.844 7.95e-04 score ok
#> Classes 8/11 [============== 72% ===== ] Estimated Time Left: 0sOLS ~. mean Δ 2.83 0.844 7.95e-04 lik_ratio ok
#> Classes 9/11 [============== 81% ======== ] Estimated Time Left: 0sMedian Regr ~. median ef… 1.78 1.29 1.75e-01 wald ok
#> Classes 10/11 [============== 90% =========== ] Estimated Time Left: 0sRobust Regr ~. mean Δ 2.40 0.860 7.65e-03 wald ok
#> Classes 11/11 [============= 100% ==============] Estimated Time Left: 0s-------------------------------------------------------------------------------------------
#> Status: Completed in 1s.
#>
#> Estimand: HL shift (1 inferences) : p = 0.00247
#> Estimand: mean Δ (9 inferences) : p = 0.00127
#> Estimand: median effect (1 inferences): p = 0.17536
#>
#> Combined evidence against the sharp null across 3 estimands
#> (11 inferences, weighting = uniform within estimand):
#> p = 0.0025The same pattern for every response type
Only the response and the inference class change between response
types. The helper below builds a complete-randomization design on the
same covariates X, assigns treatment, and records whatever
responses make_y(w) returns. A list return is passed on as
the ys, y_Ls and y_Rs arguments,
which is how censored survival responses are recorded.
fixed_design = function(response_type, make_y) {
des = DesignFixediBCRD$new(n = n, response_type = response_type, verbose = FALSE)
des$add_all_subjects_to_experiment(X)
des$assign_w_to_all_subjects()
y = make_y(des$get_w())
if (is.list(y)) do.call(des$add_all_subject_responses, y) else des$add_all_subject_responses(y)
des
}Each chunk below uses a different inference class and a different kind of procedure.
Incidence, exact test. Fisher’s exact test gives an exact p-value and an exact confidence interval for the log odds ratio.
des_inc = fixed_design("incidence", function(w) rbinom(n, 1, plogis(-0.5 + 1.2 * w)))
inf_inc = InferenceIncidExactFisher$new(des_inc, verbose = FALSE)
inf_inc$compute_exact_two_sided_pval_for_treatment_effect()
#> [1] 0.0222413
inf_inc$compute_exact_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> 0.1824241 2.8923033More in vignette("cookbook-incidence").
Count, score interval. A negative binomial regression; the score interval for the log rate ratio inverts the score test.
des_cnt = fixed_design("count", function(w) rnbinom(n, size = 2, mu = exp(1 + 0.5 * w)))
inf_cnt = InferenceCountNegBin$new(des_cnt, verbose = FALSE)
inf_cnt$num_cores = 1L
inf_cnt$compute_score_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> -0.1509842 0.6959443More in vignette("cookbook-count").
Proportion, bootstrap interval. A beta regression;
the nonparametric bootstrap resamples subjects. A real analysis would
use a larger B.
des_prp = fixed_design("proportion", function(w) {
mu = plogis(-0.3 + 0.6 * w) # mean proportion by arm
rbeta(n, 10 * mu, 10 * (1 - mu))
})
inf_prp = InferencePropBetaRegr$new(des_prp, verbose = FALSE)
inf_prp$num_cores = 1L; inf_prp$set_seed(1)
inf_prp$compute_bootstrap_confidence_interval(alpha = 0.05, B = 501, show_progress = FALSE)
#> 2.5% 97.5%
#> -0.04132792 0.59247988More in vignette("cookbook-proportion").
Survival with censoring, likelihood ratio. A Cox
model; the likelihood-ratio interval and p-value for the log hazard
ratio. Censored subjects are recorded as the interval
(y_L, Inf).
des_srv = fixed_design("survival", function(w) {
t_event = rexp(n, exp(-2 - 0.7 * w)); t_cens = rexp(n, 0.05); cens = t_cens < t_event
list(ys = ifelse(cens, NA, t_event), y_Ls = ifelse(cens, t_cens, NA), y_Rs = ifelse(cens, Inf, NA))
})
inf_srv = InferenceSurvivalCoxPHRegr$new(des_srv, verbose = FALSE)
inf_srv$num_cores = 1L
inf_srv$compute_lik_ratio_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> -0.8242297 0.7315111
inf_srv$compute_lik_ratio_two_sided_pval()
#> [1] 0.8949234More in vignette("cookbook-survival").
Ordinal, randomization test. A proportional-odds model; the randomization test redraws treatment from the design, as in the continuous example.
des_ord = fixed_design("ordinal", function(w) as.integer(cut(0.8 * w + rlogis(n), c(-Inf, -1, 0, 1, Inf))))
inf_ord = InferenceOrdinalPropOddsRegr$new(des_ord, verbose = FALSE)
inf_ord$num_cores = 1L; inf_ord$set_seed(1)
inf_ord$compute_rand_two_sided_pval(r = 200, show_progress = FALSE)
#> [1] 0.5870647More in vignette("cookbook-ordinal").
Where to go next
-
Cookbooks, one per response type, each with a fixed
and a sequential design:
vignette("cookbook-continuous"),vignette("cookbook-incidence"),vignette("cookbook-count"),vignette("cookbook-proportion"),vignette("cookbook-survival")andvignette("cookbook-ordinal"). -
Which procedures to trust at your sample size:
vignette("procedure-trust"). -
How the estimates were validated:
vignette("validation-evidence"). -
Seeds, RNG streams and parallel reproducibility:
vignette("reproducibility"). -
Writing your own designs and inference classes:
vignette("extending-edi"). -
Symbols and naming conventions:
vignette("notation-glossary"). - Try EDI without installing it: Try EDI in your browser.
- Learn statistics with EDI: Learn statistics through EDI.
