Skip to content

Instantly share code, notes, and snippets.

@Aaronmoralesshildrick
Last active February 5, 2021 20:48
Show Gist options
  • Save Aaronmoralesshildrick/69a94f43f74d163db1f4cc7fe488e36c to your computer and use it in GitHub Desktop.
Save Aaronmoralesshildrick/69a94f43f74d163db1f4cc7fe488e36c to your computer and use it in GitHub Desktop.
/* set the folder for the project*/
cd "/Users/aaron/Documents/Minerva/Capstone/test 3/"
/*open data*/
use CapstoneDataStata
/*Rename the variables*/
rename Pollutantemissionsfromtranspo temissions
rename GDPperCapita GDPpc
rename PurchasingPowerAdijustedGDPp PP
rename PriceofGasolineinUSdollars Gasprice
rename Renewableenergyshare Renewable
rename PopulationDensity Popdensity
/*Looking at the trends over time with a simple line graph... ID 28 = UK*/
line temissions Year if ID == 28
line TotalCO2 Year if ID == 28
/*Install necesary package for SCM*/
ssc install synth, replace all
tsset ID Year
/* Because Greece, Iceland, Norway, and Switzerland don’t have data on transport emissions they are removed from the dataset. */
drop if ID == 8
drop if ID == 29
drop if ID == 30
drop if ID == 31
* Compared to Germany
twoway((line temissions Year if ID==28 & Year > 1999),(line temissions Year if ID==5 & Year > 1999))
/* UK Synth for temissions*/
synth temissions Population GDPpc Gasprice Renewable temissions(1990) temissions(2001) temissions(2007), trunit(28) trperiod(2008) unitnames(Country) mspeperiod(2000(1)2007) resultsperiod(2000(1)2014) nested keep(temissionssynthUK) replace fig
/* UK Synth for CO2 emissions*/
synth TotalCO2 Population PP GDPpc Gasprice Renewable, trunit(28) trperiod(2008) unitnames(Country) mspeperiod(1995(1)2007) resultsperiod(2000(1)2014) nested keep(CO2synthUK) replace fig
*Getting the Gap for the emissions in UK
use temissionssynthUK, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time year
rename _Y_treated treat
rename _Y_synthetic counterfact
gen gap28=treat-counterfact
sort year
twoway (line gap28 year,lp(solid)lw(vthin)lcolor(black)), yline(0, lpattern(shortdash) lcolor(black)) xline(2008, lpattern(shortdash) lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ytitle("Gap in transport emissions prediction error", size(medsmall)) legend(off)
save Synth-gap28, replace
*Getting the Gap for the CO2 in UK
use CO2synthUK, clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time year
rename _Y_treated treat
rename _Y_synthetic counterfact
gen gap28=treat-counterfact
sort year
twoway (line gap28 year,lp(solid)lw(vthin)lcolor(black)), yline(0, lpattern(shortdash) lcolor(black)) xline(2008, lpattern(shortdash) lcolor(black)) xtitle("",si(medsmall)) xlabel(#10) ytitle("Gap in transport emissions prediction error", size(medsmall)) legend(off)
save SynthCo2-gap28, replace
*Open dataset back up manually
/* Because Greece, Iceland, Norway, and Switzerland don’t have data on transport emissions they are removed from the dataset. */
drop if ID == 8
drop if ID == 29
drop if ID == 30
drop if ID == 31
tsset ID Year
#delimit;
set more off, permanently;
tsset ID Year;
local ID 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28;
foreach i of local ID {;
synth temissions
Population GDPpc Gasprice Renewable temissions(1990) temissions(2001) temissions(2007)
,
trunit(`i') trperiod(2008) unitnames(Country)
mspeperiod(2000(1)2007) resultsperiod(2000(1)2014) nested
keep(temissionssynth_`i') replace;
matrix state`i' = e(RMSPE); /* check the V matrix*/
};
#delimit cr
local ID 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
foreach i of local ID {
use temissionssynth_`i',clear
keep _Y_treated _Y_synthetic _time
drop if _time==.
rename _time year
rename _Y_treated treat`i'
rename _Y_synthetic counterfact`i'
gen gap`i'=treat`i'-counterfact`i'
sort year
save synth_gap_temissions`i', replace
}
use Synth-gap28, clear
sort year
save placebo_28.dta, replace
foreach i of local ID {
merge year using synth_gap_temissions`i'
drop _merge
sort year
save Finalplacebo_temissions, replace
}
use Finalplacebo_temissions
/* Messy Placebo graph*/
#delimit;
twoway
(line gap1 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap2 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap3 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap4 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap5 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap6 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap7 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap9 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap10 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap11 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap12 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap13 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap14 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap15 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap16 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap17 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap18 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap19 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap20 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap21 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap22 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap23 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap24 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap25 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap26 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap27 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap28 year ,lp(solid)lw(thick)lcolor(black)), /*treatment unit, UK*/
yline(0, lpattern(shortdash) lcolor(black)) xline(2008, lpattern(shortdash) lcolor(black))
xtitle("",si(small)) xlabel(#10) ytitle("Gap in Transport Emissions prediction error", size(small))
legend(off);
#delimit cr
/* Clean Placebo graph*/
#delimit;
twoway
(line gap3 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap4 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap6 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap10 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap11 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap12 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap15 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap19 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap20 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap22 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap27 year ,lp(solid)lw(vthin) lcolor(gs7))
(line gap28 year ,lp(solid)lw(thick)lcolor(black)), /*treatment unit, UK*/
yline(0, lpattern(shortdash) lcolor(black)) xline(2008, lpattern(shortdash) lcolor(black))
xtitle("",si(small)) xlabel(#10) ytitle("Gap in Transport Emissions prediction error", size(small))
legend(off);
#delimit cr
/*Getting the Treatment Effect*/
use Synth-gap28, clear
gen tr_effect= treat - counterfact
drop if missing(year)
gen diff_pre_treat = tr_effect if year < 2008
gen diff_post_treat = tr_effect if year >= 2008
*I make use of Excel's z-Test: Two Sample for Means for statistical diff between the two groups
*Robustness test leaving one out
*Restart the whole project and open full data
/*Rename the variables*/
rename Pollutantemissionsfromtranspo temissions
rename GDPperCapita GDPpc
rename PurchasingPowerAdijustedGDPp PP
rename PriceofGasolineinUSdollars Gasprice
rename Renewableenergyshare Renewable
rename PopulationDensity Popdensity
*Install synth and tsset
ssc install synth, replace all
tsset ID Year
*drop countries with no data on t-emissions
drop if ID == 8
drop if ID == 29
drop if ID == 30
drop if ID == 31
*drop countries one at a time
*countries to drop: Germany, France, Netherlands, Poland, and Finland
*respective IDs: 5,10,19,21,26
*drop if ID == 5
*drop if ID == 10
*drop if ID == 19
*drop if ID == 21
drop if ID == 26
*run synth
synth temissions Population GDPpc Gasprice Renewable temissions(1990) temissions(2001) temissions(2007), trunit(28) trperiod(2008) unitnames(Country) mspeperiod(2000(1)2007) resultsperiod(2000(1)2014) nested keep(temissionssynthUK) replace fig
*Robustness test -placebo in time
*Restart the whole project and open full data
/*Rename the variables*/
rename Pollutantemissionsfromtranspo temissions
rename GDPperCapita GDPpc
rename PurchasingPowerAdijustedGDPp PP
rename PriceofGasolineinUSdollars Gasprice
rename Renewableenergyshare Renewable
rename PopulationDensity Popdensity
*Install synth and tsset
ssc install synth, replace all
tsset ID Year
*drop countries with no data on t-emissions
drop if ID == 8
drop if ID == 29
drop if ID == 30
drop if ID == 31
*run synth with different time set
synth temissions Population GDPpc Gasprice Renewable temissions(1990) temissions(2001) temissions(2007), trunit(28) trperiod(2003) unitnames(Country) mspeperiod(2000(1)2003) resultsperiod(2000(1)2014) nested keep(temissionssynthUK) replace fig
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment