Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / model_diagnostics_ex-glm.R
Created September 5, 2025 14:50
Model Diagnostics - GLM
# Title: Model Diagnostics - Generalized Linear Model (GLM)
# Description: Demonstrating how we can explore model diagnostics
# using simulated data. For this specific use case, we are going
# to explore diagnostics for a generalized linear model (glm).
# Setup ----
#... Libraries ----
@battenr
battenr / ite_ate.R
Created September 4, 2025 17:45
Average and Individual Treatment Effects
# Title: Average Treatment Effects (ATE) vs Individual Treatment Effects (ITE)
# Description: Demonstrating how it's important to distinguish between individual treatment effects
# and the average treatment effect.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / splines_in_ci.R
Created September 3, 2025 13:54
Splines in Causal Inference
# Title: Splines for Causal Inference
# Description: Demonstrating how splines are helpful for causal inference.
# Specifically, they are helpful when we have a non-linear relationship.
# The goal with causal inference is to block backdoor paths. To do this, we
# adjust for confounders. We need to model the relationship between the
# confounder and exposure, between confounder and outcome or both.
# Sometimes these relationships are non-linear. So what do we do?
@battenr
battenr / step_selection_bad_proof.R
Created September 1, 2025 15:15
Stepwise Selection is a Bad Idea for Causal Inference (Proof)
# Title: Stepwise Selection is a Bad Idea for Causal Inference
# Description: Demonstrating how using stepwise selection can lead to
# a biased estimate when the goal is causal inference
# Setup ----
#... Libraries ----
library(tidyverse) # ol' faithful
@battenr
battenr / ipw_survival_curves.R
Created August 28, 2025 17:11
IP Weighted Survival Curves
# Title: Estimating Survival Curves Using IP Weights
# Description: Demonstrating how we can create survival curves with inverse
# probability weights for causal inference.
# Notes: Used the book Causal Inference: What If by Hernan & Robins and accompanying
# R code for guidance. Thanks to everyone involved for making it freely available!
# Setup ----
@battenr
battenr / ipw_different_estimands_rct_rwe.R
Created August 26, 2025 14:28
Same Method, Different Answers - Causal Estimands (RCT vs RWE)
# Title: Same Method, Different Answers - Causal Estimands
# Description: Demonstrating how you can use the same method (IPW), but end up with
# different answers. Each of them is valid, but each answer is for
# a different question.
# This code simulates data for two study designs:
# - An RCT
# - An observational study (RWE)
@battenr
battenr / survival_different_distributions.R
Created August 19, 2025 16:12
Survival Data - Different Distributions Mean Different Curves
# Title: Different Methods of Modelling Survival Data
# Description: There are a wide variety of methods than can be used, however
# we need to be careful about what assumptions we are making.
# The goal of this code is to demonstrate how survival curves can look
# different depending on different methods.
# This code simulates data based on a Weibull distribution, then
# attempts to fit different models to the data, highlighting how
@battenr
battenr / confounding_pathway.R
Last active August 6, 2025 18:17
Confounding as a Pathway
# Title: Confounding as a Pathway
# Description: Confounding is sometimes mixed with the term confounder. This
# code demonstrates how we can adjust for either variable on a confounding pathway
# to reduce bias (assuming there are no colliders)
# Setup ----
#... Libraries ----
@battenr
battenr / bootstrap_gif.R
Created July 16, 2025 16:09
Bootstrap Illustration (GIF)
# Title: Demonstrating Bootstrapping Using a Visual (gif)
# Description: Demonstrating how bootstrapping works, repeatedly resampling then
# calculating the statistic of interest.
# Note: ChatGPT was used to assist with creating this code
# Setup ----
#... Packages ----
@battenr
battenr / descendant_of_collider.R
Last active July 14, 2025 17:15
Descendant of Collider
# Title: Descendant of a Collider - To adjust or not adjust?
# Description: Should we adjust for a variable that is a descendant of a collider?
# It is commonly accepted that we should not, since it will increase bias.
# The goal of this code is to test that through simulation.
# Setup ----
#... Libraries ----