1- # ' method class
1+ # ' method classes
22# '
33# ' @slot method_name character.
44# ' @slot bootstrap_flag Logical indicating whether bootstrap inference is used.
2424 contains = " method_obj"
2525)
2626
27- # estimate() generic----
28-
2927# ' Run estimation for a method object
3028# '
3129# ' S4 generic that dispatches to the appropriate estimation logic
32- # ' based on the method class.
30+ # ' based on the method class. Each method subclass (e.g.,
31+ # ' \code{ec_ipw_method}, \code{did_ec_ipw_method}) implements its own
32+ # ' \code{estimate()} method containing the full estimation pipeline.
3333# '
3434# ' @param method An S4 method object (e.g., from \code{\link{ec_ipw}}).
3535# ' @param data Data frame with all subjects (RCT + external controls).
4646# ' @export
4747setGeneric ("estimate ", function(method, ...) standardGeneric("estimate"))
4848
49- # bootstrap helpers----
50-
51- # ' @noRd
52- .boot_ci_type_long <- function (short ) {
53- switch (short ,
54- norm = " normal" , bca = " bca" , stud = " student" ,
55- perc = " percent" , basic = " basic"
56- )
57- }
58-
49+ # ' Run stratified bootstrap and extract confidence intervals.
50+ # ' Shared by all method classes that support bootstrap inference.
51+ # ' Stratifies by interaction(S, A) to preserve group proportions.
52+ # ' @param df data frame with columns S (trial status) and A (treatment).
53+ # ' @param statistic function with signature (data, indices, ...) returning
54+ # ' a numeric vector of point estimates.
55+ # ' @param n_estimates number of estimates returned by statistic (length of tau).
56+ # ' @param bootstrap number of bootstrap replicates.
57+ # ' @param bootstrap_ci_type short CI type name ("perc", "bca", etc.).
58+ # ' @param alpha significance level for CIs.
59+ # ' @param ... additional arguments passed through to statistic.
60+ # ' @return list with lower_ci, upper_ci (vectors), and sd_boot (vector).
5961# ' @noRd
6062.run_bootstrap <- function (df , statistic , n_estimates , bootstrap ,
6163 bootstrap_ci_type , alpha , ... ) {
6264 group_id <- as.integer(interaction(df $ S , df $ A , drop = TRUE ))
63- ci_type_long <- .boot_ci_type_long(bootstrap_ci_type )
65+
66+ ci_type_long <- switch (
67+ bootstrap_ci_type ,
68+ norm = " normal" ,
69+ bca = " bca" ,
70+ stud = " student" ,
71+ perc = " percent" ,
72+ basic = " basic"
73+ )
6474
6575 boot_out <- boot :: boot(
6676 data = df ,
@@ -70,37 +80,25 @@ setGeneric("estimate", function(method, ...) standardGeneric("estimate"))
7080 ...
7181 )
7282
73- lower_ci <- vapply(seq_len(n_estimates ), \(i ) {
74- ci <- boot :: boot.ci(boot_out , conf = 1 - alpha ,
75- type = bootstrap_ci_type , index = i )
76- ci [[ci_type_long ]][4 ]
77- }, numeric (1 ))
78-
79- upper_ci <- vapply(seq_len(n_estimates ), \(i ) {
83+ ci_bounds <- vapply(seq_len(n_estimates ), \(i ) {
8084 ci <- boot :: boot.ci(boot_out , conf = 1 - alpha ,
8185 type = bootstrap_ci_type , index = i )
82- ci [[ci_type_long ]][5 ]
83- }, numeric (1 ))
86+ ci [[ci_type_long ]][4 : 5 ]
87+ }, numeric (2 ))
8488
8589 sd_boot <- sqrt(diag(var(boot_out $ t )))
8690
87- list (lower_ci = lower_ci , upper_ci = upper_ci , sd_boot = sd_boot )
88- }
89-
90- # validation----
91-
92- .validate_method_base <- function (method_name , bootstrap_flag , bootstrap_obj ) {
93- checkmate :: assert_string(method_name )
94- checkmate :: assert_flag(bootstrap_flag )
95- checkmate :: assert_class(bootstrap_obj , " bootstrap_obj" )
91+ list (lower_ci = ci_bounds [1 , ], upper_ci = ci_bounds [2 , ], sd_boot = sd_boot )
9692}
9793
9894setup_method <- function (method_name = " " ,
9995 bootstrap_flag = FALSE ,
10096 bootstrap_obj = .bootstrap_obj()) {
101- .validate_method_base(method_name , bootstrap_flag , bootstrap_obj )
97+ checkmate :: assert_string(method_name )
98+ checkmate :: assert_flag(bootstrap_flag )
99+ checkmate :: assert_class(bootstrap_obj , " bootstrap_obj" )
102100
103- method_obj <- .method_obj(
101+ .method_obj(
104102 method_name = method_name ,
105103 bootstrap_flag = bootstrap_flag ,
106104 bootstrap_obj = bootstrap_obj
0 commit comments