Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / ipw_stabilized_truncated.R
Created June 25, 2025 16:20
Stabilizing and Truncating Weights
# Title: Stabilizing and Truncating Weights
# Description: When using weights, for example IPTW, it is often helpful to
# stabilize and/or truncate them. This code is meant to demonstrate how we
# can do that, and why.
# Setup ----
#... Packages ----
@battenr
battenr / check_balance_covar_ps.R
Created June 19, 2025 17:19
Checking for Balance - Covariates AND Propensity Score
# Title: Checking Propensity Scores and Covariates
# Description: This script demonstrates that sometimes there can appear to be overlap when checking only the propensity score,
# however upon checking the covariates overlap, there is a lack of overlap.
# Setup ----
#... Libraries ----
library(tidyverse) # ol' faithful
@battenr
battenr / var_selection_confounder_strength.R
Created June 16, 2025 15:25
Variable Selection - Considering Strength of Confounder
# Title: Strength of Confounders When Choosing a Data Source
# Description: When using real-world data, an important aspect is choosing the
# dataset to use. Not all variables are always available in each data source.
# For example, eletronic health records vs claims data vs registry data each
# have strengths and benefits (one of which, is the variables available)
# Setup ----
#... Packages ----
@battenr
battenr / cause_confounder_and_outcome.R
Created June 13, 2025 14:15
Common Cause of Confounder and Outcome
# Title: Adjusting for Predictor Variable
# Description: Directed acyclic graphs (DAGs) are fantastic for variable selection.
# Sometimes there are variable that it's not clear if we should include or not.
# This code demonstrates one example: a variable that causes one of the confounders
# and the outcome.
# Both bias and relative increase in precision are explored for this situation.
# Inverse probability weighting, estimating the average treatment effect, is
# used. When adjusting for the predictor, it is only included in the outcome model
@battenr
battenr / lack_of_overlap.R
Created June 5, 2025 17:57
Lack of Overlap
# Title: What to Do If Lack of Overlap?
# Description: This code creates a figure where there is a lack of overlap
# due to leaving out a confounder.
# Setup ----
#... Libraries ----
library(tidyverse) # ol' faithful
@battenr
battenr / confounder_and_collider_butterfly_bias.R
Created June 2, 2025 15:13
Confounder and Collider (Butterfly Bias)
# Title: Collider and confounder - adjust or not adjust? (Butterfly bias)
# Description: Understanding if we should adjust for a variable that is
# both a collider and a confounder. For this example, we will use a DAG that has
# butterfly bias (a specific type of M-bias)
# The idea for this code was to keep the situation simple to start. It can be built upon
# rather easily. A few things to try altering would be the strength of the causal relationships,
# continuous vs binary vs TTE variables, more/less variables, etc.
@battenr
battenr / adjusting_for_mediator.R
Created May 26, 2025 19:01
Adjusting for Mediator
# Title: Adjusting for Mediator ----
# Description: This code demonstrates how adjusting for a mediator doesn't
# estimate the total causal effect.
# Setup ----
library(tidyverse) # ol' faithful
library(ggdag) # for drawing DAGs
library(broom) # tidying model outputs
@battenr
battenr / smds_vs_plots.R
Created May 19, 2025 15:49
Checking for Balance: SMDs & Plots
# Title: Checking Balance After IPTW: SMDs & Plots
# Description: This code demonstrates how standardized mean differences (SMDs)
# can be a useful tool to check for balance but may not show the entire picture.
# Setup ----
#... Packages ----
library(tidyverse) # ol faithful
@battenr
battenr / sample_size_calc_precision.R
Created May 7, 2025 16:54
Sample Size Using Precision
# Title: Estimating Sample Size Required Using Precision -----
# Description: Sample sizes are primarly based upon an assumed power. For some studies,
# such as observational studies with large samples, there isn't an issue with power. However,
# there may be a need to understand the required sample size (i.e., how much data to request from vendor, etc)
# In these situations, precision can be used instead!
# Setup ----
@battenr
battenr / doubly_robust_vs_ipw_vs_outcome_model.R
Created May 1, 2025 15:31
Doubly Robust Estimation - Reduced Bias
# Doubly Robust Estimation ----
# Description: Doubly robust estimation can be helpful for estimating
# causal effects. This code is demonstrating how there is reduced bias
# compared to IPTW only or using the outcome model only
# Setup ----
#... Packages ----