Skip to content

Commit 2ee978b

Browse files
author
Matt Secrest
committed
method_class
1 parent 70e58d3 commit 2ee978b

6 files changed

Lines changed: 47 additions & 41 deletions

File tree

R/method_DID_class.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ setup_method_DID <- function(method_name = "IPW",
6363
model_form_piA = "",
6464
model_form_mu0_rct = "",
6565
model_form_mu1_rct = "") {
66-
.validate_method_base(method_name, bootstrap_flag, bootstrap_obj)
66+
checkmate::assert_string(method_name)
67+
checkmate::assert_flag(bootstrap_flag)
68+
checkmate::assert_class(bootstrap_obj, "bootstrap_obj")
6769
checkmate::assert_string(model_form_piS)
6870
checkmate::assert_string(model_form_piA)
6971
checkmate::assert_character(model_form_mu0_ext)

R/method_SCM_class.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ setup_method_SCM <- function(method_name = "SCM",
5454
nlambda = 10,
5555
parallel = "no",
5656
ncpus = 1) {
57-
.validate_method_base(method_name, bootstrap_flag, bootstrap_obj)
57+
checkmate::assert_string(method_name)
58+
checkmate::assert_flag(bootstrap_flag)
59+
checkmate::assert_class(bootstrap_obj, "bootstrap_obj")
5860
checkmate::assert_number(lambda.min)
5961
checkmate::assert_number(lambda.max)
6062
if (lambda.max < lambda.min) {

R/method_class.R

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' method class
1+
#' method classes
22
#'
33
#' @slot method_name character.
44
#' @slot bootstrap_flag Logical indicating whether bootstrap inference is used.
@@ -24,12 +24,12 @@
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).
@@ -46,21 +46,31 @@
4646
#' @export
4747
setGeneric("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

9894
setup_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

R/method_weighting_class.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ setup_method_weighting <- function(method_name = "IPW",
7272
model_form_mu0_rct = "",
7373
model_form_mu1_rct = "") {
7474
.Deprecated("ec_ipw")
75-
.validate_method_base(method_name, bootstrap_flag, bootstrap_obj)
75+
checkmate::assert_string(method_name)
76+
checkmate::assert_flag(bootstrap_flag)
77+
checkmate::assert_class(bootstrap_obj, "bootstrap_obj")
7678
checkmate::assert_flag(optimal_weight_flag)
7779
checkmate::assert_number(wt)
7880
checkmate::assert_string(model_form_piS)

man/estimate.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/method_obj-class.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)