
Cookbook: Proportion Outcome, End to End
Source:vignettes/cookbook-proportion.Rmd
cookbook-proportion.RmdThis cookbook is part of EDI (Experimental Design and Inference), an R package for randomized experimental designs with inference matched to the design and response type.
One complete, runnable script for a proportion
outcome — a response that is itself a fraction in (0, 1) per subject
(adherence rate, fraction of tissue affected, score normalized to a 0–1
scale). This is distinct from the incidence cookbook, where each subject
contributes a single 0/1. Same four steps as every cookbook (see
vignette("cookbook-continuous")); the inference classes are
the InferenceProp* family — fractional logit
(quasi-binomial), Beta regression, and zero/one-inflated Beta for data
with exact 0s and 1s.
Setup
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")Fixed design, fractional logit
The response is generated from a Beta distribution whose mean follows
a logistic model in treatment and covariates. Fractional logit is a
quasi-likelihood and fits exact 0s and 1s as they are, so it needs no
clipping. Beta regression is different: the Beta density is zero at 0
and 1, and when any response is exactly 0 or 1,
InferencePropBetaRegr squeezes every response toward 1/2
before fitting (see ?InferencePropBetaRegr). The clip below
only keeps the simulated values strictly inside (0, 1) so that the Beta
fit later in this article uses them unchanged. For real data with exact
boundary values, use fractional logit or the zero/one-inflated class
(InferencePropZeroOneInflatedBetaRegr).
des = DesignFixedBernoulli$new(n = n, response_type = "proportion", verbose = FALSE)
des$add_all_subjects_to_experiment(X)
des$assign_w_to_all_subjects()
w = des$get_w()
mu = plogis(-0.5 + true_logit_shift * w - 0.1 * X$severity + 0.4 * X$prior)
phi = 15 # Beta precision
y = rbeta(n, mu * phi, (1 - mu) * phi)
y = pmin(pmax(y, 1e-4), 1 - 1e-4) # strictly inside (0, 1), for the Beta fit
des$add_all_subject_responses(y)
inf = InferencePropFractionalLogit$new(des, verbose = FALSE)
inf$num_cores = 1L
inf$compute_estimate() # treatment effect on the logit scale
#> [1] 0.692258
inf$compute_asymp_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> 0.4710137 0.9135023
inf$compute_asymp_two_sided_pval()
#> [1] 8.645963e-10
inf$set_seed(1)
inf$compute_rand_two_sided_pval(r = 200, show_progress = FALSE)
#> [1] 0.009950249
inf$set_seed(1)
inf$compute_bootstrap_confidence_interval(alpha = 0.05, B = 501, show_progress = FALSE)
#> 2.5% 97.5%
#> 0.4669989 0.9306843Beta regression on the same design
inf_beta = InferencePropBetaRegr$new(des, verbose = FALSE)
inf_beta$num_cores = 1L
inf_beta$compute_estimate()
#> [1] 0.7149261
inf_beta$compute_asymp_confidence_interval(alpha = 0.05)
#> 2.5% 97.5%
#> 0.4960634 0.9337889Everything at once
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/14 [ 0% ] Status: Estimating...Avg Δ mean Δ 0.145 0.0288 3.61e-06 wald ok
#> Classes 1/14 [== 7% ] Estimated Time Left: 1sAvg Δ Pooled … mean Δ 0.145 0.0287 2.91e-06 wald ok
#> Classes 2/14 [==== 14% ] Estimated Time Left: 1sWilcox HL shift 0.143 0.0307 1.45e-05 wald ok
#> Classes 3/14 [======= 21% ] Estimated Time Left: 1sBeta Regr ~. logodds m… 0.715 0.112 1.53e-10 wald ok
#> Classes 4/14 [========= 28% ] Estimated Time Left: 1sBeta Regr ~. logodds m… 0.715 0.112 2.80e-17 score ok
#> Classes 5/14 [=========== 35% ] Estimated Time Left: 1sBeta Regr ~. logodds m… 0.715 0.112 8.37e-09 lik_ratio ok
#> Classes 6/14 [============== 42% ] Estimated Time Left: 0sFractional Lo… ~. logodds m… 0.692 0.113 8.65e-10 wald ok
#> Classes 7/14 [============== 50% ] Estimated Time Left: 0sFractional Lo… ~. logodds m… 0.692 0.113 NA score ok
#> Classes 8/14 [============== 57% ] Estimated Time Left: 0sFractional Lo… ~. logodds m… 0.692 0.113 NA lik_ratio ok
#> Classes 9/14 [============== 64% == ] Estimated Time Left: 0sG Comp Avg Δ ~. mean Δ 0.158 0.0249 NA NA ok
#> Classes 10/14 [============== 71% ==== ] Estimated Time Left: 0sMedian Regr ~. median ef… 0.768 0.158 5.96e-06 wald ok
#> Classes 11/14 [============== 78% ====== ] Estimated Time Left: 0sZero One Infl… ~. logodds m… 0.715 0.112 1.53e-10 wald ok
#> Classes 12/14 [============== 85% ========= ] Estimated Time Left: 0sZero One Infl… ~. logodds m… 0.715 0.112 2.82e-17 score ok
#> Classes 13/14 [============== 92% =========== ] Estimated Time Left: 0sZero One Infl… ~. logodds m… 0.715 0.112 8.37e-09 lik_ratio ok
#> Classes 14/14 [============= 100% ==============] Estimated Time Left: 0s-------------------------------------------------------------------------------------------
#> Status: Completed in 1s.
#>
#> Estimand: HL shift (1 inferences) : p = 0.00001452
#> Estimand: logodds marginal (7 inferences): p = 0.00010000
#> Estimand: mean Δ (2 inferences) : p = 0.00010000
#> Estimand: median effect (1 inferences) : p = 0.00000596
#>
#> Combined evidence against the sharp null across 4 estimands
#> (11 inferences, weighting = uniform within estimand):
#> p = 0.0001Sequential design
des_seq = DesignSeqOneByOneKK14$new(n = n, response_type = "proportion", verbose = FALSE)
for (i in seq_len(n)) {
w_i = des_seq$add_one_subject_to_experiment_and_assign(X[i, , drop = FALSE])
mu_i = plogis(-0.5 + true_logit_shift * w_i - 0.1 * X$severity[i] + 0.4 * X$prior[i])
y_i = rbeta(1, mu_i * phi, (1 - mu_i) * phi)
des_seq$add_one_subject_response(i, min(max(y_i, 1e-4), 1 - 1e-4))
}
inf_seq = InferencePropFractionalLogit$new(des_seq, verbose = FALSE)
inf_seq$num_cores = 1L
inf_seq$compute_estimate()
#> [1] 0.9420852
inf_seq$set_seed(1)
inf_seq$compute_rand_two_sided_pval(r = 200, show_progress = FALSE)
#> [1] 0.009950249Where to go next
- Exact 0s and 1s in the data:
InferencePropZeroOneInflatedBetaRegr. - A marginal mean difference on the original 0–1 scale rather than a
logit coefficient:
InferencePropGCompMeanDiff. -
vignette("validation-evidence"): checked againstbetareg::betaregandstats::glm(family = quasibinomial).