Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / ppc_check_frequentist.R
Created April 23, 2026 17:44
Posterior Predictive Checks for Causal Inference
# Title: Posterior Predictive Checks for Bayesian & Frequentist Models
# Description: When fitting a model, we need to check assumptions. In a Bayesian framework,
# one excellent tool is the posterior predictive check. A similar thing can be done with
# frequentist models by simulating data with the model then comparing to the observed data
# This code demonstrates that and how it's useful
# Note: This code was created with the help of Claude Sonnet 4.5 and reviewed by me (R Batten)
@battenr
battenr / ato_target_pop.R
Created April 13, 2026 15:20
Average Treatment Effect in the Overlap - Target Population
# Title: Average Treatment Effect in the Overlap
# Description: The average treatment effect in the overlap (ATO) can be a tremendously helpful
# causal estimand. However, it can be tricky to define because it's hard to identify
# before an analysis.
# This code demonstrates what the target population is for the ATO using code
# Note: Claude was used to help with parts of this code, in particular the formatting of the graphic.
@battenr
battenr / bca_ate_rr.R
Created April 9, 2026 17:52
Bias-corrected and accelerated bootstrap interval
# Title: Bias-corrected and Accelerated Bootstrap Interval for Causal Inference
# Description: Sometimes the confidence interval can be tricky to estimate for
# causal inference. In particular, there may be situations where we can't simply estimate
# it with a formula.
# In these cases, bootstrapping can help. However, when the sampling distribution
# is skewed or biased this can cause problems. The BCa interval can help by accounting
# for this bias and skewness. This code demonstrates how the BCa bootstrap
# interval works with an example.
@battenr
battenr / dichot_cont_confounder.R
Created March 19, 2026 17:00
Dichotomizing Continuous Confounder - Just Don't
# Title: Why Dichotomizing a Continuous Confounder is a Bad Idea
# Description: Dichotomizing a continuous confounder is a bad idea.
# This code demonstrates why it's a bad idea, and how it can lead to
# bias.
# Setup ----
#... Packages ----
@battenr
battenr / causal_estimands_po.R
Created March 9, 2026 13:21
Causal Estimands using Potential Outcomes
# Title: Plotting Causal Estimands using Potential Outcomes
# Description: Understanding different causal estimands is a key part of causal inference.
# Using the potential outcomes framework can help with this!
# This code simulates data under the following conditions:
# Research Question: Does coffee cause happiness?
# Three variables:
@battenr
battenr / msm_vs_iptw.R
Created March 2, 2026 16:43
Marginal Structural Models for Time-Varying Confounding
# Title: Marginal Structural Models for Time-Varying Confounding
# Description: Time-varying confounding can be tricky. If it's not properly accounted
# for the results can be misleading.
# This code demonstrates how to fit a marginal structural model, using
# simulated data and the WeightIt package. It also demonstrates how
# we can get an estimate at each time point but also an overall
# marginal effect.
@battenr
battenr / entropy_balancing.R
Created February 23, 2026 17:52
Entropy Balancing for Causal Inference
# Title: Entropy Balancing
# Description: Demonstrating how entropy balancing can be useful for causal inference.
# In particular, it can be useful when there is some overlap between the groups.
# For further reading, I highligh recommend the paper by Jans Hainmuller
# "Entropy Balancing for Causal Effects"
# Setup ----
@battenr
battenr / mixed_effects_models.R
Last active February 20, 2026 00:56
Mixed Effects Models for Causal Inference
# Title: Linear Mixed Effects Models for Causal Inference
# Description: Mixed effects models can be useful for causal inference, in particular
# when there are clusters of groups that we want to account for. If we do not account
# for these clusters, we could get an incorrect result
# (specifically we need to account for the differences within and between clusters)
# Note: This code was created with the help of Gemini
# Setup ----
@battenr
battenr / ccw_method.R
Created February 9, 2026 17:52
Clone-Censor-Weight Method
# Title: Clone-Censor-Weight Method
# Description: Immortal time bias can be a problem for causal inference.
# The typical approach is to deal with this through study design. However, sometimes
# that's not possible. In particular when using secondary data.
# One method that can be useful is the clone-censor-weight method. The process of doing that is
# demonstrated below.
# Notes:
@battenr
battenr / coverage_probability.R
Created February 2, 2026 16:32
Coverage Probability - Why it's useful for causal inference
# Title: Coverage Probability
# Description: Demonstrating how coverage probability is useful for causal inference.
# This code uses the example of calculating variance based on three methods:
# a) sandwich estimator, b) M-estimation, c) bootstrapping.
# The coverage probability is calculated for each method then compared.
# Notes:
# 1. The bootstrap step may take a while to run
# 2. This code was generated with the help of Gemini