Created
January 25, 2026 17:54
-
-
Save mingjiphd/cbea6d70948691a9f8bbb0470c277747 to your computer and use it in GitHub Desktop.
Causal Analysis using R Interrupted Time Series Analysis
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
| This R snippets shows how to perform interrupted time series analysis on a synthetic data set using R. |
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
| #### Causal Analysis using R Interrupted Time Series | |
| ### Brief Overview of ITS | |
| # - Interrupted time series (ITS) is a quasi-experimental method used to evaluate the impact | |
| # of an intervention by analyzing an outcome measured over time before and after the | |
| # intervention occurs. | |
| # - The technique works by modeling changes in both the level and slope of the time series | |
| # that follow the intervention, estimating both immediate and long-term effects. | |
| # - ITS is valuable when randomization is not feasible and can provide strong evidence | |
| # for causal inference, but requires multiple data points on both sides of the intervention. | |
| # - Key assumptions include that the pre-intervention trend would have continued in the | |
| # absence of the intervention, and that no major confounding events coincide with | |
| # the intervention period. | |
| # - ITS is frequently used in public health, policy, and social sciences to assess population-level | |
| # changes resulting from interventions or policy changes. | |
| install.packages("its.analysis") | |
| library(its.analysis) | |
| # Generating Synthetic Data | |
| year <- c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, | |
| 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018) | |
| depv <- c(8.22, 8.19, 8.23, 8.28, 8.32, 8.39, 8.02, | |
| 7.92, 7.62, 7.23, 7.1, 7.11, 6.95, 7.36, 7.51, 7.78, 7.92, 7.81) | |
| interruption <- c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0) | |
| cov1 <- c(3.1, 3.3, 5.1, 5.2, 5.4, 4.5, 4.7, 4.9, 5.3, | |
| 5.6, 5.8, 6.0, 4.8, 5.2, 4.5, 4.6, 5.1, 4.7) | |
| # Create data frame | |
| x <- as.data.frame(cbind(year, depv, interruption, cov1)) | |
| head(x) | |
| # Run interrupted time series model | |
| itsa.model(data=x, time="year", depvar="depv", interrupt_var="interruption", | |
| alpha=0.05, bootstrap=TRUE, Reps=250) | |
| ##Interpretations | |
| ##ANOVA Table the depvar significantly associated with interrupt_var (intervention effects) | |
| ## the lag_depvar highly significant (dependency on prior data points) | |
| ##Tukey. result pairwise group comparison controlling for familywise type I error. | |
| ## diff shows direction (in this case: intervention caused a decine in depvar) | |
| ##itsa.result a string of a statement of conclusions | |
| ##Shapiro.test for normality of data; levenes.test for equal variance | |
| ##autcorr for autocorrelation of residuals | |
| # Run model with covariate and different alpha level | |
| itsa.model(data=x, time="year", depvar="depv", interrupt_var="interruption", | |
| covariates="cov1", alpha=0.1, bootstrap=TRUE, Reps=250) | |
| # Run model without plots and bootstrap.(In this case, linear regression is usually used) | |
| # Bootstrap is preferred when the data distribution is not normal or sample size is limited | |
| itsa.model(data=x, time="year", depvar="depv", interrupt_var="interruption", | |
| covariates="cov1", alpha=0.1, no.plots=TRUE, bootstrap=FALSE) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The link to the original YouTube video: https://www.youtube.com/watch?v=3-dN19Vhkiw&list=PL3OtMQSLGztmePsY5aXjUiYBrP1Sw8ml7