*安慰剂检验,随机抽取实验对象
mat b = J(10000,1,0) //* 系数矩阵
mat se = J(10000,1,0) //* 标准误矩阵
mat p = J(10000,1,0) //* P值矩阵
forvalues i=1/10000{
use "D:\des_brief_panel.dta",clear
xtset ids year //面板数据声明
keep if year==2013 //保留一期数据
sample 1969, count //随机抽取1969个城市
keep ids //得到所抽取样本的id编号
save match_ids.dta, replace //另存id编号数据
merge 1:m ids using "D:\des_brief_panel.dta" //与原数据匹配
gen treat = (_merge == 3) //将所抽取样本赋值为1,其余为0,得到政策分组虚拟变量
gen period = (year == 2014) //生成政策时间虚拟变量
gen did = treat*period
reghdfe TotalScore_sd did cogn sex HuKou economy sibling parent_edu empty_nest class_size class_rank school_rank school_urban, absorb(ids year) vce(cluster ids)
* 将回归结果赋值到对应矩阵的对应位置
mat b[`i',1] = _b[did]
mat se[`i',1] = _se[did]
* 计算P值并赋值于矩阵
mat p[`i',1] = 2*ttail(e(df_r), abs(_b[did]/_se[did]))
}
* 矩阵转化为向量
svmat b, names(coef)
svmat se, names(se)
svmat p, names(pvalue)
* 删除空值并添加标签
drop if pvalue1 == .
label var pvalue1 p值
label var coef1 估计系数
keep coef1 se1 pvalue1
*画p值散点图
twoway (scatter pvalue1 coef1, msymbol(smcircle_hollow) mcolor(blue)), ///
title("Placebo Test") ///
xlabel(-0.06(0.02)0.06) ylabel(,angle(0)) ///
xline(0, lwidth(vthin) lp(shortdash)) xtitle("Coefficients") ///
yline(0.01,lwidth(vthin) lp(dash)) ytitle(p value) ///
legend(label(1 "kdensity of estimates") label( 2 "p value")) ///
plotregion(style(none)) ///无边框
graphregion(color(white)) //白底
*画系数-核密度图
twoway (kdensity coef1) , ///
xlabel(-0.1(0.1)0.1) ylabel(,angle(0)) ///
xline(0, lwidth(vthin) lp(shortdash)) xtitle("Coefficients") ///
yline(0.01,lwidth(vthin) lp(dash)) ytitle(Density) ///
legend(label(1 "kdensity of estimates") label( 2 "p value")) ///
plotregion(style(none)) ///无边框
graphregion(color(white)) //白底