Using `WeightIt` R package for causal inference

Elena Dudukina
2 min readJul 21, 2022
Photo by Marten Newhall on Unsplash

I recently discovered WeightIt R package and was very happy with its functionality and performance. I “delegated” my code computing IPTW to WeightIt and it was faster while producing the same results, as expected. In this post I will use publicly available data to demonstrate the great functionality of WeightIt and the scope of challenges it may help address.

First, lets’s load the data for the analysis.

Next, I will re-use the code by Joy Shi and Sean McGrath available here to recode some variables in the data set. You can find the exact code snippet in this blog post.

Now we can check some baseline descriptive stats on the covariables using standardized mean difference (SMD) as a measure of balance/imbalance between exposed and unexposed.

Let’s go on and compute the inverse probability of treatment weights for the point treatment qsmk.

Next, we can investigate how much we improved the exchangeability between exposed and unexposed by re-weighting the original population using computed IPTWs.

An important covariable smokeintensity remained unbalanced; however, there is a clear exchangeability improvement (less confounding) in the re-weighted population vs the original one (sex, age, physical activity variables and previous weight wt71).

Next, we run a weighted Cox proportional hazards regression model using computed weights.

A spurious confounded association indicating a 39% increased relative risk for smoking quitting disappeared after IPT weighting for multiple confounding factors showing the hazard ratio of unity.

Now, only one thing is missing and that is a 95% CI that takes into account the uncertainty about weights and the point estimate itself — I will use bootstrapping with the percentile method.

Voila!

The full code, output and an additional example of applying IPT weighting to time-varying exposure and confounding (see DAG) with simulated data set live in my primary blog here.

Happy coding ✌️

References:

  1. Hernán MA, Robins JM (2020). Causal Inference: What If. Boca Raton: Chapman & Hall/CRC
  2. R code by Joy Shi and Sean McGrath available here
  3. WeightIt R package: https://cran.r-project.org/web/packages/WeightIt/index.html
  4. Austin, Peter C. 2011. “An Introduction to Propensity Score Methods for Reducing the Effects of Confounding in Observational Studies.” Multivariate Behavioral Research 46 (3): 399–424. https://doi.org/10.1080/00273171.2011.568786.
  5. Austin, Peter C., and Elizabeth A. Stuart. 2015. “Moving Towards Best Practice When Using Inverse Probability of Treatment Weighting (IPTW) Using the Propensity Score to Estimate Causal Treatment Effects in Observational Studies.” Statistics in Medicine 34 (28): 3661–79. https://doi.org/10.1002/sim.6607

--

--

Elena Dudukina

I do research in reproductive health and write about pharmacoepidemiology, drug utilization & safety, & Rstats. Primary blog https://www.elenadudukina.com