stata连享会stata计量经济学(stata)

Stata:边际效应分析

2018-05-30  本文已影响7110人  stata连享会

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

Stata连享会 精彩推文1 || 精彩推文2

1. 边际效应简介

1.1 引言

1.2 边际效应分析的必要性

1.3 边际效应的定义

2. 计算边际效应命令(margins)与绘图命令(marginsplot)

2.1 margins 命令

margins [marginlist] [if] [in] [weight] [, response_options options]

Note: 有关如何使用因子变量的介绍请参见 往期推文:stata中因子变量的使用方法

2.2 marginsplot 绘图命令

图2 marginsplot命令解释.png

3. margins与marginsplot命令举例

3.1 基础案例

.   sysuse "nlsw88.dta", clear
(NLSW, 1988 extract)

.   reg wage i.race i.industry

      Source |       SS           df       MS      Number of obs   =     2,232
-------------+----------------------------------   F(13, 2218)     =     13.00
       Model |  5246.90865        13  403.608358   Prob > F        =    0.0000
    Residual |  68870.3701     2,218  31.0506628   R-squared       =    0.0708
-------------+----------------------------------   Adj R-squared   =    0.0653
       Total |  74117.2788     2,231  33.2215503   Root MSE        =    5.5723

------------------------------------------------------------------------------------------
                    wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------------------+----------------------------------------------------------------
                    race |
                  black  |  -1.099771   .2743495    -4.01   0.000     -1.63778   -.5617626
                  other  |   .1317467   1.103937     0.12   0.905    -2.033111    2.296604
                         |
                industry |
                 Mining  |   9.469702   3.097312     3.06   0.002     3.395767    15.54364
           Construction  |   1.832193   1.702718     1.08   0.282    -1.506895     5.17128
          Manufacturing  |   2.021802   1.382963     1.46   0.144    -.6902358    4.733841
 Transport/Comm/Utility  |   5.891929   1.473775     4.00   0.000     3.001807    8.782052
 Wholesale/Retail Trade  |   .4639784    1.38559     0.33   0.738     -2.25321    3.181167
Finance/Ins/Real Estate  |    4.10511   1.410372     2.91   0.004     1.339321    6.870898
    Business/Repair Svc  |   1.888596   1.479264     1.28   0.202     -1.01229    4.789483
      Personal Services  |  -.9699527   1.466554    -0.66   0.508    -3.845914    1.906009
  Entertainment/Rec Svc  |   1.038595   1.911355     0.54   0.587    -2.709638    4.786828
  Professional Services  |   2.252467   1.365435     1.65   0.099    -.4251976    4.930132
  Public Administration  |   3.602952   1.415632     2.55   0.011     .8268485    6.379055
                         |
                   _cons |   5.879891   1.353025     4.35   0.000     3.226563    8.533219
------------------------------------------------------------------------------------------
.   margins i.race, atmeans    //前缀 i. 可省略不写

Adjusted predictions                            Number of obs     =      2,232
Model VCE    : OLS

Expression   : Linear prediction, predict()
at           : 1.race          =    .7289427 (mean)
               2.race          =    .2594086 (mean)
               3.race          =    .0116487 (mean)
               1.industry      =    .0076165 (mean)
               2.industry      =    .0017921 (mean)
               3.industry      =    .0129928 (mean)
               4.industry      =    .1644265 (mean)
               5.industry      =    .0403226 (mean)
               6.industry      =    .1491935 (mean)
               7.industry      =    .0860215 (mean)
               8.industry      =    .0385305 (mean)
               9.industry      =    .0434588 (mean)
               10.industry     =    .0076165 (mean)
               11.industry     =    .3691756 (mean)
               12.industry     =     .078853 (mean)

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
        race |
      white  |   8.067219   .1387997    58.12   0.000     7.795028     8.33941
      black  |   6.967447   .2345358    29.71   0.000     6.507515     7.42738
      other  |   8.198965   1.094967     7.49   0.000     6.051697    10.34623
------------------------------------------------------------------------------
marginsplot

3.2 交乘项案例

3.2.1 类别变量与类别变量交乘

.    reg wage i.industry collgrad##i.race

      Source |       SS           df       MS      Number of obs   =     2,232
-------------+----------------------------------   F(16, 2215)     =     23.20
       Model |  10639.3304        16  664.958149   Prob > F        =    0.0000
    Residual |  63477.9484     2,215   28.658216   R-squared       =    0.1435
-------------+----------------------------------   Adj R-squared   =    0.1374
       Total |  74117.2788     2,231  33.2215503   Root MSE        =    5.3533

------------------------------------------------------------------------------------------
                    wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------------------+----------------------------------------------------------------
                industry |
                 Mining  |   10.14131   2.976152     3.41   0.001     4.304972    15.97765
           Construction  |   2.198272   1.636227     1.34   0.179    -1.010428    5.406972
          Manufacturing  |   2.419311   1.329182     1.82   0.069     -.187261    5.025884
 Transport/Comm/Utility  |    6.12037    1.41599     4.32   0.000     3.343563    8.897177
 Wholesale/Retail Trade  |   .7578957   1.331357     0.57   0.569    -1.852943    3.368734
Finance/Ins/Real Estate  |   4.258568   1.355005     3.14   0.002     1.601355    6.915781
    Business/Repair Svc  |   1.894149   1.421387     1.33   0.183    -.8932415    4.681539
      Personal Services  |  -.3136217   1.410269    -0.22   0.824     -3.07921    2.451967
  Entertainment/Rec Svc  |   .7577804   1.837152     0.41   0.680     -2.84494      4.3605
  Professional Services  |   1.399826   1.313806     1.07   0.287    -1.176593    3.976246
  Public Administration  |   3.367397   1.360314     2.48   0.013     .6997726    6.035021
                         |
                collgrad |
           college grad  |   3.346222   .3197178    10.47   0.000     2.719244      3.9732
                         |
                    race |
                  black  |  -1.279729   .2951157    -4.34   0.000    -1.858461   -.7009966
                  other  |  -.3266847    1.31086    -0.25   0.803    -2.897328    2.243959
                         |
           collgrad#race |
     college grad#black  |   2.098549   .6590111     3.18   0.001     .8062049    3.390893
     college grad#other  |   .7141623     2.2373     0.32   0.750    -3.673262    5.101587
                         |
                   _cons |    5.20828   1.301126     4.00   0.000     2.656726    7.759834
------------------------------------------------------------------------------------------
.    margins collgrad#i.race, atmeans

Adjusted predictions                            Number of obs     =      2,232
Model VCE    : OLS

Expression   : Linear prediction, predict()
at           : 1.industry      =    .0076165 (mean)
               2.industry      =    .0017921 (mean)
               3.industry      =    .0129928 (mean)
               4.industry      =    .1644265 (mean)
               5.industry      =    .0403226 (mean)
               6.industry      =    .1491935 (mean)
               7.industry      =    .0860215 (mean)
               8.industry      =    .0385305 (mean)
               9.industry      =    .0434588 (mean)
               10.industry     =    .0076165 (mean)
               11.industry     =    .3691756 (mean)
               12.industry     =     .078853 (mean)
               0.collgrad      =    .7629928 (mean)
               1.collgrad      =    .2370072 (mean)
               1.race          =    .7289427 (mean)
               2.race          =    .2594086 (mean)
               3.race          =    .0116487 (mean)

-----------------------------------------------------------------------------------------
                        |            Delta-method
                        |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
------------------------+----------------------------------------------------------------
          collgrad#race |
not college grad#white  |   7.226442    .156204    46.26   0.000      6.92012    7.532763
not college grad#black  |   5.946713   .2504188    23.75   0.000     5.455632    6.437793
not college grad#other  |   6.899757   1.300669     5.30   0.000     4.349098    9.450416
    college grad#white  |   10.57266    .273137    38.71   0.000     10.03703    11.10829
    college grad#black  |   11.39148   .5335905    21.35   0.000     10.34509    12.43787
    college grad#other  |   10.96014   1.789176     6.13   0.000     7.451503    14.46878
-----------------------------------------------------------------------------------------
. marginsplot
.    margins, dydx(collgrad) at(race=(1 2 3)) atmeans

Conditional marginal effects                    Number of obs     =      2,232
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : 1.collgrad

1._at        : 1.industry      =    .0076165 (mean)
               2.industry      =    .0017921 (mean)
               3.industry      =    .0129928 (mean)
               4.industry      =    .1644265 (mean)
               5.industry      =    .0403226 (mean)
               6.industry      =    .1491935 (mean)
               7.industry      =    .0860215 (mean)
               8.industry      =    .0385305 (mean)
               9.industry      =    .0434588 (mean)
               10.industry     =    .0076165 (mean)
               11.industry     =    .3691756 (mean)
               12.industry     =     .078853 (mean)
               0.collgrad      =    .7629928 (mean)
               1.collgrad      =    .2370072 (mean)
               race            =           1

2._at        : 1.industry      =    .0076165 (mean)
               2.industry      =    .0017921 (mean)
               3.industry      =    .0129928 (mean)
               4.industry      =    .1644265 (mean)
               5.industry      =    .0403226 (mean)
               6.industry      =    .1491935 (mean)
               7.industry      =    .0860215 (mean)
               8.industry      =    .0385305 (mean)
               9.industry      =    .0434588 (mean)
               10.industry     =    .0076165 (mean)
               11.industry     =    .3691756 (mean)
               12.industry     =     .078853 (mean)
               0.collgrad      =    .7629928 (mean)
               1.collgrad      =    .2370072 (mean)
               race            =           2

3._at        : 1.industry      =    .0076165 (mean)
               2.industry      =    .0017921 (mean)
               3.industry      =    .0129928 (mean)
               4.industry      =    .1644265 (mean)
               5.industry      =    .0403226 (mean)
               6.industry      =    .1491935 (mean)
               7.industry      =    .0860215 (mean)
               8.industry      =    .0385305 (mean)
               9.industry      =    .0434588 (mean)
               10.industry     =    .0076165 (mean)
               11.industry     =    .3691756 (mean)
               12.industry     =     .078853 (mean)
               0.collgrad      =    .7629928 (mean)
               1.collgrad      =    .2370072 (mean)
               race            =           3

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
0.collgrad   |  (base outcome)
-------------+----------------------------------------------------------------
1.collgrad   |
         _at |
          1  |   3.346222   .3197178    10.47   0.000     2.719244      3.9732
          2  |   5.444771   .5929386     9.18   0.000     4.281997    6.607545
          3  |   4.060384   2.212164     1.84   0.067    -.2777476    8.398516
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
. marginsplot

3.2.2 类别变量与连续型变量交乘

.    reg wage i.industry c.hours##i.union

      Source |       SS           df       MS      Number of obs   =     1,864
-------------+----------------------------------   F(14, 1849)     =     19.48
       Model |  4165.55214        14  297.539439   Prob > F        =    0.0000
    Residual |  28235.7439     1,849  15.2708188   R-squared       =    0.1286
-------------+----------------------------------   Adj R-squared   =    0.1220
       Total |   32401.296     1,863      17.392   Root MSE        =    3.9078

------------------------------------------------------------------------------------------
                    wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------------------+----------------------------------------------------------------
                industry |
                 Mining  |   1.965311    2.98501     0.66   0.510    -3.889033    7.819655
           Construction  |   2.944549   1.427687     2.06   0.039     .1445001    5.744598
          Manufacturing  |   .8876583   1.149997     0.77   0.440    -1.367771    3.143088
 Transport/Comm/Utility  |    4.66455   1.206553     3.87   0.000     2.298201    7.030899
 Wholesale/Retail Trade  |  -.2176107    1.15411    -0.19   0.850    -2.481107    2.045886
Finance/Ins/Real Estate  |   2.714369   1.171926     2.32   0.021     .4159324    5.012805
    Business/Repair Svc  |   1.261646   1.238032     1.02   0.308    -1.166442    3.689734
      Personal Services  |  -1.602596   1.231961    -1.30   0.193    -4.018775    .8135844
  Entertainment/Rec Svc  |   1.454197   1.537525     0.95   0.344    -1.561271    4.469666
  Professional Services  |   1.454095   1.137893     1.28   0.201    -.7775962    3.685785
  Public Administration  |   2.658987   1.171222     2.27   0.023     .3619308    4.956043
                         |
                   hours |   .0565515   .0104406     5.42   0.000     .0360748    .0770282
                         |
                   union |
                  union  |   3.761049   .8990725     4.18   0.000     1.997745    5.524353
                         |
           union#c.hours |
                  union  |  -.0747591   .0226682    -3.30   0.001    -.1192171   -.0303012
                         |
                   _cons |   3.864411   1.193668     3.24   0.001     1.523332     6.20549
------------------------------------------------------------------------------------------
.    margins, dydx(hour) at(union=(0 1))

Average marginal effects                        Number of obs     =      1,864
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : hours

1._at        : union           =           0
2._at        : union           =           1

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
hours        |
         _at |
          1  |   .0565515   .0104406     5.42   0.000     .0360748    .0770282
          2  |  -.0182076    .020211    -0.90   0.368    -.0578463    .0214312
------------------------------------------------------------------------------
. marginsplot
.    keep if e(sample)
(382 observations deleted)
 
.    sum hours

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       hours |      1,864    37.62071    9.959845          1         80

.    margins, dydx(union) at(hours=(1(5)80))

Average marginal effects                        Number of obs     =      1,864
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : 1.union

1._at        : hours           =           1
2._at        : hours           =           6
3._at        : hours           =          11
4._at        : hours           =          16
5._at        : hours           =          21
6._at        : hours           =          26
7._at        : hours           =          31
8._at        : hours           =          36
9._at        : hours           =          41
10._at       : hours           =          46
11._at       : hours           =          51
12._at       : hours           =          56
13._at       : hours           =          61
14._at       : hours           =          66
15._at       : hours           =          71
16._at       : hours           =          76

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
0.union      |  (base outcome)
-------------+----------------------------------------------------------------
1.union      |
         _at |
          1  |    3.68629   .8771081     4.20   0.000     1.966064    5.406516
          2  |   3.312494   .7678999     4.31   0.000     1.806452    4.818536
          3  |   2.938699   .6600865     4.45   0.000     1.644105    4.233292
          4  |   2.564903   .5544822     4.63   0.000     1.477426     3.65238
          5  |   2.191107   .4526359     4.84   0.000     1.303376    3.078838
          6  |   1.817312   .3577712     5.08   0.000     1.115634     2.51899
          7  |   1.443516   .2771526     5.21   0.000     .8999512    1.987081
          8  |    1.06972   .2265375     4.72   0.000     .6254243    1.514017
          9  |   .6959248   .2269741     3.07   0.002     .2507724    1.141077
         10  |   .3221292   .2782222     1.16   0.247    -.2235335    .8677918
         11  |  -.0516665   .3591522    -0.14   0.886    -.7560529    .6527199
         12  |  -.4254621   .4541644    -0.94   0.349    -1.316191    .4652668
         13  |  -.7992578   .5560869    -1.44   0.151    -1.889882    .2913665
         14  |  -1.173053   .6617344    -1.77   0.076    -2.470879    .1247717
         15  |  -1.546849   .7695742    -2.01   0.045    -3.056175   -.0375234
         16  |  -1.920645   .8787996    -2.19   0.029    -3.644189   -.1971008
------------------------------------------------------------------------------
Note: dy/dx for factor levels is the discrete change from the base level.
. marginsplot

3.2.3 连续型变量与连续型变量交乘

.    sysuse "auto.dta", clear
(1978 Automobile Data)

.    reg price foreign c.mpg##c.weight

      Source |       SS           df       MS      Number of obs   =        74
-------------+----------------------------------   F(4, 69)        =     18.96
       Model |   332566402         4  83141600.6   Prob > F        =    0.0000
    Residual |   302498994        69  4384043.39   R-squared       =    0.5237
-------------+----------------------------------   Adj R-squared   =    0.4961
       Total |   635065396        73  8699525.97   Root MSE        =    2093.8

--------------------------------------------------------------------------------
         price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
       foreign |   3369.814   691.4218     4.87   0.000     1990.466    4749.163
           mpg |   292.8295   162.2982     1.80   0.076    -30.94655    616.6056
        weight |   5.382755   1.198909     4.49   0.000     2.990997    7.774512
               |
c.mpg#c.weight |  -.1189117   .0636245    -1.87   0.066    -.2458392    .0080157
               |
         _cons |  -10105.04   4023.204    -2.51   0.014    -18131.11   -2078.967
--------------------------------------------------------------------------------
.    keep if e(sample)
(0 observations deleted)

.    sum mpg  //查看 mpg 的基本统计量

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
         mpg |         74     21.2973    5.785503         12         41

.    margins, dydx(weight) at(mpg=(12(2)41))

Average marginal effects                        Number of obs     =         74
Model VCE    : OLS

Expression   : Linear prediction, predict()
dy/dx w.r.t. : weight

1._at        : mpg             =          12
2._at        : mpg             =          14
3._at        : mpg             =          16
4._at        : mpg             =          18
5._at        : mpg             =          20
6._at        : mpg             =          22
7._at        : mpg             =          24
8._at        : mpg             =          26
9._at        : mpg             =          28
10._at       : mpg             =          30
11._at       : mpg             =          32
12._at       : mpg             =          34
13._at       : mpg             =          36
14._at       : mpg             =          38
15._at       : mpg             =          40

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
weight       |
         _at |
          1  |   3.955814   .6732094     5.88   0.000     2.612798     5.29883
          2  |    3.71799   .6344514     5.86   0.000     2.452294    4.983686
          3  |   3.480167   .6198636     5.61   0.000     2.243573    4.716761
          4  |   3.242343   .6311243     5.14   0.000     1.983285    4.501402
          5  |    3.00452   .6669255     4.51   0.000      1.67404       4.335
          6  |   2.766696   .7236338     3.82   0.000     1.323086    4.210306
          7  |   2.528873   .7967979     3.17   0.002     .9393044    4.118441
          8  |   2.291049   .8823336     2.60   0.011      .530842    4.051257
          9  |   2.053226   .9769968     2.10   0.039     .1041705    4.002281
         10  |   1.815402   1.078387     1.68   0.097    -.3359202    3.966725
         11  |   1.577579   1.184777     1.33   0.187    -.7859873    3.941145
         12  |   1.339755   1.294937     1.03   0.304    -1.243573    3.923084
         13  |   1.101932   1.407981     0.78   0.437    -1.706913    3.910777
         14  |   .8641083   1.523268     0.57   0.572    -2.174727    3.902944
         15  |   .6262849   1.640324     0.38   0.704    -2.646072    3.898641
------------------------------------------------------------------------------
marginsplot

3.3 非线性模型案例 (Logit Model)

.    sysuse "auto.dta", clear
(1978 Automobile Data)

.    logit foreign mpg weight

Iteration 0:   log likelihood =  -45.03321  
Iteration 1:   log likelihood = -29.238536  
Iteration 2:   log likelihood = -27.244139  
Iteration 3:   log likelihood = -27.175277  
Iteration 4:   log likelihood = -27.175156  
Iteration 5:   log likelihood = -27.175156  

Logistic regression                             Number of obs     =         74
                                                LR chi2(2)        =      35.72
                                                Prob > chi2       =     0.0000
Log likelihood = -27.175156                     Pseudo R2         =     0.3966

------------------------------------------------------------------------------
     foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         mpg |  -.1685869   .0919175    -1.83   0.067    -.3487418     .011568
      weight |  -.0039067   .0010116    -3.86   0.000    -.0058894    -.001924
       _cons |   13.70837   4.518709     3.03   0.002     4.851859    22.56487
------------------------------------------------------------------------------
. margins, dydx(mpg)

Average marginal effects                        Number of obs     =         74
Model VCE    : OIM

Expression   : Pr(foreign), predict()
dy/dx w.r.t. : mpg

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         mpg |  -.0197187   .0096987    -2.03   0.042    -.0387277   -.0007096
------------------------------------------------------------------------------

. margins, dydx(weight)

Average marginal effects                        Number of obs     =         74
Model VCE    : OIM

Expression   : Pr(foreign), predict()
dy/dx w.r.t. : weight

------------------------------------------------------------------------------
             |            Delta-method
             |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      weight |  -.0004569   .0000571    -8.01   0.000    -.0005688   -.0003451
------------------------------------------------------------------------------

4. marginscontplot命令及举例

4.1 命令简介

4.2 语法

{marginscontplot|mcp} xvar1 [(xvar1a [xvar1b ...])] [xvar2 [(xvar2a [xvar2b ...])]] [if] [in] [, options]
图9 marginscontplot命令解释.png

4.3 安装

4.4 使用案例

4.4.1 单个变量

.    sysuse "nlsw88.dta", clear
(NLSW, 1988 extract)

.    reg wage i.industry c.hours##i.union

      Source |       SS           df       MS      Number of obs   =     1,864
-------------+----------------------------------   F(14, 1849)     =     19.48
       Model |  4165.55214        14  297.539439   Prob > F        =    0.0000
    Residual |  28235.7439     1,849  15.2708188   R-squared       =    0.1286
-------------+----------------------------------   Adj R-squared   =    0.1220
       Total |   32401.296     1,863      17.392   Root MSE        =    3.9078

------------------------------------------------------------------------------------------
                    wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------------------+----------------------------------------------------------------
                industry |
                 Mining  |   1.965311    2.98501     0.66   0.510    -3.889033    7.819655
           Construction  |   2.944549   1.427687     2.06   0.039     .1445001    5.744598
          Manufacturing  |   .8876583   1.149997     0.77   0.440    -1.367771    3.143088
 Transport/Comm/Utility  |    4.66455   1.206553     3.87   0.000     2.298201    7.030899
 Wholesale/Retail Trade  |  -.2176107    1.15411    -0.19   0.850    -2.481107    2.045886
Finance/Ins/Real Estate  |   2.714369   1.171926     2.32   0.021     .4159324    5.012805
    Business/Repair Svc  |   1.261646   1.238032     1.02   0.308    -1.166442    3.689734
      Personal Services  |  -1.602596   1.231961    -1.30   0.193    -4.018775    .8135844
  Entertainment/Rec Svc  |   1.454197   1.537525     0.95   0.344    -1.561271    4.469666
  Professional Services  |   1.454095   1.137893     1.28   0.201    -.7775962    3.685785
  Public Administration  |   2.658987   1.171222     2.27   0.023     .3619308    4.956043
                         |
                   hours |   .0565515   .0104406     5.42   0.000     .0360748    .0770282
                         |
                   union |
                  union  |   3.761049   .8990725     4.18   0.000     1.997745    5.524353
                         |
           union#c.hours |
                  union  |  -.0747591   .0226682    -3.30   0.001    -.1192171   -.0303012
                         |
                   _cons |   3.864411   1.193668     3.24   0.001     1.523332     6.20549
------------------------------------------------------------------------------------------

.    marginscontplot hours, ci 
图10 当其他变量取均值,hours取不同值时,wage的预测边际值.png
.    marginscontplot hours, ci var1(4)
图11 当其他变量取均值,hours取值为4个数值时(均匀分布于hours变量的取值范围)wage的预测边际值.png
.    marginscontplot hours, ci at1(10(5)40)
图12 当其他变量取均值,hours取值为10至50之间时,wage的预测边际值.png

4.4.2 两个变量

在3.2.2节案例中,由于加入了 unionhours 的交乘项并且该系数在统计上显著,由此,我们还想分别计算当 hours 取值不同时,工会成员与非工会成员的 wage 的预测边际值。因此,需要在 marginscontplot 命令中加入两个变量,stata 中的命令和结果如下所示:

.    marginscontplot hours union, ci at1(1(5)80)
*-或写为
.    marginscontplot hours union, ci at1(1(5)80) at2(0 1)
图13 当其他变量取均值,hours取不同值时,工会与非工会成员的wage的预测边际值.png

4.4.3 对变量的数值转换

.    gen lnhours = ln(hours)
(4 missing values generated)

.    reg wage i.industry collgrad union lnhours

      Source |       SS           df       MS      Number of obs   =     1,864
-------------+----------------------------------   F(14, 1849)     =     43.37
       Model |  8010.08706        14  572.149076   Prob > F        =    0.0000
    Residual |   24391.209     1,849  13.1915679   R-squared       =    0.2472
-------------+----------------------------------   Adj R-squared   =    0.2415
       Total |   32401.296     1,863      17.392   Root MSE        =     3.632

------------------------------------------------------------------------------------------
                    wage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------------------+----------------------------------------------------------------
                industry |
                 Mining  |   2.610535   2.774693     0.94   0.347    -2.831325    8.052395
           Construction  |   2.840079   1.326651     2.14   0.032     .2381882    5.441971
          Manufacturing  |   1.231468   1.069077     1.15   0.250     -.865257    3.328193
 Transport/Comm/Utility  |   4.943764   1.121633     4.41   0.000     2.743963    7.143564
 Wholesale/Retail Trade  |  -.1333967    1.07259    -0.12   0.901    -2.237011    1.970218
Finance/Ins/Real Estate  |   2.806101   1.089317     2.58   0.010     .6696813    4.942521
    Business/Repair Svc  |   .9528829   1.150232     0.83   0.408    -1.303006    3.208772
      Personal Services  |  -1.255364   1.144976    -1.10   0.273    -3.500946    .9902172
  Entertainment/Rec Svc  |   1.157725   1.428893     0.81   0.418    -1.644688    3.960138
  Professional Services  |   .4944097   1.058875     0.47   0.641    -1.582307    2.571126
  Public Administration  |   2.404413   1.088813     2.21   0.027     .2689805    4.539845
                         |
                collgrad |   3.701502   .2091694    17.70   0.000     3.291269    4.111735
                   union |   .6846933   .2044257     3.35   0.001     .2837639    1.085623
                 lnhours |   .4712976   .2119332     2.22   0.026     .0556441    .8869511
                   _cons |   3.742689   1.285111     2.91   0.004     1.222268     6.26311
------------------------------------------------------------------------------------------
.    keep if e(sample)
(382 observations deleted)

.    sum hours

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       hours |      1,864    37.62071    9.959845          1         80

.    range h r(min) r(max) 20
(1,844 missing values generated)

.    gen lnh = ln(h)
(1,844 missing values generated)

.    marginscontplot hours(lnhours), ci var1(h(lnh))
图14 对hours变量取对数后wage的预测边际值.png

5. 相关阅读资料

关于我们

Stata连享会 精彩推文1 || 精彩推文2

联系我们

往期精彩推文

Stata连享会推文列表

……Stata 连享会精彩推文……
Stata连享会 精彩推文1 || 精彩推文2


欢迎加入Stata连享会(公众号: StataChina)
上一篇 下一篇

猜你喜欢

热点阅读