| ipw | R Documentation |
ipw() is a bring-your-own-model (BYOM) inverse probability weighted
estimator for causal inference. You supply a fitted propensity score model
and a fitted weighted outcome model; ipw() computes causal effect estimates
with standard errors that correctly account for the two-step estimation
process.
ipw() currently supports binary exposures with binary or continuous
outcomes. For binary outcomes, it returns the risk difference, log risk
ratio, and log odds ratio. For continuous outcomes, it returns the difference
in means.
ipw(
ps_mod,
outcome_mod,
.data = NULL,
estimand = NULL,
ps_link = NULL,
conf_level = 0.95
)
## S3 method for class 'ipw'
as.data.frame(x, row.names = NULL, optional = NULL, exponentiate = FALSE, ...)
ps_mod |
A fitted propensity score model of class |
outcome_mod |
A fitted weighted outcome model of class |
.data |
A data frame containing the exposure, outcome, and covariates.
If |
estimand |
A character string specifying the causal estimand: |
ps_link |
A character string specifying the link function used in the
propensity score model: |
conf_level |
Confidence level for intervals. Default is |
x |
An |
row.names, optional, ... |
Passed to |
exponentiate |
If |
An S3 object of class ipw with the following components:
estimandThe causal estimand: one of "ate", "att", "ato",
or "atm".
ps_modThe fitted propensity score model.
outcome_modThe fitted outcome model.
estimatesA data frame with one row per effect measure and the
following columns: effect (the measure name), estimate (point
estimate), std.err (standard error), z (z-statistic),
ci.lower and ci.upper (confidence interval bounds),
conf.level, and p.value.
as.data.frame() returns the estimates component as a data
frame. When exponentiate = TRUE, the log(rr) and log(or) rows are
transformed: point estimates and confidence limits are exponentiated and
the effect labels become "rr" and "or".
ipw() is designed around a three-step workflow:
Fit a propensity score model (e.g., logistic regression of exposure on confounders).
Calculate propensity score weights for your estimand (e.g., wt_ate())
and fit a weighted outcome model.
Pass both models to ipw() to obtain causal effect estimates with
correct standard errors.
You are responsible for specifying and fitting both models. ipw() handles
the variance estimation.
For binary outcomes (stats::glm() with family = binomial()), ipw()
returns three effect measures:
rd: Risk difference (marginal risk in exposed minus unexposed)
log(rr): Log risk ratio
log(or): Log odds ratio
For continuous outcomes (stats::lm() or stats::glm() with
family = gaussian()), only the difference in means (diff) is returned.
Use as.data.frame() with exponentiate = TRUE to obtain risk ratios and
odds ratios on their natural scale.
Standard errors are computed via linearization, which correctly accounts for the uncertainty introduced by estimating propensity scores. This avoids the known problem of underestimated standard errors that arises from treating estimated weights as fixed. See Kostouraki et al. (2024) for details.
Kostouraki A, Hajage D, Rachet B, et al. On variance estimation of the inverse probability-of-treatment weighting estimator: A tutorial for different types of propensity score weights. Statistics in Medicine. 2024;43(13):2672–2694. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.1002/sim.10078")}
wt_ate(), wt_att(), wt_atm(), wt_ato() for calculating propensity
score weights.
ps_trim(), ps_trunc() for handling extreme propensity scores before
weighting.
# Simulate data with a confounder, binary exposure, and binary outcome
set.seed(123)
n <- 200
x1 <- rnorm(n)
z <- rbinom(n, 1, plogis(0.5 * x1))
y <- rbinom(n, 1, plogis(-0.5 + 0.8 * z + 0.3 * x1))
dat <- data.frame(x1, z, y)
# Step 1: Fit a propensity score model
ps_mod <- glm(z ~ x1, data = dat, family = binomial())
# Step 2: Calculate ATE weights and fit a weighted outcome model
wts <- wt_ate(ps_mod)
outcome_mod <- glm(y ~ z, data = dat, family = binomial(), weights = wts)
# Step 3: Estimate causal effects with correct standard errors
result <- ipw(ps_mod, outcome_mod)
result
# Exponentiate log-RR and log-OR to get RR and OR
as.data.frame(result, exponentiate = TRUE)
# Continuous outcome example
y_cont <- 2 + 0.8 * z + 0.3 * x1 + rnorm(n)
dat$y_cont <- y_cont
outcome_cont <- lm(y_cont ~ z, data = dat, weights = wts)
ipw(ps_mod, outcome_cont)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.