Stata R:不可不知的合成双重差分SDID案例集合
sdid—Stata中合成双重差分
继Arkhangelsky等人(2021年)之后,该Stata包实现了综合双重差分估计过程,以及一系列推断和绘图过程。Arkhangelsky等人提供了一个使用R的代码实现,这里附带了一些材料:synthdid。这里我们提供了一个原生的Stata实现,主要是用Mata编写的。这个包扩展了原始R包的功能,允许非常简单地在多个处理阶段交错采用的上下文中进行评估(以及在原始代码的单个采用阶段)。
1、下载命令ssc install sdid
findit sdid
help sdid
2、Stata
- https://github.com/Daniel-Pailanir/sdid/3、R codes
- https://github.com/Daniel-Pailanir/sdid/blob/main/sdidExamples.do
- https://github.com/Daniel-Pailanir/sdid/blob/main/sdidExamples.pdf
- https://github.com/Daniel-Pailanir/sdid/blob/main/sdidExamples.do
- https://github.com/skranz/xsynthdid
4、选项含义 及语法格式
sdid depvar groupvar timevar treatment, vce(vcetype) [options]
options Description
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
vce(vcetype) vcetype may be bootstrap, jackknife, or placebo.
covariates(varlist, [type]) Allows for the inclusion of covariates in the calculation of the synthetic counterfactual. Optional type can be specified, as either 'optimized' (the default) or
'projected', which is preferable in certain circumstances.
seed(#) set random-number seed to #.
reps(#) repetitions for bootstrap and placebo inference.
graph if this option is specified, graphs will be displayed in the style of figure 1 from Arkhangelsky et al. (2021)
g1_opt(graph options) option to modify the appearance of the unit-specific weight graph.
g2_opt(graph options) option to modify the appearance of the outcome trend graphs.
graph_export(string, type) option allowing for generated graphs to be saved to the disk.
unstandardized In the case of 'optimized' covariates, by default covariates will be standardized as z-scores, unless the unstandardized option is specified.
选项含义:
Y:结果变量(数值)
S:单位变量(数字或字符串)
T:时间变量(数值)
D:处理的虚拟,如果单位被处理,则为1,否则为0(数字)
vce (): bootstrap, jackknife,安慰剂标准误差。
seed():伪随机数的种子定义。
reps(#):重复bootstrap和安慰剂。
covariates(varlist [, method]):用于调整y的协变量。应包含协变量的varlist,并可选用于调整方法的选项。在这种情况下,它可以遵循Arkhangelsky等人提出的方法进行“优化”,在这种情况下,它可以遵循Kranz, 2021 (xsynth在R中)提出的程序。在没有指定方法的情况下,默认使用“优化”。克兰兹已经证明投影法在许多情况下是可取的。在这个实现中,投影的方法通常要快得多。
graph:如果指定了该选项,将显示单位和时间权重以及结果趋势的图形,如Arkhangelsky等人的图1。
g1_opt(string) g2_opt(string):选项,用于修改上述图形的外观。G1为单位权重图,g2为结果趋势图。请求的选项必须遵循Stata的twoway_options的语法。
unstandardized:如果包含了控制,并且指定了“优化”方法,在找到最优权重之前,控制将被标准化为z分数。这避免了控制变量具有高度离散时的优化问题。如果指定了非标准化,则只需输入控件的原始单位。应该谨慎使用这个选项。
graph_export([stub], type):使用这个选项可以将生成的图形保存到磁盘。每个单位权重和结果趋势将分别保存为weightsYYYY和trendsYYYY, YYYY表示每个治疗采用期。每个治疗采用阶段将生成两个图表。如果指定了该选项,则必须指定type,它指的是一个有效的Stata图形类型(例如'。Eps ', '.pdf'等等)。可选地,可以指定一个存根,在这种情况下,它将被放在导出的图形名称的前面。
5、案例应用代码合集案例1一个基于提案99 (Abadie et al., 2010)的例子,只有一个采用日期。加载Abadie et al.(2010)数据:
. use 'C:\Users\Metrics\Desktop\prop99_example.dta'
使用sdid进行估计,导出权重和趋势图:
. sdid packspercapita state year treated, vce(placebo) seed(1213) graph g1_opt(xtitle('')) g2_
可以使用 eststo and esttab输出结果:
*create a uniform variable to use as a controlgen r=runiform()
*run sdid
eststo sdid_1: sdid packspercapita state year treated, vce(placebo) seed(2022)
eststo sdid_2: sdid packspercapita state year treated, vce(placebo) seed(2022) covariates(r, projected)
*create a table
esttab sdid_1 sdid_2, starlevel ('*' 0.10 '**' 0.05 '***' 0.01) b(%-9.3f) se(%-9.3f)
结果为:
--------------------------------------------案例2
(1) (2)
packsperca~a packsperca~a
--------------------------------------------
treated -15.604* -15.750*
(7.981) (8.039)
--------------------------------------------
N 1209 1209
--------------------------------------------
Standard errors in parentheses
* p 0.10, ** p 0.05, *** p 0.01
基于议会性别配额、议会中的女性和孕产妇死亡率的交错采用设计实例(Bhalotra等人,2020年)。数据加载:
use 'C:\Users\Metrics\Desktop\quota_example.dta'运行没有协变量和引导标准误差的估计
sdid womparl country year quota, vce(bootstrap) seed(1213)
以投影的方式使用协变量运行sdid
sdid womparl country year quota, vce(bootstrap) seed(1213) covariates(lngdp, projected)安慰剂检验代码
安慰剂检验代码为:
//Specify URL
webuse set www.damianclarke.net/stata/
*------------------------------------------------------------------------------*
*ONE TIME ADOPTION
*------------------------------------------------------------------------------*
webuse quota_example.dta, clear
*-----------*
*Bootstrap SE
*-----------*
//Keep only one time adoption units (2002)
#delimit ;
drop if country=='Algeria' | country=='Jordan' | country=='Kenya' |
country=='Rwanda' | country=='Samoa' | country=='Swaziland' |
country=='Tanzania';
#delimit cr
sdid womparl country year quota, vce(bootstrap) seed(1234) graph
*with control
preserve
drop if lngdp==.
sdid womparl country year quota, vce(bootstrap) seed(1234) covariates(lngdp)
sdid womparl country year quota, vce(bootstrap) seed(1234) covariates(lngdp, projected)
restore
*----------*
*Placebo SE
*----------*
sdid womparl country year quota, vce(placebo) seed(1234)
*with control: it is necessary drop the missing observations in control variable
preserve
drop if lngdp==.
sdid womparl country year quota, vce(placebo) seed(1234) covariates(lngdp)
sdid womparl country year quota, vce(placebo) seed(1234) covariates(lngdp, projected)
restore
*-----------*
*Jackknife SE
*-----------*
sdid womparl country year quota, vce(jackknife)
*with control: it is necessary drop the missing observations in control variable
preserve
drop if lngdp==.
sdid womparl country year quota, vce(jackknife) covariates(lngdp)
sdid womparl country year quota, vce(jackknife) covariates(lngdp, projected)
restore
*------------------------------------------------------------------------------*
*STAGGERED ADOPTION
*------------------------------------------------------------------------------*
webuse quota_example.dta, clear
*-----------*
*Bootstrap SE
*-----------*
sdid womparl country year quota, vce(bootstrap) seed(1234) graph
*with control: it is necessary drop the missing observations in control variable
preserve
drop if lngdp==.
sdid womparl country year quota, vce(bootstrap) seed(1234) covariates(lngdp)
sdid womparl country year quota, vce(bootstrap) seed(1234) covariates(lngdp, projected)
restore
*----------*
*Placebo SE
*----------*
sdid womparl country year quota, vce(placebo) seed(1234)
*with control: it is necessary drop the missing observations in control variable
preserve
drop if lngdp==.
sdid womparl country year quota, vce(placebo) seed(1234) covariates(lngdp)
sdid womparl country year quota, vce(placebo) seed(1234) covariates(lngdp, projected)
restore
*-----------*
*Jackknife SE
*-----------*
//Keep only treatment periods with more than 2 treated units (2002-2003)
//This is required for jackknife standard error
#delimit ;
drop if country=='Algeria' | country=='Kenya' | country=='Samoa' |
country=='Swaziland' | country=='Tanzania';
#delimit cr
sdid womparl country year quota, vce(jackknife)
*with control: it is necessary drop the missing observations in control variable
preserve
drop if lngdp==.
sdid womparl country year quota, vce(jackknife) covariates(lngdp)
sdid womparl country year quota, vce(jackknife) covariates(lngdp, projected)
restore
R软件安装
当前的开发版本可以使用devtools从源代码安装。
devtools: install_github(“synth-inference / synthdid”)
R语言版本:Example
library(synthdid)References
# Estimate the effect of California Proposition 99 on cigarette consumption
data('california_prop99')
setup = panel.matrices(california_prop99)
tau.hat = synthdid_estimate(setup$Y, setup$N0, setup$T0)
se = sqrt(vcov(tau.hat, method='placebo'))
sprintf('point estimate: %1.2f', tau.hat)
sprintf('95%% CI (%1.2f, %1.2f)', tau.hat - 1.96 * se, tau.hat + 1.96 * se)
plot(tau.hat)
Dmitry Arkhangelsky, Susan Athey, David A. Hirshberg, Guido W. Imbens, and Stefan Wager. Synthetic Difference in Differences, 2019.'’
本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
0条评论