graphics - 在 Stata 的 marginsplot 中躲避误差线

标签 graphics stata

我正在使用 marginsplot 在两个不同的组之间绘制一些误差线。虽然误差线重叠,所以我试图将它们稍微向左或向右避开。

这是从 marginsplot 帮助中略微编辑的示例,它说明了问题:

use http://www.stata-press.com/data/r13/nhanes2
quietly regress bpsystol agegrp##sex
quietly margins agegrp#sex
marginsplot, recast(scatter) ciopts(recast(rspike))

enter image description here

有什么简单的方法可以避开稍微向左的蓝色Male 点和条,以及稍微向右的红色Female 点和条(或者副-相反)?就像完成的是闪避条形图。

在这里,将置信区间重新转换到一个区域并使其略微透明会很好,就像下面的帮助示例中那样。但是,对于我的实际使用,我想保留点和尖峰。

最佳答案

这是一种使用社区贡献 命令parmesteclplot 的方法。

诀窍是通过少量调整组变量的值,例如 0.1,然后使用 eclplot< 的 subby 选项:

** a short version
use http://www.stata-press.com/data/r13/nhanes2
qui reg bpsystol agegrp##sex
qui margins agegrp#sex
qui parmest , bmat(r(b)) vmat(r(V)) level( `cilevel' ) fast
qui split parm, parse( . # )
qui destring parm*, replace
replace parm1 = parm1 - ( 0.05 )
eclplot estimate min95 max95 parm1, eplot(sc) rplottype(rspike) supby(parm3, spaceby(0.1))

short version

但是,这种方法的问题是所有的标签都丢失了,但我不知道有什么好的方法可以找回它们,除了蛮力。

以下是代码的扩展版本,我尝试通过强力方法自动重新应用所有值标签:

use http://www.stata-press.com/data/r13/nhanes2, clear

** specify parameters and variables
local cilevel = 95
local groupvar agegrp
local typevar sex
local ytitle "Linear Prediction"
local title "Adjust Predictions of `groupvar'#`typevar' with `cilevel'% CIs"
local eplot scatter
local rplottype rspike
local spaceby 0.1 /* use this param to control the dodge */

** store labels of groupvar ("agegrp") and typevar ("sex")
local varlist `groupvar' `typevar'
foreach vv of var `varlist' {
    local `vv'_varlab : var lab `vv'        
    qui levelsof `vv', local( `vv'_vals )
    foreach vl of local `vv'_vals {     
        local `vv'_`vl'lab : lab `vv' `vl'  
        lab def `vv'_vallab `vl' "``vv'_`vl'lab'", add
    }
}

** run analysis
qui reg bpsystol `groupvar'##`typevar'
margins `groupvar'#`typevar'

** use parmest to store estimates
preserve
parmest , bmat(r(b)) vmat(r(V)) level( `cilevel' ) fast
lab var estimate "`ytitle'"

split parm, parse( . # )
qui destring parm*, replace
rename parm1 `groupvar'
rename parm3 `typevar'

** reaply stored labels
foreach vv of var `varlist' {
    lab var `vv' "``vv'_varlab'"
    lab val `vv' `vv'_vallab
}

** dodge and plot
replace agegrp = agegrp - ( `spaceby' / 2 )
eclplot estimate min95 max95 agegrp ///
    , eplot( `eplot' ) rplottype( `rplottype' ) ///
    supby( sex, spaceby( `spaceby' ) ) ///
    estopts1( mcolor( navy ) ) estopts2( mcolor( maroon ) ) ///
    ciopts1( lcolor( navy ) ) ciopts2( lcolor( maroon ) ) ///
    title( "`title'" )

restore

Dodged Error Bar Chart Manually

关于graphics - 在 Stata 的 marginsplot 中躲避误差线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37168964/

相关文章:

r - Stata 在 R 中的 xtlogit (fe, re) 等价物?

r - R 中按组对变量求和

Haskell 的图像绘图库?

c - 光学字符阅读代码或指南

c - GluProject 未在 C 中显示准确的坐标

python - Stata加载共享库python39.dll失败

stata - 从具有重叠的长数据集中确定每个时期的独特处方数量

c++ - 多常量缓冲区 - 寄存器 - dx12

c++ - 手动检索和存储指向 OpenGL 函数的指针时出现问题

stata - 显示变量标签而不是变量名称 Stata 的边距图