楼主: 黃河泉
49032 58

[学习心得] 如何画(倒)U 型图?   [推广有奖]

21
小丸子ddx 发表于 2018-11-27 16:14:39 |只看作者 |坛友微信交流群
黄老师,我想问一下,为什么我安装marginscontplot,安装不上去?就一直显示
ssc install: "marginscontplot" not found at SSC, type search marginscontplot
(To find all packages at SSC that start with m, type ssc describe m)

使用道具

22
黃河泉 在职认证  发表于 2018-11-27 17:12:02 |只看作者 |坛友微信交流群
小丸子ddx 发表于 2018-11-27 16:14
黄老师,我想问一下,为什么我安装marginscontplot,安装不上去?就一直显示
ssc install: "marginscontpl ...
请 search marginscontplot 后安装。

使用道具

23
小丸子ddx 发表于 2018-11-30 09:01:04 |只看作者 |坛友微信交流群
黃河泉 发表于 2018-11-27 17:12
请 search marginscontplot 后安装。
谢谢黄老师,我已经安装上了

使用道具

24
arlionn 在职认证  发表于 2018-12-2 09:51:38 |只看作者 |坛友微信交流群
放一个我在 Stata 寒假现场班中的 Stata 范例,供参考 :https://gitee.com/arlionn/stata_training/blob/master/README.md
  1. *---------------------------
  2. *-4.5.2  特殊交乘项: 平方项  (Wooldridge, Sec6.3, p.189-194)
  3.   
  4.     *-二次函数回顾
  5.         *
  6.         *  y = a*x^2 + b*x + c
  7.         *
  8.         *  转折点: tpoint = -b/2a
  9.         *
  10.         *  边际效果: dy/dx = b + 2a*x
  11.         
  12.         
  13.     *-转折点(The turnning point)
  14.       
  15.           sysuse "nlsw88.dta", clear
  16.           global xx "hours age tenure married south i.race"
  17.           gen ttl_exp2 = ttl_exp^2
  18.           reg wage ttl_exp ttl_exp2 $xx
  19.       *------------------------------------------------
  20.       *     wage |   Coef.   Std. Err.      t    P>|t|
  21.       *----------+-------------------------------------
  22.       *  ttl_exp |   0.492      0.110     4.49   0.000
  23.       * ttl_exp2 |  -0.009      0.005    -1.99   0.046
  24.       *                  (ommited)                     
  25.       *------------------------------------------------
  26.          
  27.           keep if e(sample)==1  //删除缺漏值
  28.         
  29.           predict wage_hat if e(sample)
  30.         
  31.           twoway (scatter wage ttl_exp) (qfit wage_hat ttl_exp )
  32.   
  33.     *-转折点的确定
  34.           global tp: dis %3.1f -_b[ttl_exp]/(2*_b[ttl_exp2])
  35.           dis "Turn point = " $tp
  36.         
  37.         *---------------------------图示-----------------------------
  38.           local b0 = _b[_cons]
  39.           local b1 = _b[ttl_exp]
  40.           local b2 = _b[ttl_exp2]
  41.           sum ttl_exp
  42.           local min:  dis %3.1f r(min)
  43.           local max:  dis %3.1f r(max)
  44.       #delimit        
  45.           twoway ( function y = `b2'*x^2 + `b1'*x + `b0',
  46.                    range(`min' `max') )
  47.                  ( function y = `b2'*x^2 + `b1'*x + `b0',
  48.                    range(`max' 40) lp(dash) )
  49.                          ,
  50.                          ytitle("wage") xtitle("ttl_exp")
  51.                          xline(`min',lc(red) lw(*1.5))
  52.                          xline($tp,  lp(dash) lc(green))
  53.                          xline(`max',lc(red) lw(*1.5))
  54.                          xlabel(`min' "Min (`min')" 5(5)25
  55.                                  $tp "TP ($tp)" `max' "Max (`max')", angle(50))
  56.                          legend(off)
  57.                          caption("The turn point is: $tp")
  58.                          ;
  59.           #delimit cr
  60.           graph export "$Out\fig_Ushape.png", replace
  61.     *---------------------------图示-----------------------------
  62.         
  63.         *-转折点右侧的观察值占比:要注意其实际意义
  64.           keep if e(sample)
  65.           count if ttl_exp>$tp
  66.           sum ttl_exp
  67.           *-结论: 此时的转折点不具有实际意义                                                  


  68.         *-使用 margin 命令分析边际效应 (推荐使用)
  69.          
  70.           reg wage c.ttl_exp##c.ttl_exp $xx
  71.                
  72.           sum ttl_exp
  73.           global min:  dis %4.1f r(min)
  74.           global max:  dis %4.1f r(max)
  75.           margins, dydx(ttl_exp) at(ttl_exp=($min 1(3)28 $max))
  76.       *-----------------------------------------------------------
  77.       *       |         Delta-method
  78.       *       |  dy/dx     Std. Err.    t    P>|t|      [95% CI]
  79.       *-------+---------------------------------------------------
  80.       *ttl_exp|      
  81.       *   _at |
  82.       *    1  |  0.490      0.109     4.51   0.000    0.277  0.703
  83.       *    2  |  0.474      0.101     4.69   0.000    0.276  0.672
  84.       *    3  |  0.419      0.076     5.56   0.000    0.271  0.568
  85.       *    4  |  0.365      0.052     7.03   0.000    0.263  0.467
  86.       *    5  |  0.311      0.034     9.04   0.000    0.243  0.378
  87.       *    6  |  0.257      0.034     7.56   0.000    0.190  0.323
  88.       *    7  |  0.202      0.051     3.97   0.000    0.102  0.302
  89.       *    8  |  0.148      0.074     1.99   0.047    0.002  0.294
  90.       *    9  |  0.094      0.100     0.94   0.347   -0.102  0.289
  91.       *   10  |  0.040      0.126     0.31   0.754   -0.207  0.286
  92.       *   11  | -0.015      0.152    -0.10   0.923   -0.314  0.284
  93.       *   12  | -0.031      0.160    -0.19   0.847   -0.346  0.284
  94.       *-----------------------------------------------------------
  95.          
  96.           marginsplot, xlabel(, format(%3.1f) angle(60)) //绘图,附带 95% 置信区间
  97.           graph export "$Out\fig_Wage_marginplot_A.png", replace
  98.          
  99.           marginsplot, addplot(function y=0, range($min $max)  ///
  100.                            lcolor(red) lpattern(dash) legend(off)) ///
  101.                                    xlabel(, format(%3.1f) angle(60)) ///
  102.                                    ylabel(, format(%2.1f) angle(0))
  103.           graph export "$Out\fig_Wage_marginplot_B.png", replace  
复制代码


fig_Ushape.png fig_Ushape.png

fig_Wage_marginplot_A

fig_Wage_marginplot_B


Stata 寒假现场班    https://gitee.com/arlionn/stata_training/blob/master/README.md


已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zshkjj + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

25
黃河泉 在职认证  发表于 2018-12-2 10:12:31 |只看作者 |坛友微信交流群
arlionn 发表于 2018-12-2 09:51
放一个我在 Stata 寒假现场班中的 Stata 范例,供参考 :https://gitee.com/arlionn/stata_training/blob/m ...
1. 感谢连老师提供的精彩例子。2. 我已经将其设定为精彩回复,这样一来,其他论坛朋友就可以更清楚并容易注意到此精彩例子。3. 不知您有无注意到,好像"大部分"的情况与例子,都是倒 U 而非正 U 型关系?

使用道具

26
arlionn 在职认证  发表于 2018-12-5 11:15:20 |只看作者 |坛友微信交流群
其实,我那个例子是想说明:多数情况下我们只看到了 U 型或 倒 U 型的半边。也就是说,在转折点左侧或右侧的样本数很少,此时下结论时要谨慎。

使用道具

27
黃河泉 在职认证  发表于 2018-12-5 12:10:48 |只看作者 |坛友微信交流群
arlionn 发表于 2018-12-5 11:15
其实,我那个例子是想说明:多数情况下我们只看到了 U 型或 倒 U 型的半边。也就是说,在转折点左侧或右侧的 ...
也没错!

使用道具

28
zyj592991367 发表于 2019-7-5 16:26:04 |只看作者 |坛友微信交流群
黄老师,我用了你给的画U型关系图的命令,
xi:reg ca3 sub c.sub#c.sub down bm lev size roa indr growth ci lnky top1 lnage mng com ex gdprate ppi i.year i.indu,robust
marginscontplot sub, ci
为什么stata一直提示invalid numlist has too many elements?

使用道具

29
黃河泉 在职认证  发表于 2019-7-5 16:42:06 |只看作者 |坛友微信交流群
zyj592991367 发表于 2019-7-5 16:26
黄老师,我用了你给的画U型关系图的命令,
xi:reg ca3 sub c.sub#c.sub down bm lev size roa indr growth ...
看不出问题!

使用道具

30
NXi1537782054 发表于 2019-8-20 19:37:34 |只看作者 |坛友微信交流群
感谢分享,想请问一下这个图的含义是什么?是pcgdp的边际作用么?如果是的话,跟后面您给出的marginsplot命令中的结果图有什么区别吖?谢谢您!

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-19 15:25