2017-01-12 53 views
0

我想在Stata中編寫一個事件研究的代碼,但我無法完全得到我想要的。 Jacobson,LaLonde和Sullivan(1993),698頁圖3(http://www.princeton.edu/~davidlee/wp/0.pdf)有一個和我想要的非常相似的情節,除了我也想增加置信區間。Stata事件研究圖形代碼

基於此教程,http://www.stata.com/meeting/germany14/abstracts/materials/de14_jann.pdf,我寫了下面的代碼:

sysuse auto, clear 
egen t = fill(1,2,3,4,1,2,3,4) 
quietly regress price ib2.t trunk weight if foreign==0 
estimates store domestic 
quietly regress price ib2.t trunk weight if foreign==1 
estimates store foreign 
coefplot (domestic, label(Domestic Cars)) (foreign, label(Foreign Cars)), drop(_cons) xline(0) vertical omitted baselevels 

這將產生在我想要什麼看球的東西,但存在以下問題:

  1. 的點估計值和置信區間是並列的,而不是相互之間(如果這是唯一的問題,這可能很好)。
  2. 我的時間變量t出現在每個x標籤(t = 1,t = 2等)中,但我只想讓它在沒有t =的情況下說(1,2等)。
  3. 我不得不在這個玩具的例子中以1開始我的t編號,因爲因子變量與i運算符組合必須是非負的。我希望我的時間變量能夠承擔負數。
  4. 我不希望trunkweight出現在圖中。將這些放在drop(...)中可以嗎?
  5. 我也希望能夠在迴歸殘差中做所有這些,而不是我上面所說的。
  6. 我想用線連接點估計值。

我根本沒有結婚coefplot命令。其他技術,特別是使用內置的Stata命令也是完全可以接受的。

回答

2

希望我已經正確地回答你的問題,也許我誤解的東西,但這裏是我的回答:

(我沒有解決5,因爲我不知道到底是什麼,你正在尋找與這個問題,但看到我的解決方案後,也許這將是清楚的)

代碼:

// load data same as before 
sysuse auto, clear 
egen t = fill(1,2,3,4,1,2,3,4) 

// get coefficients and standard errors of regressions over foreign 
statsby _b _se , clear by(foreign): regress price ib2.t trunk weight 

// there are some extra variables we don't need/want 
drop *_trunk *_weight *_cons 

// generate confidence intervals and rename coefficient variables 
forvalues i = 1/4 { 
    local j = `i'+7 
    gen ci_low`i' = _stat_`i' - 1.96*_stat_`j' 
    gen ci_high`i' = _stat_`i' + 1.96*_stat_`j' 
    rename _stat_`i' coef`i' 

} 
// no longer in need of standard error variables 
drop _stat_8 _stat_9 _stat_10 _stat_11 

// now, we want our data in long format so we can do a twoway graph 
reshape long coef ci_low ci_high, i(foreign) j(t) 

// we can label the t values so that they start below 1 
lab def timeseries 1 "-1" 2 "0" 3 "1" 4 "2" 
lab values t timeseries 

// now graph, note each factor has two pieces, a scatter (with connecting lines) 
// and an rcap for the confidence intervals 
twoway (sc coef t if foreign == 1, mcolor(navy) lcolor(navy) connect(direct)) /// 
     (rcap ci_low ci_high t if foreign == 1, lcolor(navy)) /// 
    (sc coef t if foreign == 0, mcolor(maroon) lcolor(maroon) connect(direct)) /// 
     (rcap ci_low ci_high t if foreign == 0, lcolor(maroon)), /// 
    legend(lab(1 "Foreign") lab(2 "Foreign CI") lab(3 "Domestic") lab(4 "Domestic CI")) /// 
    xlab(,val) 

output:

一些人們可能希望改善這種方法是:

  • 與標籤定義,可以使用一個for循環的時間序列比這所以不是全部由手工完成再

  • 我不是在statsby的專家,也許還有一個更簡單的方式來獲得的置信區間,並離開了樹幹,重量和不斷

至於殘差,這個答案的基本直覺是你想要一個包含係數和置信區間的數據集。因此,如果您可以計算殘差及其CI的值並將其放入數據集中,則可以使用相同類型的雙向圖。