This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 ---- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |