ipwpoint | R Documentation |
Estimate inverse probability weights to fit marginal structural models in a point treatment situation. The exposure for which we want to estimate the causal effect can be binomial, multinomial, ordinal or continuous. Both stabilized and unstabilized weights can be estimated.
ipwpoint(exposure, family, link, numerator = NULL, denominator,
data, trunc = NULL, ...)
exposure |
a vector, representing the exposure variable of interest. Both numerical and categorical variables can be used. A binomial exposure variable should be coded using values |
family |
is used to specify a family of link functions, used to model the relationship between the variables in |
link |
specifies the link function between the variables in |
numerator |
is a formula, specifying the right-hand side of the model used to estimate the elements in the numerator of the inverse probability weights. When left unspecified, unstabilized weights with a numerator of 1 are estimated. |
denominator |
is a formula, specifying the right-hand side of the model used to estimate the elements in the denominator of the inverse probability weights. This typically includes the variables specified in the numerator model, as well as confounders for which to correct. |
data |
is a dataframe containing |
trunc |
optional truncation percentile (0-0.5). E.g. when |
... |
are further arguments passed to the function that is used to estimate the numerator and denominator models (the function is chosen using |
For each unit under observation, this function computes an inverse probability weight, which is the ratio of two probabilities:
the numerator contains the probability of the observed exposure level given observed values of stabilization factors (usually a set of baseline covariates). These probabilities are estimated using the model regressing exposure
on the terms in numerator
, using the link function indicated by family
and link
.
the denominator contains the probability of the observed exposure level given the observed values of a set of confounders, as well as the stabilization factors in the numerator. These probabilities are estimated using the model regressing exposure
on the terms in denominator
, using the link function indicated by family
and link
.
When the models from which the elements in the numerator and denominator are predicted are correctly specified, and there is no unmeasured confounding, weighting the observations by the inverse probability weights adjusts for confounding of the effect of the exposure of interest. On the weighted dataset a marginal structural model can then be fitted, quantifying the causal effect of the exposure on the outcome of interest.
With numerator
specified, stabilized weights are computed, otherwise unstabilized weighs with a numerator of 1 are computed. With a continuous exposure, using family = "gaussian"
, weights are computed using the ratio of predicted densities. Therefore, for family = "gaussian"
only stabilized weights can be used, since unstabilized weights would have infinity variance.
A list containing the following elements:
ipw.weights |
is a vector containing inverse probability weights for each unit under observation. This vector is returned in the same order as the measurements contained in |
weights.trunc |
is a vector containing truncated inverse probability weights for each unit under observation. This vector is only returned when |
call |
is the original function call to |
num.mod |
is the numerator model, only returned when |
den.mod |
is the denominator model. |
Currently, the exposure
variable and the variables used in numerator
and denominator
should not contain missing values.
Willem M. van der Wal willem@vanderwalresearch.com, Ronald B. Geskus rgeskus@oucru.org
Cole, S.R. & Hernán, M.A. (2008). Constructing inverse probability weights for marginal structural models. American Journal of Epidemiology, 168(6), 656-664.
Robins, J.M., Hernán, M.A. & Brumback, B.A. (2000). Marginal structural models and causal inference in epidemiology. Epidemiology, 11, 550-560.
Van der Wal W.M. & Geskus R.B. (2011). ipw: An R Package for Inverse Probability Weighting. Journal of Statistical Software, 43(13), 1-23. \Sexpr[results=rd]{tools:::Rd_expr_doi("10.18637/jss.v043.i13")}.
basdat
, haartdat
, ipwplot
, ipwpoint
, ipwtm
, timedat
, tstartfun
.
#Simulate data with continuous confounder and outcome, binomial exposure.
#Marginal causal effect of exposure on outcome: 10.
n <- 1000
simdat <- data.frame(l = rnorm(n, 10, 5))
a.lin <- simdat$l - 10
pa <- exp(a.lin)/(1 + exp(a.lin))
simdat$a <- rbinom(n, 1, prob = pa)
simdat$y <- 10*simdat$a + 0.5*simdat$l + rnorm(n, -10, 5)
simdat[1:5,]
#Estimate ipw weights.
temp <- ipwpoint(
exposure = a,
family = "binomial",
link = "logit",
numerator = ~ 1,
denominator = ~ l,
data = simdat)
summary(temp$ipw.weights)
#Plot inverse probability weights
graphics.off()
ipwplot(weights = temp$ipw.weights, logscale = FALSE,
main = "Stabilized weights", xlim = c(0, 8))
#Examine numerator and denominator models.
summary(temp$num.mod)
summary(temp$den.mod)
#Paste inverse probability weights
simdat$sw <- temp$ipw.weights
#Marginal structural model for the causal effect of a on y
#corrected for confounding by l using inverse probability weighting
#with robust standard error from the survey package.
require("survey")
msm <- (svyglm(y ~ a, design = svydesign(~ 1, weights = ~ sw,
data = simdat)))
coef(msm)
confint(msm)
## Not run:
#Compute basic bootstrap confidence interval .
#require(boot)
#boot.fun <- function(dat, index){
# coef(glm(
# formula = y ~ a,
# data = dat[index,],
# weights = ipwpoint(
# exposure = a,
# family = "gaussian",
# numerator = ~ 1,
# denominator = ~ l,
# data = dat[index,])$ipw.weights))[2]
# }
#bootres <- boot(simdat, boot.fun, 499);bootres
#boot.ci(bootres, type = "basic")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.