Generate Balancing Weights for Longitudinal Treatments
weightitMSM()
allows for the easy generation of balancing weights for marginal structural models for time-varying treatments using a variety of available methods for binary, continuous, and multinomial treatments. Many of these methods exist in other packages, which weightit()
calls; these packages must be installed to use the desired method. Also included are print()
and summary()
methods for examining the output.
Currently only "wide" data sets, where each row corresponds to a unit's entire variable history, are supported. You can use reshape()
or other functions to transform your data into this format; see example below.
weightitMSM(formula.list, data = NULL, method = "ps", stabilize = FALSE, by = NULL, s.weights = NULL, num.formula = NULL, moments = NULL, int = FALSE, missing = NULL, verbose = FALSE, include.obj = FALSE, is.MSM.method, weightit.force = FALSE, ...) ## S3 method for class 'weightitMSM' print(x, ...)
formula.list |
a list of formulas corresponding to each time point with the time-specific treatment variable on the left hand side and pre-treatment covariates to be balanced on the right hand side. The formulas must be in temporal order, and must contain all covariates to be balanced at that time point (i.e., treatments and covariates featured in early formulas should appear in later ones). Interactions and functions of covariates are allowed. |
data |
an optional data set in the form of a data frame that contains the variables in the formulas in |
method |
a string of length 1 containing the name of the method that will be used to estimate weights. See |
stabilize |
|
num.formula |
optional; a one-sided formula with the stabilization factors (other than the previous treatments) on the right hand side, which adds, for each time point, the stabilization factors to a model saturated with previous treatments. See Cole & Hernán (2008) for a discussion of how to specify this model; including stabilization factors can change the estimand without proper adjustment, and should be done with caution. Unless you know what you are doing, we recommend setting |
by |
a string containing the name of the variable in |
s.weights |
a vector of sampling weights or the name of a variable in |
moments |
|
int |
|
missing |
|
verbose |
whether to print additional information output by the fitting function. |
include.obj |
whether to include in the output a list of the fit objects created in the process of estimating the weights at each time point. For example, with |
is.MSM.method |
whether the method estimates weights for multiple time points all at once ( |
weightit.force |
several methods are not valid for estimating weights with longitudinal treatments, and will produce an error message if attempted. Set to |
... |
other arguments for functions called by |
x |
a |
In general, weightitMSM()
works by separating the estimation of weights into separate procedures for each time period based on the formulas provided. For each formula, weightitMSM()
simply calls weightit()
to that formula, collects the weights for each time period, and multiplies them together to arrive at longitudinal balancing weights.
Each formula should contain all the covariates to be balanced on. For example, the formula corresponding to the second time period should contain all the baseline covariates, the treatment variable at the first time period, and the time-varying covariates that took on values after the first treatment and before the second. Currently, only wide data sets are supported, where each unit is represented by exactly one row that contains the covariate and treatment history encoded in separate variables.
The "cbps"
method, which calls CBPS()
in CBPS, will yield different results from CBMSM()
in CBPS because CBMSM()
takes a different approach to generating weights than simply estimating several time-specific models.
A weightitMSM
object with the following elements:
weights |
The estimated weights, one for each unit. |
treat.list |
A list of the values of the time-varying treatment variables. |
covs.list |
A list of the covariates used in the fitting at each time point. Only includes the raw covariates, which may have been altered in the fitting process. |
data |
The data.frame originally entered to |
estimand |
"ATE", currently the only estimand for MSMs with binary or multinomial treatments. |
method |
The weight estimation method specified. |
ps.list |
A list of the estimated propensity scores (if any) at each time point. |
s.weights |
The provided sampling weights. |
by |
A data.frame containing the |
stabilization |
The stabilization factors, if any. |
Noah Greifer
Cole, S. R., & Hernán, M. A. (2008). Constructing Inverse Probability Weights for Marginal Structural Models. American Journal of Epidemiology, 168(6), 656–664. doi: 10.1093/aje/kwn164
weightit()
for information on the allowable methods.
library("twang") # library("cobalt") data("iptwExWide", package = "twang") (W <- weightitMSM(list(tx1 ~ age + gender + use0, tx2 ~ tx1 + use1 + age + gender + use0, tx3 ~ tx2 + use2 + tx1 + use1 + age + gender + use0), data = iptwExWide, method = "ps")) summary(W) # bal.tab(W) ##Going from long to wide data data("iptwExLong", package = "twang") wide_data <- reshape(iptwExLong$covariates, #long data timevar = "time", #time variable v.names = c("use", "tx"), #time-varying idvar = "ID", #time-stable direction = "wide", sep = "") (W2 <- weightitMSM(list(tx1 ~ age + gender + use1, tx2 ~ tx1 + use2 + age + gender + use1, tx3 ~ tx2 + use3 + tx1 + use2 + age + gender + use1), data = wide_data, method = "ps")) summary(W2) all.equal(W$weights, W2$weights)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.