******************************************************************************** ** Title: ITSA in Stata ** Programmer: Mark Bounthavong ** Date: 29 October 2023 ** Updated: NA ** Updated by: NA ** Notes: Hosted on Github: https://raw.githubusercontent.com/mbounthavong/STATA-programming-and-codes/master/itsa_stata ******************************************************************************** /******************************************************************************* Notes: In this dataset, there are three groups, and there are 200 subjects in each group. The amount of sleep per subjets is measured once a week across eight weekly intervals. The three groups are: - Control - Medication - Education There are two phase to the study: - Baseline phase (days 1 to 30 days) - Treatment phase (days 31 to 54 days) *******************************************************************************/ // For MAC users: clear all cd "/Users/mbounthavong/Library/CloudStorage/Dropbox/STATA references/Mitchels book - Visualing in Stata/ivrm2" use sleep_cat3pw.dta, replace // For Windows users: clear all cd "C:\Users\carn3_a5ada\Dropbox\STATA references\Mitchels book - Visualing in Stata\ivrm2" use sleep_cat3pw.dta, replace // For Windows users 2: clear all cd "C:\Users\mbounthavong\Dropbox\STATA references\Mitchels book - Visualing in Stata\ivrm2" use sleep_cat3pw.dta, replace // Inspect data describe summarize sort id obsday /* Sort by id and time */ list in 1/10 /* list the first 10 rows */ tab group, m /* Number of groups; there should be three groups */ *********************************************** // METHOD 1 - SPLINES FOR SINGLE-GROUP ITSA *********************************************** // Keep only the control group keep if group == 1 // Use mkspline to create knots. We will create a knot at obsday = 31 (index date) /* The marginal option => estimate the change in slope b/w knot2 v. knot1 */ mkspline knot1 31 knot2 = obsday, marginal // Create a variable to separate the treatment periods. gen period = . replace period = 0 if obsday < 31 /* Baseline phase */ replace period = 1 if obsday >= 31 & !missing(obsday) /* Treatment phase */ tab period, m // Linear mixed effects model for the single-group ITSA xtmixed sleep i.period c.knot1 c.knot2 || id: period knot1 knot2, covariance(unstructured) predict linear_spline *** Plot single-group ITSA graph twoway (lfit linear_spline obsday if group == 1 & period == 0, lcol(navy)) /// (lfit linear_spline obsday if group == 1 & period == 1, lcol(navy) /// xline(31) /// legend(order(1 "Before intervention" 2 "After intervention"))) margins, dydx(knot1) /* Slope before the intervention */ margins, dydx(period) /* Level change immediately after the intervention */ margins, dydx(knot2) /* Diff in slopes after and before the intervention */ /* Slope after the intervention */ margins, expression(_b[knot1] + _b[knot2]) /* Method 1 */ lincom knot1+knot2 /* Method 2 */ *********************************************** // METHOD 1 - SPLINES FOR SINGLE-GROUP ITSA *********************************************** // RELOAD DATA // For MAC users: clear all cd "/Users/mbounthavong/Library/CloudStorage/Dropbox/STATA references/Mitchels book - Visualing in Stata/ivrm2" use sleep_cat3pw.dta, replace // For Windows users: clear all cd "C:\Users\carn3_a5ada\Dropbox\STATA references\Mitchels book - Visualing in Stata\ivrm2" use sleep_cat3pw.dta, replace // For Windows users 2: clear all cd "C:\Users\mbounthavong\Dropbox\STATA references\Mitchels book - Visualing in Stata\ivrm2" use sleep_cat3pw.dta, replace // Keep only the control group keep if group == 1 // Create a variable to separate the treatment periods. gen period = . replace period = 0 if obsday < 31 /* Baseline phase */ replace period = 1 if obsday >= 31 & !missing(obsday) /* Treatment phase */ tab period, m // Linear mixed effects model for the single-group ITSA xtmixed sleep i.period c.obsday i.period#c.obsday || id: period obsday i.period#c.obsday, covariance(unstructured) predict linear_int *** Plot single-group ITSA graph twoway (lfit linear_int obsday if group == 1 & period == 0, lcol(navy)) /// (lfit linear_int obsday if group == 1 & period == 1, lcol(navy) /// xline(31) /// legend(order(1 "Before intervention" 2 "After intervention"))) margins if obsday < 31, dydx(obsday) /* Slope before the intervention */ margins, dydx(period) at(obsday = 31) /* Estimate the level change at obsday == 31 */ margins, dydx(obsday) over(period) pwcompare(effects) /* Diff between slopes after and before the intervention */ /* Slope after the intervention */ margins, expression(_b[obsday] + _b[1.period#c.obsday]) /* Method 1 */ lincom obsday + 1.period#c.obsday /* Method 2 */ margins, dydx(obsday) over(period) /* Est slopes before and after intervention */ ********************************************** // METHOD 1: MULTIPLE GROUP ITSA - SPLINES ********************************************** // Use mkspline to create knots. We will create a knot at obsday = 31 (index date) /* The marginal option => estimate the change in slope b/w knot2 v. knot1 */ mkspline knot1 31 knot2 = obsday, marginal // Create a variable to separate the treatment periods. gen period = . replace period = 0 if obsday < 31 /* Baseline phase */ replace period = 1 if obsday >= 31 & !missing(obsday) /* Treatment phase */ tab period, m // Linear mixed effects regression model xtmixed sleep i.group i.period c.knot1 c.knot2 i.group#i.period i.group#c.knot1 i.group#c.knot2 || id: period knot1 knot2, covariance(unstructured) predict linear1 /* Predict the linear estimates */ *** Plot linear estimates graph twoway (lfit linear1 obsday if group == 1 & period == 0, lcol(navy) ) /// (lfit linear1 obsday if group == 1 & period == 1, lcol(navy)) /// (lfit linear1 obsday if group == 2 & period == 0, lcol(green) lpattern(dash)) /// (lfit linear1 obsday if group == 2 & period == 1, lcol(green) lpattern(dash)) /// (lfit linear1 obsday if group == 3 & period == 0, lcol(cranberry) lpattern(shortdash)) /// (lfit linear1 obsday if group == 3 & period == 1, lcol(cranberry) lpattern(shortdash) /// xline(31) /// legend(title("Treatment groups", size(small)) size(vsmall) position(3) /// order(1 "Control" 3 "Medication" 5 "Education"))) /* Notes on the coeffients: - knot1 represents the slope for the control group - in period == 1 (baseline phase); - i.group#c.knot1 = interaction term that estimates - the diff in slopes b/w groups at period == 0 (baseline phase); - knot2 represents the change in slope for the control group - (period = 1 v. period = 0); - i.group#c.knot2 = interaction term that estimtes - the diff in slopes for each group before and - after (period = 0 v. period = 1) the index date - period coeff rep the level change for the control - at the index date - i.group#i.period rep the difference in level change - for each group compared to control */ // Difference in slopes at the baseline phase *** Test the equality of the slopes for the three groups in the period == 0 (baseline phase) contrast i.group#c.knot1 *** Estimate the slopes for the three groups in the period == 0 (baseline phase) margins, dydx(knot1) over(group) margins, dydx(knot1) over(group) pwcompare(effects) // Difference in level ("jump") at the index date ***Test the equality of the level changes between the three groups contrast i.group#i.period *** Estimate the amount of level change for each group /* Method 1: using the contrast command */ contrast period@group, pveffects nowald /* Method 2: using the margins command */ margins, dydx(period) over(group) // Differences in slopes before and after the index date *** Test the equality of the changes in slope for the three groups contrast i.group#c.knot2 *** Estimate the change in slopes for each group margins, dydx(knot2) over(group) margins, dydx(knot2) over(group) pwcompare(effects) ********************************************************* // METHOD 2: MULTIPLE GROUP ITSA - TRIPLE INTERACTIONS ********************************************************* // For MAC users: clear all cd "/Users/mbounthavong/Library/CloudStorage/Dropbox/STATA references/Mitchels book - Visualing in Stata/ivrm2" use sleep_cat3pw.dta, replace // For Windows users: clear all cd "C:\Users\carn3_a5ada\Dropbox\STATA references\Mitchels book - Visualing in Stata\ivrm2" use sleep_cat3pw.dta, replace // Generate a variable for the pre- and post-index date (obsday == 31) gen period = . replace period = 0 if obsday < 31 /* Baseline phase */ replace period = 1 if obsday >= 31 & !missing(obsday) /* Treatment phase */ tab period, m // Generate interaction terms gen obsday_period = obsday * period gen triple_int = obsday*period*group // Linear mixed effects regression model xtmixed sleep i.group c.obsday i.period i.group#c.obsday i.group#i.period c.obsday#i.period i.group#c.obsday#i.period || id: i.period obsday c.obsday#i.period, covariance(unstructured) predict linear2 /* Predict the linear estimates */ *** Plot linear estimates graph twoway (lfit linear2 obsday if group == 1 & period == 0, lcol(navy) ) /// (lfit linear2 obsday if group == 1 & period == 1, lcol(navy)) /// (lfit linear2 obsday if group == 2 & period == 0, lcol(green) lpattern(dash)) /// (lfit linear2 obsday if group == 2 & period == 1, lcol(green) lpattern(dash)) /// (lfit linear2 obsday if group == 3 & period == 0, lcol(cranberry) lpattern(shortdash)) /// (lfit linear2 obsday if group == 3 & period == 1, lcol(cranberry) lpattern(shortdash) /// xline(31) /// legend(title("Treatment groups", size(small)) size(vsmall) position(3) /// order(1 "Control" 3 "Medication" 5 "Education"))) // Difference in slopes at the baseline phase *** Test the equality of the slopes for the three groups in the period == 0 (baseline phase) contrast i.group#c.obsday *** Estimate the slopes for the three groups in the period == 0 (baseline phase) margins if period == 0, dydx(obsday) over(group) /* Method 1 */ margins group, dydx(obsday) at(period = 0) /* Method 2 */ margins if period == 0, dydx(obsday) over(group) pwcompare(effects) /* Method 1 */ margins group, dydx(obsday) at(period = 0) pwcompare(effects) /* Method 2 */ // Difference in level ("jump") at the index date ***Test the equality of the level changes between the three groups contrast i.group#i.period *** Estimate the amount of level change for each group /* Using the margins command */ margins if obsday == 31, dydx(period) over(group) // Differences in slopes before and after the index date *** Test the equality of the changes in slope for the three groups contrast i.group#c.obsday#i.period *** Estimate the change in slopes for each group margins group, dydx(obsday) at(period = (0 1)) pwcompare(effects) /* Method 1 */ margins, dydx(obsday) over(group period) pwcompare(effects) /* Method 2 */ margins group, dydx(obsday) over(period) pwcompare(effects) /* Method 3 */ contrast i.group#c.obsday#i.period, effects /* difference-in-differences */ contrast ar.group#c.obsday#i.period, effects /* difference-in-differences */ contrast b2.group#c.obsday#i.period, effects /* difference-in-differences */