***Boris cycle hire segmented regression*** clear set mem 800m set more off capture log close log using segmented_reg.txt, replace text use boris_final.dta ************************************************* ***Creating variables for segmented regression*** ************************************************* ***London tube strike dates:** ***September 6 2010=39 for time variable** ***October 4 2010=67 for time variable** label variable start_date "Date" label variable duration_sec "Mean trip duration per day" label variable t_trip "Total number of trips per day" egen time = fill(1/95) tsset time codebook t_trip codebook duration_sec gen duration_min=duration_sec/60 codebook duration_min ***Generating slope variables**** /*To estimate the slopes for the pre-strike, strike 1 and strike 2 periods three continuous time variables were created. The time variable used to estimate the slope of the pre-strike period indicated the time elapsed in days from the start of the study period (i.e. from 1 to 95 days). The time variables used to estimate the slope of the strike 1 and strike 2 periods indicated the number of days elapsed after strikes 1 and 2 respectively./* egen time = fill(1/95) /*This is the pre-strike slope*\ generate time1 = (time - 38) replace time1 = 0 if time < 39 /*This is the strike 1 slope change*/ generate time2 = (time - 66) replace time2 = 0 if time < 67 /*This is the strike 2 slope change*/ ***Generating intercept variables*** /*To estimate the intercepts for the pre-strike, strike 1 and strike 2 periods three dichotomous variables were created. These variables were set to 1 on the days of the relevant period and zero on all other days. For example, the variable that estimated the intercept for strike 1 was set to 1 from 6 September to 3 October and zero on all other days./* generate t0_int = 1 replace t0_int = 0 if time >= 39 /*This is the pre-strike intercept (also the constant in the model)*/ generate t1_int = 1 replace t1_int = 0 if time < 39 replace t1_int = 0 if time > 66 /*This is the strike 1 intercept change*/ generate t2_int = 1 replace t2_int = 0 if time < 67 /*This is the strike 2 intercept change*/ ***************************** ***Trip count regression **** ***************************** regress t_trip time time1 time2 t1_int t2_int predict yhat1 predict res, r ******************************************* **Graphing trip count regression results*** ******************************************* twoway (scatter t_trip start_date, msymbol(o) mcolor(black))(line yhat1 start_date, sort lcolor(black) lpattern(solid)), ylabel(0(5000)25000) ytitle(Total trips per day) xtitle(Date) title(Total number of trips per day on the Boris bikes, color(black)) legend(off) xline(18511 18539) graphregion(color(white) margin(large)) graph export total_boris.png, replace summ res, d plot res time estat dwatson save boris_final.dta, replace ******************************** ***Trip duration regression **** ******************************** codebook duration_min regress duration_min t1_int t2_int time time1 time2 predict yhat_min predict res_min, r ********************************************** **Graphing trip duration regression results*** ********************************************** twoway (scatter duration_min start_date, msymbol(o) mcolor(black)) (line yhat_min start_date, sort lcolor(black) lpattern(solid) mlabsize(medium)), ylabel(0(5)35) ytitle(Mean trip duration (minutes)) xtitle(Date) title(Mean trip duration on the Boris bikes, color(black)) legend(off) xline(18511 18539) graphregion(color(white) margin(large)) graph export minutes_boris.png, replace summ res_min, d plot res_min time estat dwatson save boris_final.dta, replace