雜項
cd "C:\file"
# 切換路徑
use "data.dta", clear
webuse cancer, clear
# 從本體/網絡讀取數據
preserve
...
restore
# 在不更改數據的情況下進行操作
summarize y, detail
tabstat t, statistic(mean sd)
tab x1 x2
tab1 x1 x2
# 統(tǒng)計描述
foreach v in x y z{
......
}
forvalues i = 1/5{
......
}
# 常用循環(huán)
egen cat = group(x1 x2)
# 分組
egen cutpoint50 = pctile(mv), p(50)
gen cat = 1
replace cat = 2 if mv > cutpoint50
# 按值分組
reshape wide score, i(class) j(student)
reshape long score, i(class) j(student)
# 數據reshape
bysort id: egen num = sum(x == "A")
bysort id: egen sum = sum(x)
# 分組統(tǒng)計與分組求和
input Id str1 cons1 str1 cons2 str1 cons3 str1 cons4
1 A A B C
2 A A A A
3 B C D A
4 A B D D
5 E B S C
end
# 手動導入數據
local a = 1
display `i'
global varlist x1 x2 x3 x4
reg y $varlist
# local and global
duplicates drop x1 x2 x3 x4, force
# 刪除重復項
recode x (-3/-2 = .)(-1 = .)
# 批量賦值
est store A
esttab, compress nogap b(%6.3f) scalars(r2 N) star(* 0.1 ** 0.05 *** 0.01) title("")
# 回歸結果導出
gen varname= varname[_n+1]
# 按編號索引
多元插值
mi set mlong
mi register imputed x
# 設定插值的變量為x
set seed 29390
mi impute mvn visit4 = trt visit1, add(2) replace
# 插值
# 注意:該一系列命令前不能帶preserve
置換檢驗
permute died sum=r(mean), reps(100) left nodrop nowarn: summarize died if drug
面板數據模型(固定效應)
xtset id time
xtreg y x control_variable, fe robust
# robust減弱假設,fe固定效應夕春,控制了id層面的差異
reg y x i.id, vce(cluster id)
# 該命令與上述命令等價
reghdfe i.industry i.race i.year, absorb(id) vce(cluster id)
# 多維面板固定效應估計
xtreg y x control_variable i.y, fe robust
# 雙向固定效應模型,同時控制了id、time層面差異
# 將y拆分為僅與個體相關遍希、僅與時間相關、同時與個體與時間相關的三部分
xtfmb y x
# 下文提到Fama and Macbeth的pols估計方法
xtreg y x treat#post i.post, fe
# 雙重差分里烦,其中did可理解為標準did中的did孵班,post反應政策前還是政策后
xtreg y x treat#cat#post treat#cat treat#post cat#post i.cat i.post, fe
# 三重差分涉兽,其中did可理解為標準did中的did,post反應政策前還是政策后篙程,cat是第三維的分類變量
xtabond y x
# 動態(tài)面板 GMM
xtthres
# 門檻模型
備注
- POLS (Pooled Ols)枷畏,混合OLS,將面板數據中所有截面混合到一起作為一個整體樣本虱饿。其中Fama and Macbeth的方法為拥诡,對面板中截面分別進行回歸,然后取均值氮发。
參考資料
面板數據 | 連玉君
reghdfe | 多維面板固定效應估計
去心
center x, prefix(c)
# c_x = x-mean(x)
# prefix設定生成新變量前綴
工具變量 IV
ivregress 2sls y x2 x3 x4 (x1 = IV), robust
estat endog
estat firststage
estat overid
ivprobit y x2 x3 x4 (x1 = IV), robust
ivtobit y x2 x3 x4 (x1 = IV), robust
結構方程 SEM
alpha ......
# 信度分析
spearman ......
# 效度分析
sem ......
estat gof, stats(all)
# 擬合檢驗
estat eqtest
estat framework, compact
參考資料
結構方程模型(SEM)
主成分分析
global varlist x1 x2 x3 x4
pca $varlist, vce(normal)
# 進行一次不限定主成分數量的分析
screeplot
# 繪制碎石圖渴肉,決定主成分數量
pca $varlist, comp(4) vce(normal)
# 重新進行限定了數量的主成分分析
estat anti
estat kmo
estat loadings
estat residuals
estat smc
estat summarize
PSM
global varlist x1 x2 x3 x4
psmatch2 treatment $varlist, out(y) logit ate kernel common
pstest $varlist, both graph
# 用于平衡性檢驗
twoway (histogram _pscore if pa==0, bin(100) fcolor(black%60) lcolor(black%70) lwidth(none) ylabel(, glcolor(white) nogmin nogmax nogextend) legend(off) graphregion(margin(zero) fcolor(white) lcolor(white) lwidth(none) ifcolor(white) ilcolor(white) ilwidth(none)) plotregion(margin(zero) fcolor(white) lcolor(white) lwidth(none) ifcolor(white) ilcolor(white) ilwidth(none))) (histogram _pscore if pa==1, bin(100) fcolor(blake%30) lcolor(black%60) lwidth(none) ylabel(, glcolor(white) nogmin nogmax nogextend) legend(off) graphregion(margin(zero) fcolor(white) lcolor(white) lwidth(none) ifcolor(white) ilcolor(white) ilwidth(none)) plotregion(margin(zero) fcolor(white) lcolor(white) lwidth(none) ifcolor(white) ilcolor(white) ilwidth(none))),aspectratio(1) xscale(titlegap(0) outergap(2)) xtitle(傾向得分) ytitle(密度)
# 傾向得分直方圖,用于完全覆蓋假設