Skip to content

Instantly share code, notes, and snippets.

View battenr's full-sized avatar

Ryan Batten battenr

View GitHub Profile
@battenr
battenr / prob_of_censor
Created April 8, 2024 13:38
Calculating Probability of Censoring
# Title: Calculating Probability of Censoring
# Description: Inverse probability of censoring weighting (IPCW) can be used to remove the potential selection bias
# caused by censoring. There are different ways to calculate the probability of censoring. The goal of this code was
# to examine whether the two methods produced the same result.
# Setup ----
#... Libraries
@battenr
battenr / demo_clt.R
Created April 11, 2024 16:49
Demonstrating the Central Limit Theorem
# Title: Demonstration of the Central Limit Theorem
# Description: The purpose of this script is to simulate data for three different
# distributions (normal, logistic and poisson) for four different sample sizes.
# Each of these is then plotted to demonstrate how the sampling distribution
# changes with increasing sizes of the sample.
# Setup ----
@battenr
battenr / collider_bias.R
Last active November 17, 2025 21:28
Proving Collider Bias
# Title: Conditioning on a Collider
# Description: Directed acyclic graphs (DAGs) are a great tool that can be
# used to guide variable selection. Colliders, a variable where two arrowheads meet,
# should not be adjusted for since they can induce bias. This code aims to prove
# and quantify the bias using a simple example.
# Setup ----
#... Libraries ----
# Title: Plotting Distributions for PS Methods
# Description: An example of plotting the distributions before and after
# IPTW of two confounders.
# Setup ----
#... Libraries ----
library(tidyverse) # ol faithful
@battenr
battenr / power_demo
Created May 9, 2024 17:14
Demonstration of Power at Different Effect Sizes and Sample Sizes
# Title: Demonstration of How Power Can Vary
# Description: The purpose of this script is to show how power can vary
# based on effect size and sample size. Power is calculated for several scenarios
# then plotted to demonstrate the importance of power/sample size.
# Setup ----
library(tidyverse) # ol faithful
@battenr
battenr / demo_se.R
Created May 16, 2024 16:35
Demonstrating Standard Error
# Title: Demonstration of Standard Error
# Description: The purpose of this script is to demonstrate standard error.
# A sample is repeatedly drawn then the mean is calculated. The result, each
# sample mean is then plotted
# Setup ----
library(tidyverse) # ol faithful
@battenr
battenr / p_value.R
Last active July 7, 2025 14:57
Demonstrating What a P-Value Is
# Title: Demonstration of P-Value
# Description: The purpose of this script is to demonstrate what a
# p-value is. A logistic regression was used and the difference in deviances for
# including the main effect in the model vs not (aka Likelihood Ratio Test).
# Setup ----
library(tidyverse) # ol faithful
@battenr
battenr / demo_ci.R
Last active June 23, 2025 15:37
Demonstrating Confidence Intervals
# Title: Demonstration of Confidence Intervals
# Description: The purpose of this script is to simulate data to show what a
# confidence interval is. The goal is to highlight that the 95% has to do
# with us repeatedly sampling the value.
# Setup ----
library(tidyverse) # ol faithful
library(ggridges) # used for ridge plot
@battenr
battenr / demo_dichot_precision.R
Created July 4, 2024 17:38
Demonstrating Effect of Dichotomization on Precision
# Title: Dichotomization Can Reduce Precision (SE)
# Description: Demonstrating how dichotomization of a continuous covariate
# can reduce precision (showing this based on the standard error)
# Setup ----
library(tidyverse) # ol' faithful
# Parameters for the Simulation ----
@battenr
battenr / power_loss_dichot.R
Created July 10, 2024 19:41
Power Loss from Dichotomizing
# Title: Demonstration of Power Loss
# Description: Demonstrating the reduction in power by dichotomizing a covariate.
# Setup ----
library(tidyverse) # ol faithful
library(broom) # for tidying the model results
# Function for Estimating Treatment Effect ----