From a6b3fdf14e3fdc13d73003a52847c3a3027b0ed3 Mon Sep 17 00:00:00 2001 From: ielbadisy Date: Sat, 14 Nov 2026 16:27:32 +0000 Subject: [PATCH] Give estimate() a correct standard error and keep bootstrap replicates --- R/estimate.R | 92 +++++++++++++++++++++++++++++----- man/estimate.Rd | 16 ++++-- tests/testthat/test-estimate.R | 43 ++++++++++++++++ 3 files changed, 135 insertions(+), 16 deletions(-) diff --git a/R/estimate.R b/R/estimate.R index 5e7c977..4f3124c 100644 --- a/R/estimate.R +++ b/R/estimate.R @@ -12,12 +12,22 @@ #' @param control_level Optional control level for binary treatment. #' @param spec Hyperparameter list passed to `fit()`. #' @param type Prediction type override for the outcome model. -#' @param interval Interval method: `"normal"` or `"bootstrap"`. +#' @param interval Interval method: `"normal"` or `"bootstrap"`. The standard +#' error accounts for estimation of the outcome model. With `model = "glm"` +#' and `interval = "normal"` it is a delta-method standard error of the +#' g-computation contrast (plus, for ATE and ATT, the sampling variance of +#' the averaged unit-level effects). For other learners, and for +#' `interval = "bootstrap"`, it is the standard deviation of `n_boot` +#' nonparametric bootstrap estimates, each refitting the outcome model; +#' `"normal"` then uses estimate +/- z * SE and `"bootstrap"` uses +#' percentile limits. #' @param conf_level Confidence level for uncertainty intervals. -#' @param n_boot Number of bootstrap resamples used when `interval = "bootstrap"`. +#' @param n_boot Number of bootstrap resamples used when the standard error +#' or interval is bootstrapped (see `interval`). #' A resample with no treated or no control rows is redrawn, and a resample #' whose fit fails is dropped with a warning (an error if more than half -#' fail). The counts are returned in `$bootstrap`. +#' fail). The counts, the replicate estimates (`estimates`) and their +#' standard deviation (`std_error`) are returned in `$bootstrap`. #' @param seed Optional seed. #' @param fit Optional preconfigured `funcml_fit` object. #' @param ... Passed to `fit()`. @@ -111,9 +121,10 @@ estimate <- function(data, formula, model = NULL, treatment = NULL, ) boot_info <- attr(ci, "bootstrap") + std_error <- attr(ci, "std_error") %||% est_core$std_error out <- list( estimate = est_core$estimate, - std_error = est_core$std_error, + std_error = std_error, conf_int = stats::setNames(as.numeric(ci), c("lower", "upper")), conf_level = conf_level, interval_method = interval, @@ -230,7 +241,8 @@ estimate <- function(data, formula, model = NULL, treatment = NULL, list( estimate = if (estimand == "IATE") NA_real_ else stats::weighted.mean(ite, w = weights), - std_error = if (estimand == "IATE") NA_real_ else .weighted_se(ite, weights), + std_error = if (estimand == "IATE") NA_real_ else + .gcomp_delta_se(base_fit, cf_treated, cf_control, weights, ite, estimand), effects = data.frame( row_id = seq_along(ite), effect = ite, @@ -249,9 +261,11 @@ estimate <- function(data, formula, model = NULL, treatment = NULL, return(stats::setNames(c(NA_real_, NA_real_), c("lower", "upper"))) } - if (interval == "normal") { - crit <- stats::qnorm(1 - alpha / 2) - return(stats::setNames(estimate_value + c(-1, 1) * crit * se_value, c("lower", "upper"))) + crit <- stats::qnorm(1 - alpha / 2) + if (interval == "normal" && is.finite(se_value)) { + ci <- stats::setNames(estimate_value + c(-1, 1) * crit * se_value, c("lower", "upper")) + attr(ci, "std_error") <- se_value + return(ci) } boot_vals <- .bootstrap_estimand( @@ -267,15 +281,23 @@ estimate <- function(data, formula, model = NULL, treatment = NULL, fit_obj = fit_obj, n_boot = n_boot ) - ci <- stats::setNames( - as.numeric(stats::quantile(boot_vals, probs = c(alpha / 2, 1 - alpha / 2), na.rm = TRUE, names = FALSE)), - c("lower", "upper") - ) + boot_se <- stats::sd(boot_vals, na.rm = TRUE) + ci <- if (interval == "normal") { + stats::setNames(estimate_value + c(-1, 1) * crit * boot_se, c("lower", "upper")) + } else { + stats::setNames( + as.numeric(stats::quantile(boot_vals, probs = c(alpha / 2, 1 - alpha / 2), na.rm = TRUE, names = FALSE)), + c("lower", "upper") + ) + } + attr(ci, "std_error") <- boot_se attr(ci, "bootstrap") <- list( n_boot = length(boot_vals), n_used = sum(!is.na(boot_vals)), n_failed = attr(boot_vals, "n_failed"), - n_redrawn = attr(boot_vals, "n_redrawn") + n_redrawn = attr(boot_vals, "n_redrawn"), + estimates = as.numeric(boot_vals), + std_error = boot_se ) ci } @@ -341,6 +363,50 @@ estimate <- function(data, formula, model = NULL, treatment = NULL, vals } +# Delta-method standard error of the g-computation contrast for a `glm` +# outcome model. The contrast is sum_i w_i (mu1_i - mu0_i) with normalized +# weights, mu = linkinv(x' beta). Its gradient in beta is +# sum_i w_i (mu.eta(eta1_i) x1_i - mu.eta(eta0_i) x0_i), so the model part of +# the variance is g' V g with V = vcov(beta). For ATE and ATT a second term, +# the variance of the weighted mean of the unit-level effects, accounts for +# averaging over the sampled covariates (asymptotically uncorrelated with +# beta). CATE averages over fixed target rows and keeps the model term only. +# Returns NA for learners other than glm; the caller then bootstraps. +.gcomp_delta_se <- function(base_fit, cf_treated, cf_control, weights, ite, estimand) { + g <- base_fit$state$state + if (!identical(base_fit$model, "glm") || !inherits(g, "glm")) { + return(NA_real_) + } + res <- tryCatch({ + beta <- stats::coef(g) + keep <- !is.na(beta) + tt <- stats::delete.response(stats::terms(g)) + design <- function(newdata) { + X <- .encode_new(base_fit, newdata, na_action = base_fit$na_action) + mm <- stats::model.matrix(tt, data = data.frame(X)) + mm[, keep, drop = FALSE] + } + mm1 <- design(cf_treated) + mm0 <- design(cf_control) + b <- beta[keep] + mu_eta <- stats::family(g)$mu.eta + w <- as.numeric(weights) / sum(weights) + d1 <- mu_eta(as.numeric(mm1 %*% b)) + d0 <- mu_eta(as.numeric(mm0 %*% b)) + grad <- colSums(w * (d1 * mm1 - d0 * mm0)) + V <- stats::vcov(g)[names(b), names(b), drop = FALSE] + v_model <- as.numeric(crossprod(grad, V %*% grad)) + v_x <- if (estimand %in% c("ATE", "ATT")) { + se_x <- .weighted_se(ite, weights) + if (is.finite(se_x)) se_x^2 else 0 + } else { + 0 + } + sqrt(v_model + v_x) + }, error = function(e) NA_real_) + if (!is.finite(res)) NA_real_ else res +} + .weighted_se <- function(x, w) { w <- as.numeric(w) w <- w / sum(w) diff --git a/man/estimate.Rd b/man/estimate.Rd index 27a4efc..391bc45 100644 --- a/man/estimate.Rd +++ b/man/estimate.Rd @@ -46,14 +46,24 @@ is treated as the treatment variable unless \code{treatment} is supplied.} \item{type}{Prediction type override for the outcome model.} -\item{interval}{Interval method: \code{"normal"} or \code{"bootstrap"}.} +\item{interval}{Interval method: \code{"normal"} or \code{"bootstrap"}. The standard +error accounts for estimation of the outcome model. With \code{model = "glm"} +and \code{interval = "normal"} it is a delta-method standard error of the +g-computation contrast (plus, for ATE and ATT, the sampling variance of +the averaged unit-level effects). For other learners, and for +\code{interval = "bootstrap"}, it is the standard deviation of \code{n_boot} +nonparametric bootstrap estimates, each refitting the outcome model; +\code{"normal"} then uses estimate +/- z * SE and \code{"bootstrap"} uses +percentile limits.} \item{conf_level}{Confidence level for uncertainty intervals.} -\item{n_boot}{Number of bootstrap resamples used when \code{interval = "bootstrap"}. +\item{n_boot}{Number of bootstrap resamples used when the standard error +or interval is bootstrapped (see \code{interval}). A resample with no treated or no control rows is redrawn, and a resample whose fit fails is dropped with a warning (an error if more than half -fail). The counts are returned in \verb{$bootstrap}.} +fail). The counts, the replicate estimates (\code{estimates}) and their +standard deviation (\code{std_error}) are returned in \verb{$bootstrap}.} \item{seed}{Optional seed.} diff --git a/tests/testthat/test-estimate.R b/tests/testthat/test-estimate.R index e16a4eb..d3a6910 100644 --- a/tests/testthat/test-estimate.R +++ b/tests/testthat/test-estimate.R @@ -105,3 +105,46 @@ test_that("estimate() reports positivity problems and survives degenerate bootst expect_true(res$bootstrap$n_redrawn >= 0L) expect_equal(res$bootstrap$n_failed, 0L) }) + +test_that("glm g-computation ATE has a delta-method SE matching lm", { + set.seed(2) + n <- 2000 + x <- rnorm(n) + a <- rbinom(n, 1, plogis(x)) + y <- 1 + 0.5 * a + x + rnorm(n) + d <- data.frame(y, a = factor(a), x) + est <- estimate(d, y ~ a + x, model = "glm", treatment = "a", estimand = "ATE") + lm_se <- summary(stats::lm(y ~ a + x, data = d))$coefficients["a1", "Std. Error"] + expect_equal(est$std_error, lm_se, tolerance = 1e-6) + expect_gt(diff(est$conf_int), 0.15) +}) + +test_that("glm delta-method SE agrees with the bootstrap for a binary outcome", { + set.seed(3) + n <- 800 + x <- rnorm(n) + a <- rbinom(n, 1, plogis(0.5 * x)) + y <- factor(rbinom(n, 1, plogis(-0.3 + 0.8 * a + x)), labels = c("no", "yes")) + d <- data.frame(y, a, x) + est <- estimate(d, y ~ a + x, model = "glm", treatment = "a", estimand = "ATE") + boot <- estimate(d, y ~ a + x, model = "glm", treatment = "a", estimand = "ATE", + interval = "bootstrap", n_boot = 200, seed = 4) + expect_gt(est$std_error, 0.02) + expect_equal(est$std_error, boot$std_error, tolerance = 0.25) + expect_length(boot$bootstrap$estimates, 200) + expect_equal(boot$std_error, stats::sd(boot$bootstrap$estimates)) +}) + +test_that("non-glm learners get a bootstrap SE under the normal interval", { + set.seed(6) + n <- 150 + x <- rnorm(n) + a <- rbinom(n, 1, 0.5) + y <- 1 + a + x + rnorm(n) + d <- data.frame(y, a, x) + est <- estimate(d, y ~ a + x, model = "rpart", treatment = "a", estimand = "ATE", + n_boot = 20, seed = 1) + expect_gt(est$std_error, 0) + expect_length(est$bootstrap$estimates, 20) + expect_equal(unname(est$conf_int), est$estimate + c(-1, 1) * stats::qnorm(0.975) * est$std_error) +})