有效地绘制置信区间

时间:2015-05-19 16:53:48

标签: stata

我希望在运行回归模型后绘制一些估计值的置信区间。

当我使用非常大的数据集时,我需要一个有效的解决方案:特别是,一个不需要我sortsave数据集的解决方案。在以下示例中,我绘制了b1b6的估算值:

reg y b1 b2 b3 b4 b5 b6

foreach i of numlist 1/6 {
   local mean `mean'  `=_b[b`i']' `i'   
   local ci `ci' ///
    (scatteri ///
    `=_b[b`i'] +1.96*_se[b`i']' `i' ///
    `=_b[`i'] -1.96 * _se[b`i']' `i' ///
    ,lpattern(shortdash) lcolor(navy)) 
} 
twoway `ci' (scatteri `mean', mcolor(navy)), legend(off) yline(0) 

虽然scatteri有效地绘制了估算值,但我无法获得类似于rcap的置信区间的边界。

有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

这是您似乎想要的令牌代码。这个例子太荒谬了。我个人认为,鉴于coefplot背后非常有成就的先前工作,改进这一点毫无意义。乘数1.96仅适用于非常大的样本。

sysuse auto, clear 
set scheme s1color 

reg mpg weight length displ 

gen coeff = . 
gen upper = . 
gen lower = . 
gen which = .  

local i = 0 
quietly foreach v in weight length displ {
   local ++i 
   replace coeff = _b[`v'] in `i'   
   replace upper = _b[`v'] + 1.96 * _se[`v'] in `i' 
   replace lower = _b[`v'] - 1.96 * _se[`v'] in `i' 
   replace which = `i' in `i' 
   label def which `i' "`v'", modify 
} 

label val which which 

twoway scatter coeff which, mcolor(navy) xsc(r(0.5, `i'.5)) xla(1/`i', val) ///
|| rcap upper lower which, lcolor(navy) xtitle("") legend(off)