查看原文
其他

分享一篇使用合成控制法SCM进行实证研究的文章的代码

计量经济圈 计量经济圈 2022-05-11

凡是搞计量经济的,都关注这个号了

箱:econometrics666@126.com

所有计量经济圈方法论丛的code程序, 宏微观数据库和各种软件都放在社群里.欢迎到计量经济圈社群交流访问.

下面是一篇使用合成控制法SCM进行实证研究的文章的代码。对SCM方法感兴趣的学者,可以将其复制到Stata中对照着文章进行理解,以便将来开展自己的相关研究。针对各种方法的代码,公众号里已经有很多推荐了,有需要的学者可以自行检索【计量经济圈公众号搜索功能及操作流程演示】。

合成控制法SCM1.合成控制法创始人如何用SCM做实证呢?这些规定动作一个都不能少!2.关于合成控制法SCM的33篇精选Articles专辑!小组惊动了阿里巴巴!3.再谈合成控制法SCM, 帮你寻找因果推断控制组4.最新: 运用机器学习和合成控制法研究武汉封城对空气污染和健康的影响!5.合成控制法与HCW方法, 谁能够走得更远?6.广义合成控制法gsynth, Stata运行程序release7.广义合成控制法gsynth, 基于交互固定效应的因果推断8.DID, 合成控制, 匹配, RDD四种方法比较, 适用范围和特征9.合成控制法什么鬼? 因果推断的前沿方法指南10.用回归控制法HCW(附程序和数据)评估各国经济刺激计划的影响, 真是一个好主意!11.计量院士为你讲解回归控制法HCW的操作, 并配上自己写的一篇范文!12.HCW面板数据政策评估方法, panel数据构造对照组,13.断点回归RD和合成控制法SCM免费课程, 文章, 数据和代码全在这里, 有必要认真研究学习!14.中文刊上用断点回归RDD和合成控制法SCM的实证文章有哪些?不看至少需要收藏一下!15.用合成控制法, 机器学习和面板数据模型开展政策评估的论文!

*社群也上传的有,群友可以自行下载。

**下面的代码复制一篇文章“Carl Bonander,2017,Compared ith what? Estimating the effects
of injury prevention policies using the synthetic control method,Injury Prevention”
____________________________________________________________
Open the dataset
______________________________________________________________________
*/
*Change ... to the directory where you unzipped your data file.
cd "..."
use rep_opioids.dta, clear
*Define the incidence rate variable (per 100.000 population)
gen rate = deaths/population*100000
**Identify and drop states with missing outcome values (Synth doesn't like
**missing values)
gen missing = missing(rate)
tab state if missing==1
by statecode (missing), sort: drop if missing[_N]==1
*Tell Stata that the data is a panel dataset
xtset statecode year
/*
_________________________________________________________________________
Notes on the Synth code
_________________________________________________________________________
The code works as follows:
synth outcomevar covariate(s), options
where outcomevar is the outcome variable (rate in this case) and covariate(s) 
is a list of variables to match on. We can specify options for each variable
using () to modify the period matched on (see help file for further details).
The options trunit(), trperiod() specifiy the treated state (id for Florida=12)
and the start of the intervention period (2010).
keep() tells synth to store the results in a separate file, which is useful
for e.g. plotting in external programs, since it stores the outcomes in the
synthetic control unit. We will also exploit this feature later on to produce
inferential statistics using permutation tests.
unitnames() makes the output easier to interpet by displaying state names,
as stored in the variable state, instead of id numbers.
_________________________________________________________________________
Section 1: Model selection.
Run a set of different matching specifications to select the best model
_________________________________________________________________________
We begin by selecting the best fitting matching specifiation,
using the models suggested by Ferman et al. (2016, referenced in the paper).
The results are stored in separate files (see the keep() option), which
can be used for plotting them together.
We can use this test for model selection by choosing the model that minimizes
the pre-intervention RMSPE (which is what Synth reports in the output), and present
this model as the main result (as is done in the paper). We can also plot the
stored results to identify if the results holds (approximately) for all or any
alternative specification (which lends more confidence in the results).
As discussed in the paper, we choose the best fitting model of the options
that do not include the full vector of pre-intervention outcome years, since
other covariates are included in the model (see notes below).
*/
*All years (note that covariate weights are set to zero in models with all outcome year)[1]
synth rate rate(1999) rate(2000) rate(2001) rate(2002) rate(2003) rate(2004) rate(2005) rate(2006)
rate(2007) rate(2008) rate(2009) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_1.dta, replace) unitnames(state)
*Odd years
synth rate rate(1999) rate(2001) rate(2003) rate(2005) rate(2007) rate(2009) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_2.dta, replace) unitnames(state)
*Even years (best fit)
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_3.dta, replace) unitnames(state)
*Average
synth rate rate gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_4.dta, replace) unitnames(state)
*First half
synth rate rate(1999) rate(2001) rate(2002) rate(2003) rate(2004) rate(2005) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_5.dta, replace) unitnames(state)
*First three fourths
synth rate rate(1999) rate(2001) rate(2002) rate(2003) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(spec_6.dta, replace) unitnames(state)
/*NOTE [1]
We can extract the information regarding variable weights to probe how much
importance is given to each predictor in the model (this is based on their
predictive power). In the model using the entire range of outcome years,
the covariate weights for gdp inc and unemp_rate will be set to 0 (and are
hence redundant). Choosing one of the other model options
is likely preferable in cases when other covariates are included.
The main output tells us the unit weights and predictor balance,
but omits the variable weights. However, the variable weight matrix
can still be extracted after estimation using:
*/
matlist e(V_matrix)
/*
Which gives the results for the lastest model estimated. Now check
to compare against the model that matches on all years:
*/
qui synth rate rate(1999) rate(2000) rate(2001) rate(2002) rate(2003) rate(2004) rate(2005) rate(2006)
rate(2007) rate(2008) rate(2009) gdp inc unemp_rate, ///
trunit(12) trperiod(2010)
matlist e(V_matrix) //the variable weights for gdp, inc and unemp_rate are now = 0
/*
_________________________________________________________________________
Section 2: Main analysis.
Run synth to perform the main analysis as presented in the paper.
__________________________________________________________________________
Here we match on the outcome itself (every even year in the pre-intervention
period) and the pre-intervention averages of gdp, inc and unemp_rate. We explore
other options below, but for now: this is the model with the best pre-intervention
fit, as measured by the RMSPE (lower=better).
*/
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, ///
trunit(12) trperiod(2010) fig keep(main_res.dta, replace) unitnames(state)
/*
________________________________________________________________________________
Section 2: Inferential statistics as suggested by Abadie, Diamond and Heinmuller
We will now perform two placebo permutation tests (one graphical and one
"formal" test).
The graphical test gives us a hint regarding the distribution of placebo results from untreated states, but is sometimes hard to read.
The other produces a pseudo p-value by ranking the states (placebo and Florida) according to their post/pre-intervention RMSPE ratio (larger ratio=bigger effect estimate irrespective of direction).
________________________________________________________________________________
*/
******************
**SPAGHETTI PLOT**
******************
/*
This code runs through all units in the dataset and estimate a synthetic control unit for each one of them. It then stores and merges the results from all of these iterations (46 total), producing a new variable containing the difference between the observed values of outcome Y to the synthetic values
of Y for every year in the time series. We define the variable as the estimated effect for unit i at time t.
In the end, it plots the placebo results in gray against the main results for the treated unit (Florida) in black.
NOTE: Make sure to run the entire loop at once.
*/
set more off
egen id = group(statecode)
xtset id year
forvalues i = 1/46 { //change last number to number of units in the data
tempfile synth_`i' //tempfile to store the results from model `i'
qui synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, ///
trunit(`i') trperiod(2010) keep(`synth_`i'', replace)
}
forvalues i = 1/46 { //change last number to number of units in the data
use `synth_`i'', clear
rename _time years
gen tr_effect_`i' = _Y_treated - _Y_synthetic
keep years tr_effect_`i'
drop if missing(years)
save `synth_`i'', replace
}
use `synth_1', clear
forvalues i = 2/46 { //change last number to number of units in the data
qui merge 1:1 years using `synth_`i'', nogenerate
}
local lp
forvalues i = 1/46{ //change last number to number of units in the data
 local lp `lp' line tr_effect_`i' years, lcolor(gs12) lpattern(solid) ||
}
twoway `lp' || line tr_effect_9 years, ///
lcolor(black) lpattern(solid) legend(off) xline(2010, lpattern(dash) lcolor(black)) ylab(,nogrid)
/* NOTES:
(1) Change "line tr_effect_9" to tr_effect_[treatunit] to highlight other treatment
units in the plot. Also, change xline([year],...) to the intervention year.
In the paper, I use the resulting dataset (now in use) to generate Fig. 4, which
also highlights the placebos with the same or greater post/pre-RMSPE ratio. I use an
external plotting program for this, although code can easily be added to
the twoway plot above to highlight specific placebos. 
(2) The forvalues loop is only valid if id codes are in consecutive order.
If not, generate a new id var using "egen newid = group(oldid)" and identify
value of the treatment unit on the new id variable and the change input values
indicated in the code above. (This is not a problem in this dataset)
*/
***********************************************
**"PSEUDO P-VALUES" AND POST/PRE RMSPE RATIOS**
***********************************************
/*
Next, let's calculate a pseudo p-value based on the rank of the treatment unit's
post/pre-RMSPE ratio compared to the untreated placebo units post/pre-RMSPE
ratios (as in Abadie, Diamond and Heinmuller 2010, referenced in the paper).
We use the placebo effect dataset genereted by the code above for this,
so the code for the spaghetti plot above must be run first.
*/
preserve
gen post = 0
replace post = 1 if years>=2010 //change intervention year if needed
forvalues i = 1/46 { //change last number to to number of units/panels in the data
gen error_squared_`i' = (tr_effect_`i')^2
}
collapse (mean) error_squared_*, by(post)
reshape long error_squared_, i(post) j(id)
reshape wide error_squared_, i(id) j(post)
gen rootpre = sqrt(error_squared_0)
gen rootpost = sqrt(error_squared_1)
gen Ratio = rootpost/rootpre
summarize Ratio if id==9, meanonly //change id to treatunit
scalar treatRMSPE = r(mean)
gen prob = 0
replace prob = 1 if Ratio>=treatRMSPE
summarize prob, meanonly
scalar pval = r(mean)
tempname resmat //list post/pre ratios for all units
 forvalues i = 1/46 { //change last number to to number of units/panels in the data
summarize Ratio if id==`i', meanonly
matrix `resmat' = nullmat(`resmat') \ r(mean)
 local names `"`names' `"`i'"'"'
 }
 mat colnames `resmat' = "Post/Pre Ratio"
 mat rownames `resmat' = `names'
 matlist `resmat' , row("Temp ID")
matrix pvalue = pval
mat colnames pvalue = "p-value"
mat rownames pvalue = "Ratio trunit"
matlist pvalue //probability of finding post/pre ratio => ratio in treatment unit
restore
/*
_______________________________________________________________________________
Section 3: Sensitivity analysis (leave-one-out)
_______________________________________________________________________________
In this section, we run the leave-one-out sensitivity analysis as presented
in the paper. We begin by opening the analysis dataset again.
*/
use opioids.dta, clear
*Define the incidence rate variable (per 100.000 population)
gen rate = deaths/population*100000
*Add covariates
merge 1:1 state year using "Covariates\unemp_panel.dta", nogenerate
merge 1:1 state year using "Covariates\capita_gdp_panel.dta", nogenerate
merge 1:1 state year using "Covariates\personal_capita_income_panel.dta", nogenerate
**Identify and drop states with missing outcome values (Synth doesn't like
**missing values)
gen missing = missing(rate)
tab state if missing==1
by statecode (missing), sort: drop if missing[_N]==1
*Tell Stata that the data is a panel dataset
xtset statecode year
******************************
*********LEAVE ONE OUT********
******************************
/*
This procedure can get a little tedious as we will do it by hand,
checking the pre-intervention RMSPE for each model, and iteratively
removing the state that recieved the largest weight in the previous
iteration. As we saw in the main analysis, Maine was the largest
contributor to the synthetic control unit. As such, we begin by
dropping Maine from the donor pool. In iteration 2, we will notice
that Hawaii is the largest contributor, so iteration 3 will drop
both Maine and Hawaii, etc, until we obtain a model with twice
the prediction errors as in the main analysis (RMSPE for the main analysis
is 0.55, so we will keep going until it is >1.10. Turns out
this results in 35 iterations before we stop. (In an ideal case,
we would probably be better off by writing a algorithm that
automates this process.)
*/
**Drop Maine (Iter 1)
preserve
drop if state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_1.dta, replace)
restore
**Drop Hawaii & Maine (Iter 2)
preserve
drop if state=="Hawaii"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_2.dta, replace)
restore
**Drop Pennsylvania, Hawaii & Maine (Iter 3)
preserve
drop if state=="Pennsylvania"|state=="Hawaii"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_3.dta, replace)
restore
**Drop Arkansas,Pennsylvania, Hawaii & Maine (Iter 4)
preserve
drop if state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_4.dta, replace)
restore
**Drop Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 5)
preserve
drop if state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_5.dta, replace)
restore
**Drop Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 6)
preserve
drop if
state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii"|state=
="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_6.dta, replace)
restore
**Drop NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 7)
preserve
drop if state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_7.dta, replace)
restore
**Drop Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 8)
preserve
drop if state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_8.dta, replace)
restore
**Drop Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 9)
preserve
drop if state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_9.dta, replace)
restore
**Drop New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 10)
preserve
drop if state=="New Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_10.dta, replace)
restore
**Drop Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii &
Maine (Iter 11)
preserve
drop if state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New 
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_11.dta, replace)
restore
**Drop Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii &
Maine (Iter 12)
preserve
drop if state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_12.dta, replace)
restore
**Drop Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania,
Hawaii & Maine (Iter 13)
preserve
drop if state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_13.dta, replace)
restore
**Drop Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali,
Arkansas,Pennsylvania, Hawaii & Maine (Iter 14)
preserve
drop if state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_14.dta, replace)
restore
**Drop Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali,
Arkansas,Pennsylvania, Hawaii & Maine (Iter 15)
preserve
drop if
state=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_15.dta, replace)
restore
**Drop Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali,
Arkansas,Pennsylvania, Hawaii & Maine (Iter 16)
preserve
drop if
state=="Utah"|state=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington
"|state=="New Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_16.dta, replace)
restore
**Drop Louisiana, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ,
Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 17)
preserve
drop if
state=="Louisiana"|state=="Utah"|state=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|
state=="Washington"|state=="New Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_17.dta, replace)
restore
**Drop Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ,
Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 18)
preserve
drop if
state=="Alabama"|state=="Louisiana"|state=="Utah"|state=="Georgia"|state=="Idaho"|state=="Texas"
|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_18.dta, replace)
restore
**Drop Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia,
NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 19)
preserve
drop if
state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|state=="Georgia"|state=="Ida
ho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New 
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_19.dta, replace)
restore
**Drop Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland,
Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 20)
preserve
drop if
state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|state=="Georgi
a"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_20.dta, replace)
restore
**Drop NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp.,
Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 21)
preserve
drop if state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_21.dta, replace)
restore
**Drop Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp.,
Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 22)
preserve
drop if state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_22.dta, replace)
restore
**Drop Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing,
New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 23)
preserve
drop if state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_23.dta, replace)
restore
**Drop Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas,
Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine
(Iter 24)
preserve
drop if state=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_24.dta, replace)
restore
**Drop Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia,
Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania,
Hawaii & Maine (Iter 25)
preserve
drop if state=="Minnesota"|state=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_25.dta, replace)
restore
**Drop Delaware, Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah,
Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali,
Arkansas,Pennsylvania, Hawaii & Maine (Iter 26)
preserve
drop if
state=="Delaware"|state=="Minnesota"|state=="Connecticut"|state=="Tennessee"|state=="Nevada"|st
ate=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New 
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_26.dta, replace)
restore
**Drop Massachusetts, Delaware, Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio, Montana,
Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali,
Arkansas,Pennsylvania, Hawaii & Maine (Iter 27)
preserve
drop if
state=="Massachusetts"|state=="Delaware"|state=="Minnesota"|state=="Connecticut"|state=="Tennes
see"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_27.dta, replace)
restore
**Drop Colorado, Massachusetts, Delaware, Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio,
Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia, NJ,
Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 28)
preserve
drop if
state=="Colorado"|state=="Massachusetts"|state=="Delaware"|state=="Minnesota"|state=="Connectic
ut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_28.dta, replace)
restore
**Drop Iowa, Colorado, Massachusetts, Delaware, Minnesota, Connecticut, Tennessee, Nevada, NC,
Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland, Virginia,
NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 29)
preserve
drop if
state=="Iowa"|state=="Colorado"|state=="Massachusetts"|state=="Delaware"|state=="Minnesota"|sta
te=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_29.dta, replace)
restore
**Drop Indiana, Iowa, Colorado, Massachusetts, Delaware, Minnesota, Connecticut, Tennessee, Nevada,
NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New Hamp., Maryland,
Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 30)
preserve
drop if
state=="Indiana"|state=="Iowa"|state=="Colorado"|state=="Massachusetts"|state=="Delaware"|state=
="Minnesota"|state=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_30.dta, replace)
restore
**Drop Wisconsin, Indiana, Iowa, Colorado, Massachusetts, Delaware, Minnesota, Connecticut,
Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New
Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 31)
preserve
drop if
state=="Wisconsin"|state=="Indiana"|state=="Iowa"|state=="Colorado"|state=="Massachusetts"|state
=="Delaware"|state=="Minnesota"|state=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=
="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_31.dta, replace)
restore
**Drop Illinois, Wisconsin, Indiana, Iowa, Colorado, Massachusetts, Delaware, Minnesota, Connecticut,
Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz., Washing, New
Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter 32)
preserve
drop if
state=="Illinois"|state=="Wisconsin"|state=="Indiana"|state=="Iowa"|state=="Colorado"|state=="Mass
achusetts"|state=="Delaware"|state=="Minnesota"|state=="Connecticut"|state=="Tennessee"|state==
"Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_32.dta, replace)
restore
**Drop Vermont, Illinois, Wisconsin, Indiana, Iowa, Colorado, Massachusetts, Delaware, Minnesota,
Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas, Ariz.,
Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine (Iter
33)
preserve
drop if
state=="Vermont"|state=="Illinois"|state=="Wisconsin"|state=="Indiana"|state=="Iowa"|state=="Color
ado"|state=="Massachusetts"|state=="Delaware"|state=="Minnesota"|state=="Connecticut"|state=="T
ennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_33.dta, replace)
restore
**Drop Missi., Vermont, Illinois, Wisconsin, Indiana, Iowa, Colorado, Massachusetts, Delaware,
Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas,
Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine
(Iter 34)
preserve
drop if
state=="Mississippi"|state=="Vermont"|state=="Illinois"|state=="Wisconsin"|state=="Indiana"|state=="
Iowa"|state=="Colorado"|state=="Massachusetts"|state=="Delaware"|state=="Minnesota"|state=="Co
nnecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_34.dta, replace)
restore
**Drop Missouri, Missi., Vermont, Illinois, Wisconsin, Indiana, Iowa, Colorado, Massachusetts, Delaware,
Minnesota, Connecticut, Tennessee, Nevada, NC, Ohio, Montana, Alabama, Utah, Georgia, Idaho, Texas,
Ariz., Washing, New Hamp., Maryland, Virginia, NJ, Kansas, Cali, Arkansas,Pennsylvania, Hawaii & Maine
(Iter 35)
preserve
drop if
state=="Missouri"|state=="Mississippi"|state=="Vermont"|state=="Illinois"|state=="Wisconsin"|state==
"Indiana"|state=="Iowa"|state=="Colorado"|state=="Massachusetts"|state=="Delaware"|state=="Minn
esota"|state=="Connecticut"|state=="Tennessee"|state=="Nevada"|state=="North
Carolina"|state=="Ohio"|state=="Montana"|state=="Alabama"|state=="Louisiana"|state=="Utah"|stat
e=="Georgia"|state=="Idaho"|state=="Texas"|state=="Arizona"|state=="Washington"|state=="New
Hampshire"|state=="Maryland"|state=="Virginia"|state=="New
Jersey"|state=="Kansas"|state=="California"|state=="Arkansas"|state=="Pennsylvania"|state=="Hawaii
"|state=="Maine"
synth rate rate(2000) rate(2002) rate(2004) rate(2006) rate(2008) gdp inc unemp_rate, trunit(12)
trperiod(2010) unitnames(state) fig keep(loo_35.dta, replace)
restore
/*
_________________
Plot the results
_________________
*/
use loo_1, clear
rename _time years
keep years _Y_treated _Y_synthetic
rename _Y_treated observed
rename _Y_synthetic loo_1
drop if missing(years)
save loo_1, replace
forvalues i = 2/35 { //change last number to number of leave one out-iterations
use loo_`i', clear
rename _time years
keep years _Y_synthetic
rename _Y_synthetic loo_`i'
drop if missing(years)
save loo_`i', replace
}
use loo_1, clear
forvalues i = 2/35 { //change last number to number of leave one out-iterations
qui merge 1:1 years using loo_`i', nogenerate
}
local lp
forvalues i = 1/35{ //change last number to number of leave one out-iterations
 local lp `lp' line loo_`i' years, lcolor(gs12) lpattern(solid) ||
}
twoway `lp' || line observed years, ///
lcolor(black) lpattern(solid) legend(off) xline(2010, lpattern(dash) lcolor(black)) ylab(,nogrid)

下面这些短链接文章属于合集,可以收藏起来阅读,不然以后都找不到了。

2.5年,计量经济圈近1000篇不重类计量文章,

可直接在公众号菜单栏搜索任何计量相关问题,

Econometrics Circle




数据系列空间矩阵 | 工企数据 | PM2.5 | 市场化指数 | CO2数据 |  夜间灯光 | 官员方言  | 微观数据 | 内部数据计量系列匹配方法 | 内生性 | 工具变量 | DID | 面板数据 | 常用TOOL | 中介调节 | 时间序列 | RDD断点 | 合成控制 | 200篇合辑 | 因果识别 | 社会网络 | 空间DID数据处理Stata | R | Python | 缺失值 | CHIP/ CHNS/CHARLS/CFPS/CGSS等 |干货系列能源环境 | 效率研究 | 空间计量 | 国际经贸 | 计量软件 | 商科研究 | 机器学习 | SSCI | CSSCI | SSCI查询 | 名家经验计量经济圈组织了一个计量社群,有如下特征:热情互助最多前沿趋势最多、社科资料最多、社科数据最多、科研牛人最多、海外名校最多。因此,建议积极进取和有强烈研习激情的中青年学者到社群交流探讨,始终坚信优秀是通过感染优秀而互相成就彼此的。


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存