stata STATA

Stata可视化:让他看懂我的结果!

2017-12-30  本文已影响1504人  stata连享会

作者:刘杨 | 连玉君 | (知乎 | 简书 | 码云)

Stata 现场培训报名中……

我一直认为:

没有耳朵不好的听众,只有嘴巴不好的讲者!

没有思维迟钝的学生,只有逻辑混乱的老师!

所以,不要怪别人笨,那是因为我们没有讲清楚,至少是没有用心讲!

1. 回归结果的图形呈现

大家在做完一篇实证研究的论文之后是否有这样的困惑:就是如何将估计系数结果能够简洁、直观的呈现出来?

在学术会议交流论文、或者在毕业论文答辩时,一个常见的做法就是直接将回归表格粘贴出来,然而弊端也是显而易见的,那就是页面中信息太多,导致想要突出的估计结果和显著性判定被淹没其中。

无疑,这是对老师和同行耐心力和细致力的一份考验,需要他们瞪大眼睛来看、竖起耳朵来听,稍有打盹,翻页过去的结果可能就忘记了,只能坐在那里安静的怀疑人生......

那么,如果能够用图形的方式呈现估计系数及其显著性,并且不同估计系数间的相互关系也能够一目了然,岂不美哉?!

很幸运,Stata 提供了这样的外部命令来完成这一工作。我们今天的推文就来给大家简单的介绍一下这两个外部命令 coefplotarrowplot

2. 目的

3. coefplot 命令

3.1 coefplot 命令的基本语法

coefplot [name] [, opition]

其中,name 为储存于 stata 内存中的回归结果名称(使用 est store 命令),如果此处缺省,则默认是 stata 内存中现存的回归结果。

 . sysuse auto, clear 
 . reg price mpg length turn
 . coefplot, drop(_cons) 

说明:

图1:coefplot 基本绘图
3.2 拓展:分组回归后绘图

实证分析中还经常划分两个或两个以上的子样本,进行分组回归,进而对比两组的回归系数。此时,使用 coefplot 可以非常直观地进行组建系数的对比。

. reg price mpg length turn if foreign==0
. est store Domestic 

. reg price mpg length turn if foreign==1
. est store Foreign 

. coefplot Domestic Foreign, drop(_cons) xline(0) 
  *-Note: xline表示在x轴0处做出辅助线,便于判断显著性
 
. coefplot Domestic Foreign,  ///
    drop(_cons)               ///
    xline(0, lp(dash) lc(black*0.3)) 
  *-Note: 如果想要调换 X 轴和 Y 轴,可以添加 vertical 选项, 图略
图2:分组呈现系数估计值
3.3 拓展:对每一组回归系数进行个性化设定

coefplot命令可以对不同的回归进行图形的单独设定,基本语法如下:

coefplot (name [, plotopts]) (name [, plotopts]) ... [, globalopts]

其中:

Stata范例 如下:

coefplot    ///
  (Domestic, label("国产汽车") offset(0.05)  pstyle(p3)) ///
  (Foreign , label("进口汽车") offset(-0.05) pstyle(p4)) ///
   , drop(_cons) xline(0, lp(dash) lc(black*0.3)   

说明:

图3:对每组的系数图形进行个性化设定
3.4 拓展:不同的估计模型下估计系数的比较

coefplot (namelist [, plotopts]) ...

不同估计模型用括号来隔开。例如,我们将多元回归的每个解释变量进行单变量回归,并将估计系数与多元回归的系数比较。

. reg price mpg length turn
. est store multi

. foreach var in mpg length turn {
    qui reg price `var'
    est store  `var'
  }

. coefplot        ///
    (mpg \length \turn, label("单变量回归"))  ///
    (multi, label("多元回归"))                ///
    , drop(_cons) ///
    xline(0, lp(dash) lc(black*0.3)) 
  graph export "图4:多元回归系数和单变量回归系数比较.png", replace
 *-Note: 在 namelist 中用 \ 分隔存储的回归结果名称
图4:多元回归系数和单变量回归系数比较
3.5 拓展:不同被解释变量下估计系数的图形比较

coefplot命令也可以对更换被解释变量后,估计系数的相互关系进行比较,这实质上是不同图形的合并。基本语法如下:

coefplot plotlist [, subgropts] || plotlist [, subgropts] || ... [, globalopts]

其中,

reg price mpg length turn if foreign==0
est store Domestic

reg price mpg length turn if foreign==1
est store Foreign

reg weight mpg length turn if foreign==0
est store Domestic_w  //更换了被解释变量

reg weight mpg length turn if foreign==1
est store Foreign_w   //更换了被解释变量

#d ;
coefplot 
  (Domestic, label("国产汽车"))  
  (Foreign , label("进口汽车")), bylabel(Price) || 
  (Domestic_w) (Foreign_w), bylabel(Weight) ||
  , 
  drop(_cons) byopts(xrescale) 
  xline(0, lp(dash) lc(black*0.3))
  ;
#d cr
graph export "图5:不同被解释变量回归的比较.png", replace
图5:不同被解释变量回归的比较

4. arrowplot 命令

顾名思义,这个命令是用来画箭头的!

安装help arrowplot 此命令为外部命令,可以使用 ssc install arrowplot 下载安装。

用途

图6:arrowplot - Stevenson-Wolfers happiness graphs

4.1 基本语法

arrowplot yvar xvar [if] [in] [weight], groupvar(varname) [options]

. sysuse "nlsw88.dta", clear
. decode occupation, gen(occu_str) maxlength(6)
. arrowplot wage hours, groupvar(occu_str)
. graph export "图7:arrowplot 基本绘图.png", replace
图7:arrowplot 基本绘图
4.2 拓展:加入控制变量的箭头图

如果想要在回归过程中加入其它控制变量,可以加入 controls(varlist) 选项,Stata 范例如下

. arrowplot wage hours, groupvar(occu_str) ///
           control(age collgrad)
. graph export "图8:arrowplot 回归中加入控制变量.png", replace
图8:arrowplot 回归中加入控制变量
4.3 拓展:添加分组信息,规定箭头长度

-Stata范例如下

. arrowplot wage hours, groupvar(occu_str) ///
      groupname(occupation) line(2)
. graph export "图9:arrowplot 添加分组信息,规定箭头长度.png", replace
图9:arrowplot 添加分组信息,规定箭头长度

4.4 更为丰富的设定

我们可以在 arrowplot 命令中加入诸多 Stata 一般图形所支持的选项,以便对图形进行更为灵活的设定。

sysuse "nlsw88.dta", clear
decode occupation, gen(occu_str) maxlength(6)
#d ;
 arrowplot wage hours,   
   groupvar(occu_str) 
   cont(age collgrad) 
   groupname(occupation) 
   line(2) 
   title("工作时数与小时工资关系之行业特征") 
   subtitle("nlsw88.dta")
   xtitle("工作时间(每周工作小时数)")  
   xscale(titlegap(2))
   ytitle("小时工资") 
   scheme(s1mono) ;
#d cr
. graph export "图10:arrowplot 加入一般化图形选项.png", replace
图10:arrowplot 中文投稿模式图形

附:文中使用的 Stata dofiles


cd "D:\stata15\ado\personal\Jianshu\coefplot"
cap mkdir Fig
cd Fig 

*-下载 coefplot 命令

   ssc install coefplot
   
   help coefplot
   
 
*-图1
 . sysuse auto, clear 
 . reg price mpg length turn
 . coefplot, drop(_cons) 
 . graph export "图1:coefplot 基本绘图.png", replace
     
 
*-图2
  sysuse auto, clear
. reg price mpg length turn if foreign==0
. est store Domestic 

. reg price mpg length turn if foreign==1
. est store Foreign 

. coefplot Domestic Foreign,  ///
    drop(_cons)      ///
    xline(0, lp(dash) lc(black*0.3)) 
. graph export "图2:分组呈现系数估计值.png", replace
    
    
*-图3    
. coefplot    ///
  (Domestic, label("国产汽车") offset(0.05)  pstyle(p3)) ///
  (Foreign , label("进口汽车") offset(-0.05) pstyle(p4)) ///
   , drop(_cons) xline(0, lp(dash) lc(black*0.3))       
. graph export "图3:对每组的系数图形进行个性化设定.png", replace   


*-图4
. reg price mpg length turn
. est store multi

  foreach var in mpg length turn {
    qui reg price `var'
     est store  `var'
  }

. coefplot        ///
    (mpg \length \turn, label("单变量回归"))  ///
     (multi, label("多元回归"))                ///
     , drop(_cons) ///
     xline(0, lp(dash) lc(black*0.3)) 
  graph export "图4:多元回归系数和单变量回归系数比较.png", replace
 *-Note: 在 namelist 中用 \ 分隔存储的回归结果名称


*-图5
reg price mpg length turn if foreign==0
est store Domestic

reg price mpg length turn if foreign==1
est store Foreign

reg weight mpg length turn if foreign==0
est store Domestic_w  //更换了被解释变量

reg weight mpg length turn if foreign==1
est store Foreign_w   //更换了被解释变量

#d ;
coefplot 
  (Domestic, label("国产汽车"))  
  (Foreign , label("进口汽车")), bylabel(Price) || 
  (Domestic_w) (Foreign_w), bylabel(Weight) ||
  , 
  drop(_cons) byopts(xrescale) 
  xline(0, lp(dash) lc(black*0.3))
  ;
#d cr
graph export "图5:不同被解释变量回归的比较.png", replace


*-图7
. sysuse "nlsw88.dta", clear
. decode occupation, gen(occu_str) maxlength(6)
. arrowplot wage hours, groupvar(occu_str)
. graph export "图7:arrowplot 基本绘图.png", replace


*-图8
. arrowplot wage hours, groupvar(occu_str) ///
           control(age collgrad)
. graph export "图8:arrowplot 回归中加入控制变量.png", replace


*-图9
. arrowplot wage hours, groupvar(occu_str) ///
      groupname(occupation) line(2)
. graph export "图9:arrowplot 添加分组信息,规定箭头长度.png", replace


*-图10
sysuse "nlsw88.dta", clear
decode occupation, gen(occu_str) maxlength(6)
#d ;
 arrowplot wage hours,   
   groupvar(occu_str) 
   cont(age collgrad) 
   groupname(occupation) 
   line(2) 
   title("工作时数与小时工资关系之行业特征") 
   subtitle("nlsw88.dta")
   xtitle("工作时间(每周工作小时数)")  
   xscale(titlegap(2))
   ytitle("小时工资") 
   scheme(s1mono) ;
#d cr
. graph export "图10:arrowplot 加入一般化图形选项.png", replace

关于我们

联系我们

往期精彩推文

Stata连享会推文列表

Stata 现场培训报名中

连玉君Stata现场班报名中

Stata连享会二维码
上一篇 下一篇

猜你喜欢

热点阅读