Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
Collate:
'bootstrap_class.R'
'method_class.R'
'analysis_class.R'
'method_SCM_class.R'
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# rdborrow 0.0.4.0

## Breaking changes
- `setup_bootstrap()` has been removed. Bootstrap settings are now specified directly in method constructors (e.g., `ec_ipw(bootstrap = 500, bootstrap_ci_type = "perc")`). Calling `setup_bootstrap()` now raises an error with migration instructions.
- `setup_method_weighting()`, `setup_method_DID()`, and `setup_method_SCM()` have been removed. Use `ec_ipw()`, `ec_aipw()`, `did_ec_ipw()`, `did_ec_aipw()`, `did_ec_or()`, or `scm()` instead. Calling the old constructors now raises an error with migration instructions.
- `EC_IPW_OPT()`, `EC_AIPW_OPT()`, and all `legacy_DID_EC_*` / `legacy_SCM*` internal functions have been deleted.
- `did_ec_ipw()` and `did_ec_aipw()`: `trt_formula` default changed from `""` to `NULL`.
Expand Down
64 changes: 0 additions & 64 deletions R/bootstrap_class.R

This file was deleted.

30 changes: 11 additions & 19 deletions R/did_ec_aipw.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ NULL
slots = c(
ps_formula = "character",
trt_formula = "characterOrNULL",
outcome_formula = "character",
bootstrap = "numericOrNULL",
bootstrap_ci_type = "character"
outcome_formula = "character"
),
prototype = list(
method_name = "DID-EC-AIPW",
ps_formula = "",
trt_formula = NULL,
outcome_formula = "",
bootstrap = NULL,
bootstrap_ci_type = "perc"
outcome_formula = ""
)
)

Expand Down Expand Up @@ -84,12 +80,7 @@ did_ec_aipw <- function(ps_formula,
outcome_formula = outcome_formula,
bootstrap = bootstrap,
bootstrap_ci_type = bootstrap_ci_type,
method_name = "DID-EC-AIPW",
bootstrap_flag = TRUE,
bootstrap_obj = .bootstrap_obj(
replicates = bootstrap,
bootstrap_CI_type = bootstrap_ci_type
)
method_name = "DID-EC-AIPW"
)
}

Expand All @@ -99,18 +90,17 @@ setMethod("estimate", "did_ec_aipw_method", function(method, data, outcomes,
covariates, alpha = 0.05,
quiet = TRUE,
T_cross) {
Y <- as.matrix(data[, outcomes, drop = FALSE])
S <- data[[trial_status]]
A <- data[[treatment]]
df <- .build_analysis_df(data, outcomes, treatment, trial_status, covariates)
Y <- as.matrix(df[, outcomes, drop = FALSE])
S <- df$S
A <- df$A

ps_formula <- sub("^[^~]*~", paste0(trial_status, " ~"), method@ps_formula)
trt_formula <- method@trt_formula
if (!is.null(trt_formula)) {
trt_formula <- sub("^[^~]*~", paste0(treatment, " ~"), trt_formula)
}

df <- data.frame(Y, S = S, A = A, data[, covariates, drop = FALSE])

if (!quiet) cat("Running DID-EC-AIPW estimator...\n")

result <- .did_ec_aipw_core(
Expand Down Expand Up @@ -229,6 +219,8 @@ setMethod("estimate", "did_ec_aipw_method", function(method, data, outcomes,
trt_formula, outcome_formula, T_cross) {
d <- data[indices, , drop = FALSE]
Y <- as.matrix(d[, outcomes, drop = FALSE])
.did_ec_aipw_core(d, Y, d$S, d$A, T_cross, ps_formula, trt_formula,
outcome_formula)$tau
.did_ec_aipw_core(
d, Y, d$S, d$A, T_cross, ps_formula, trt_formula,
outcome_formula
)$tau
}
24 changes: 7 additions & 17 deletions R/did_ec_ipw.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@ NULL
contains = "method_DID_obj",
slots = c(
ps_formula = "character",
trt_formula = "characterOrNULL",
bootstrap = "numericOrNULL",
bootstrap_ci_type = "character"
trt_formula = "characterOrNULL"
),
prototype = list(
method_name = "DID-EC-IPW",
ps_formula = "",
trt_formula = NULL,
bootstrap = NULL,
bootstrap_ci_type = "perc"
trt_formula = NULL
)
)

Expand Down Expand Up @@ -71,12 +67,7 @@ did_ec_ipw <- function(ps_formula,
trt_formula = trt_formula,
bootstrap = bootstrap,
bootstrap_ci_type = bootstrap_ci_type,
method_name = "DID-EC-IPW",
bootstrap_flag = TRUE,
bootstrap_obj = .bootstrap_obj(
replicates = bootstrap,
bootstrap_CI_type = bootstrap_ci_type
)
method_name = "DID-EC-IPW"
)
}

Expand All @@ -86,18 +77,17 @@ setMethod("estimate", "did_ec_ipw_method", function(method, data, outcomes,
covariates, alpha = 0.05,
quiet = TRUE,
T_cross) {
Y <- as.matrix(data[, outcomes, drop = FALSE])
S <- data[[trial_status]]
A <- data[[treatment]]
df <- .build_analysis_df(data, outcomes, treatment, trial_status, covariates)
Y <- as.matrix(df[, outcomes, drop = FALSE])
S <- df$S
A <- df$A

ps_formula <- sub("^[^~]*~", paste0(trial_status, " ~"), method@ps_formula)
trt_formula <- method@trt_formula
if (!is.null(trt_formula)) {
trt_formula <- sub("^[^~]*~", paste0(treatment, " ~"), trt_formula)
}

df <- data.frame(Y, S = S, A = A, data[, covariates, drop = FALSE])

if (!quiet) cat("Running DID-EC-IPW estimator...\n")

result <- .did_ec_ipw_core(df, Y, S, A, T_cross, ps_formula, trt_formula)
Expand Down
33 changes: 12 additions & 21 deletions R/did_ec_or.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ NULL
slots = c(
outcome_formula_ext = "character",
outcome_formula_rct_ctrl = "character",
outcome_formula_rct_trt = "character",
bootstrap = "numericOrNULL",
bootstrap_ci_type = "character"
outcome_formula_rct_trt = "character"
),
prototype = list(
method_name = "DID-EC-OR",
outcome_formula_ext = "",
outcome_formula_rct_ctrl = "",
outcome_formula_rct_trt = "",
bootstrap = NULL,
bootstrap_ci_type = "perc"
outcome_formula_rct_trt = ""
)
)

Expand Down Expand Up @@ -86,12 +82,7 @@ did_ec_or <- function(outcome_formula_ext,
outcome_formula_rct_trt = outcome_formula_rct_trt,
bootstrap = bootstrap,
bootstrap_ci_type = bootstrap_ci_type,
method_name = "DID-EC-OR",
bootstrap_flag = TRUE,
bootstrap_obj = .bootstrap_obj(
replicates = bootstrap,
bootstrap_CI_type = bootstrap_ci_type
)
method_name = "DID-EC-OR"
)
}

Expand All @@ -103,11 +94,9 @@ setMethod("estimate", "did_ec_or_method", function(method, data, outcomes,
covariates, alpha = 0.05,
quiet = TRUE,
T_cross) {
Y <- as.matrix(data[, outcomes, drop = FALSE])
S <- data[[trial_status]]
A <- data[[treatment]]

df <- data.frame(Y, S = S, A = A, data[, covariates, drop = FALSE])
df <- .build_analysis_df(data, outcomes, treatment, trial_status, covariates)
S <- df$S
A <- df$A

if (!quiet) cat("Running DID-EC-OR estimator...\n")

Expand All @@ -121,7 +110,7 @@ setMethod("estimate", "did_ec_or_method", function(method, data, outcomes,

if (!quiet) cat("Running bootstrap inference...\n")

n_ole <- ncol(Y) - T_cross
n_ole <- length(outcomes) - T_cross
boot_res <- .run_bootstrap(
df = df, statistic = .did_ec_or_boot_statistic,
n_estimates = n_ole, bootstrap = method@bootstrap,
Expand All @@ -137,7 +126,7 @@ setMethod("estimate", "did_ec_or_method", function(method, data, outcomes,
point_estimates = tau,
lower_CI_boot = boot_res$lower_ci,
upper_CI_boot = boot_res$upper_ci,
row.names = paste0("tau", (T_cross + 1):ncol(Y))
row.names = paste0("tau", (T_cross + 1):length(outcomes))
)
})

Expand Down Expand Up @@ -216,6 +205,8 @@ setMethod("estimate", "did_ec_or_method", function(method, data, outcomes,
outcome_formula_rct_trt,
T_cross) {
d <- data[indices, , drop = FALSE]
.did_ec_or_core(d, d$S, d$A, T_cross, outcome_formula_ext,
outcome_formula_rct_ctrl, outcome_formula_rct_trt)$tau
.did_ec_or_core(
d, d$S, d$A, T_cross, outcome_formula_ext,
outcome_formula_rct_ctrl, outcome_formula_rct_trt
)$tau
}
21 changes: 5 additions & 16 deletions R/ec_aipw.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ NULL
slots = c(
ps_formula = "character",
outcome_formula = "character",
weight = "numericOrNULL",
bootstrap = "numericOrNULL",
bootstrap_ci_type = "characterOrNULL"
weight = "numericOrNULL"
),
prototype = list(
method_name = "EC-AIPW",
ps_formula = "",
outcome_formula = "",
weight = NULL,
bootstrap = NULL,
bootstrap_ci_type = NULL
weight = NULL
)
)

Expand Down Expand Up @@ -104,12 +100,7 @@ ec_aipw <- function(ps_formula,
weight = weight,
bootstrap = bootstrap,
bootstrap_ci_type = bootstrap_ci_type,
method_name = "EC-AIPW",
bootstrap_flag = !is.null(bootstrap),
bootstrap_obj = .bootstrap_obj(
replicates = bootstrap %||% 500L,
bootstrap_CI_type = bootstrap_ci_type %||% "perc"
)
method_name = "EC-AIPW"
)
}

Expand All @@ -126,7 +117,7 @@ setMethod("estimate", "ec_aipw_method", function(method, data, outcomes,
call. = FALSE
)
}
df <- build_analysis_df(method, data, outcomes, treatment, trial_status, covariates)
df <- .build_analysis_df(data, outcomes, treatment, trial_status, covariates)
n_time <- length(outcomes)

if (!quiet) cat("Running EC-AIPW estimator...\n")
Expand Down Expand Up @@ -185,7 +176,6 @@ setMethod("estimate", "ec_aipw_method", function(method, data, outcomes,
#' @return list with tau, borrow_weight, and model intermediates.
#' @noRd
.ec_aipw_core <- function(df, outcomes, ps_formula, outcome_formula, weight) {

# see Zhou 2024a: Def 2 (Eq 7) for point estimate, Eq 11 for optimal weight

Y <- as.matrix(df[, outcomes, drop = FALSE])
Expand Down Expand Up @@ -255,7 +245,6 @@ setMethod("estimate", "ec_aipw_method", function(method, data, outcomes,
#' @return numeric vector of standard errors (length n_time).
#' @noRd
.ec_aipw_se <- function(df, core, n_time, outcome_formula) {

# see Zhou 2024a: Theorem 4 (Eq 15 for A/B matrices, Eq 16 for variance)

S <- df$S
Expand Down Expand Up @@ -354,7 +343,7 @@ setMethod("estimate", "ec_aipw_method", function(method, data, outcomes,
#' @return numeric vector of tau estimates.
#' @noRd
.ec_aipw_boot_statistic <- function(data, indices, outcomes, covariates,
ps_formula, outcome_formula, borrow_wt) {
ps_formula, outcome_formula, borrow_wt) {
d <- data[indices, , drop = FALSE]
core <- .ec_aipw_core(d, outcomes, ps_formula, outcome_formula, borrow_wt)
core$tau
Expand Down
Loading
Loading