Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / plotting_ipw_estimands.R
Created April 30, 2025 22:18
Plotting PS for IPW - Different Causal Estimands
# Title: Propensity Score Plots Using IP Weighting ----
# Description: This code shows how different patients will receive different weights
# depending upon the causal estimand when using IP weighting. This code focuses on
# three estimands: ATE, ATT and ATU.
# Note: thanks to Andrew Heiss' blog for the inspiration for these plots and making his code available!
# Setup ----
@battenr
battenr / m-estimation.R
Created April 23, 2025 16:30
M-Estimation
# Title: M-Estimation After IPTW ----
# Description: Demonstrating M-estimation after IPTW.
# Showing how it can be helpful, and comparing to if we didn't use anything
# (in this case using the SEs from GLM)
# Note: code in this script was largely inspiried by Ross et al. (2024).
# The code supplied in that paper was used to create this code
# (the authors used a binary outcome, while this code uses a continuous outcome)
@battenr
battenr / sim_for_diagnostic_plots.R
Created April 22, 2025 17:21
Using Simulation to Understand Diagnostic Plots - Posterior Predictive Check
# Title: Understanding Diagnostic Plots Through Simulation ----
# This code uses an example to demonstrate how we can use simulation to
# better understand diagnostic plots. For this specific example, we will use the
# posterior predictive check
# Note: using default priors, etc. for this example
# Setup: ----
@battenr
battenr / table2_fallacy_example.R
Created April 17, 2025 17:29
Table 2 Fallacy
# Title: Table 2 Fallacy ----
# Description: Demonstrating the table 2 fallacy using a simple example.
# This is based on the DAG that is shown in a corresponding LinkedIn post.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / iptw_vs_msm.R
Created April 11, 2025 19:29
IPTW vs MSM for Time-Varying Confounding
# Title: Time-Varying Confounding (MSM vs IPTW)
# Description: Demonstrating how marginal structural models provide
# less biased estimates compared to using IPTW at baseline only, when
# there is time-varying confounding.
# Setup ----
#... Libraries ----
@battenr
battenr / matching_weighting.R
Last active March 24, 2025 01:48
Matching vs Weighting
# Title: Matching vs Weighting
# Description: Demonstrating how matching and weighting can estimate
# different things. IPTW can also estimate ATT. The goal is to demonstrate how they
# may give different answers
# Setup ----
#... Library ----
@battenr
battenr / doubly_robust_bias.R
Created February 13, 2025 17:43
Doubly Robust Estimation - Bias
# Title: Doubly Robust Estimation
# Description: Demonstrating how doubly robust estimation can be
# a useful tool, but has limitations. Specifically by looking at three different
# scenarios and the resulting bias.
# This script has three examples:
# 1. Both models correctly specified
# 2. One of the models correctly specified
# 3. Neither model correctly specified.
@battenr
battenr / ordbetareg_example.R
Created January 16, 2025 16:59
Ordered Beta Regression
# Title: Ordered Beta Regression
# Description: This code is to demonstrate using ordered beta regression.
# This method can be particularly helpful for continuous data with upper/lower bounds.
# For more information recommend reading Kubinec (2022)
# Setup ----
#... Packages ----
@battenr
battenr / plotting_priors.R
Created January 6, 2025 15:47
Plotting Priors
# Title: Plotting Different Priors
# Description: Choosing an appropriate prior is a key part of any Bayesian analysis.
# This code shows different priors to try and highlight which one(s) might
# be reasonable.
# Setup ----
#... Packages ----
@battenr
battenr / cfb_linearly_related.R
Created January 2, 2025 16:40
Change from Baseline - Linearly Related