Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / increasing_sample_size_bias.R
Created January 29, 2026 18:57
Increasing Sample Size Doesn't Fix Bias
# Title: Increasing Sample Size Doesn't Fix Bias
# Description: Demonstrating that increasing sample size doesn't fix bias.
# Note: this code was written with the help of Gemini
# Setup ----
#.. Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / bayes_joint_vs_iptw.R
Created January 26, 2026 16:31
Bayesian Joint Model vs IPTW
# Title: Bayesian Joint Modelling Approach
# Description: Demonstrating how a Bayesian approach can be useful. For this example, we are going to model
# the outcome and the treatment at the same time.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / cca_missing_data_mechanisms.R
Created January 19, 2026 16:02
Complete Case Analysis with Different Missing Data Mechanisms
# Title: How Complete Case Analysis Impacts Results
# Description: This code demonstrates how complete case analysis can impact your results
# differently for different missing data mechanisms.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / var_selection_all_variables.R
Created December 12, 2025 19:05
Variable Selection - Why All Variable Selection is Problematic
# Title: Why Adjusting for All Variables is Problematic
# Description: It's easy to think that adjusting for all variables is helpful.
# For causal inference, it's very problematic. This code simulates data, then fits a
# generalized linear model. Bias and the Monte Carlo Standard Error of bias are calculated
# to compare results from two scenarios:
# 1. Adjusting for all variables
# 2. Adjusting for correct variables (only confounder, not mediator, not collider)
@battenr
battenr / modelling_confounding.R
Created November 25, 2025 18:08
Modelling Confounding
# Title: Modelling Confounding
# Description: Demonstrating how it's important to consider the relationship between
# variables. Directed acyclic graphs (DAGs) are helpful to identify what to
# adjust for, and what not to. However, we need to also consider the relationship
# that we are trying to model.
# This code simulates data, where there is a nonlinear relationship between
# sleep (z1) and happiness (y). The relationship is plotted, and two models fit.
# One model assuming a linear relationship (incorrectly), one model assuming
@battenr
battenr / table2_fallacy_ex.R
Created October 1, 2025 16:13
Table 2 Fallacy - Example
# Title: Table 2 Fallacy - Example ----
# Description: When interpreting results, if the goal is causal inference,
# it's important to consider pathways between the exposure and outcome.
# If the exposure of interest changes, so do the pathways. This is known as
# the Table 2 Fallacy.
# This code demonstrates the Table 2 Fallacy. Highly recommend the paper
# The table 2 fallacy: presenting and interpreting confounder and modifier coefficients
# by Westreich & Greenland
@battenr
battenr / rmst.R
Created September 18, 2025 14:41
Restricted Mean Survival Time
# Title: Restricted Mean Survival Time
# Description: When using time-to-event outcomes, a common effect measure is
# the hazard ratio. For causal inference, there are several problems with this measure
# including selection bias, and a strange interpretability.
# Luckily there are alternatives!
# One such alternative is the restricted mean survival time. This code demonstrates
# the RMST.
@battenr
battenr / truncate_weights_proof.R
Created September 15, 2025 15:20
Truncating Weights for Causal Inference
# Title: Truncating Weights for Inverse Probability Weighting
# Description: When using inverse probability weights, it is recommended to stabilize weights.
# If there are extreme weights, truncating them can help reduce variance.
# This code demonstrates the benefit of truncating the weights
# (that is setting the values > some threshold, to that threshold).
# For example, setting weights > 95th percentile to the 95th percentile
# Setup ----
@battenr
battenr / adjust_tricky_variable.R
Created September 10, 2025 14:57
Adjusting for a Tricky Variable
# Title: Adjusting for Variable for Precision ----
# Description: In causal inference, we know to adjust for confounding and avoid
# adjusting for colliders. However, what if a variable is not a confounder, collider
# or a mediator. Should we adjust?
# Note: The variable must be included in a directed acyclic graph (DAG)
# because it's a common cause of a pair of variables.
# For this example we are going to look at:
@battenr
battenr / outliers.R
Created September 8, 2025 16:01
Outliers
# Title: Outliers in Causal Inference
# Description: Outliers can cause problems. This code shows how
# we can add some data that we know are outliers (since we added them).
# Setup ----
#... Libraries ----
library(tidyverse) # ol' faithful