Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / ow_ps.R
Created August 29, 2024 18:06
Propensity Scores After Overlap Weighting
# Title: Overlap Weighting
# Description: A demonstration of overlap weighting using the propensity score
# Setup ----
#... Libraries ----
library(tidyverse) # ol' faithful
library(WeightIt) # for weighting
@battenr
battenr / param_dist.R
Created September 5, 2024 18:30
Demonstrating Parametric Statistics
# Title: Parametric vs Nonparametric Statistics
# Description: An example of what is actually meant by parametric with parametric statistics.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
library(glue) # for easy string formatting
@battenr
battenr / confounder_and_collider.R
Created September 19, 2024 18:03
To Adjust or Not Adjust? (Confounder and Collider)
# Title: A collider and a confounder - To adjust or not adjust?
# Description: Should we adjust for a variable if it's both a collider and a confounder?
# The idea for this came from an article I recently read - Liu et al.(2012)
# https://academic.oup.com/aje/article/176/10/938/92975#246684420
# 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 / varsel_twovar.R
Created September 26, 2024 17:58
Variable Selection - Model Misspecification (Two Variables)
# Title: Variable Selection - Model Misspecification
# Subtitle: Two Variables Included That Shouldn't Be
# Description: The idea for this came from trying to show what happens
# if the wrong variables are selected. Basically, when we use variable selection methods
# based on the p-value the results can be biased due to model misspecification. Furthermore,
# how would that look if it wasn't just one variable that was wrong...but two!
# Setup ----
@battenr
battenr / ps_weights_plot.R
Created October 3, 2024 22:10
Plotting Propensity Scores and Weights
# Title: Plotting Propensity Scores and Weights
# Description: Plotting propensity scores and weights is helpful for checking the
# entire distribution and assumptions. This code gives an example of doing that.
# Setup ----
library(tidyverse) # ol' faithful
library(WeightIt) # for using IPTW
library(patchwork) # for combining plots
@battenr
battenr / distrib_parameters.R
Last active November 7, 2024 19:31
Understanding Distributions and Parameters
# Title: Understanding Distributions and Parameters
# Description: The below plots show which parameters are needed for four different
# distributions: normal, exponential, binomial and poisson distributions.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / desc_of_collider.R
Last active November 14, 2024 23:37
Adjusting for Descendant of Collider
# Title: Conditioning on Descendant of a Collider Causes Bias
# Description: This script is intended to show that conditioning/adjusting
# for a descendant of a collider can induce bias.
# Setup ----
library(tidyverse) # ol' faithful
library(ggdag) # for creating and showing DAG
library(broom) # for tidying result of the model
@battenr
battenr / adjust_sunshine.R
Created November 14, 2024 23:35
Adjusting for Sunshine
# Title: Adjusting for Sunshine
# Description: Assessing whether adjusting for sunshine improves precision based on a question from David Okunlola on a LinkedIn Post
# Replicate 1000 times ----
# We could use one run as an example, however that is a single run. We need to do it many more times than that.
# Let's run it 1000 times. (Note: there are ways to calculate the number of runs required. For simplicity, we'll pick 1000 for now)
#... No Adjustment ----
@battenr
battenr / always_plot_data.R
Created November 21, 2024 17:31
Always Plot Your Data
# Title: Always Plot Your Data
# Description: A piece of advice I got from my supervisor was to always
# plot your data first. This is invaluable. The code below is meant to show
# one reason why
# Setup ----
#... Libraries ----
@battenr
battenr / probability_of_null.R
Created November 28, 2024 16:48
Posterior Distribution for Calculating Null
# Title: Showing what the probability of the null is given our data
# Description: Often the p-value is misinterpreted as the probability of the null.
# The goal of this post is to show what the probability of the null actually is.
# We can do this using Bayesian statistics.
# Note: the p-value is the probability of a test-statistic at least as extreme assuming
# the null distribution
# Setup ----