在回归结果中以相反的顺序显示分类变量

时间:2019-05-22 04:09:44

标签: stata

我正在对Stata中的类别变量进行回归:

regress y i.age i.birth  

部分回归结果输出如下:

     coef
age  
28     .1
29    -.2

birth
1958   .2
1959   .5

我希望上述结果以相反的顺序显示,以便可以使用putexcel命令将它们导出到Excel:

     coef
age  
29    -.2
28     .1

birth
1959   .5
1958   .2

我尝试在回归之前对birthage变量进行排序,但这不起作用。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

您不能在回归输出中直接反转变量的因子水平。

但是,如果最终目标是在Microsoft Excel中创建表,则执行以下操作的一种方法是:

sysuse auto.dta, clear
estimates clear

keep if !missing(rep78)
tabulate rep78, generate(rep)

regress price mpg weight rep2-rep5
estimates store r1

regress price mpg weight rep5 rep4 rep3 rep2
estimates store r2

正常结果:

esttab r1 using results.csv, label refcat(rep2 "Repair record", nolabel)

------------------------------------
                              (1)   
                            Price   
------------------------------------
Mileage (mpg)              -63.10   
                          (-0.72)   

Weight (lbs.)               2.093** 
                           (3.29)   

Repair record                       

rep78==     2.0000          753.7   
                           (0.39)   

rep78==     3.0000         1349.4   
                           (0.76)   

rep78==     4.0000         2030.5   
                           (1.12)   

rep78==     5.0000         3376.9   
                           (1.78)   

Constant                   -599.0   
                          (-0.15)   
------------------------------------
Observations                   69   
------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

相反的结果:

esttab r2 using results.csv, label refcat(rep5 "Repair record", nolabel)

------------------------------------
                              (1)   
                            Price   
------------------------------------
Mileage (mpg)              -63.10   
                          (-0.72)   

Weight (lbs.)               2.093** 
                           (3.29)   

Repair record                       

rep78==     5.0000         3376.9   
                           (1.78)   

rep78==     4.0000         2030.5   
                           (1.12)   

rep78==     3.0000         1349.4   
                           (0.76)   

rep78==     2.0000          753.7   
                           (0.39)   

Constant                   -599.0   
                          (-0.15)   
------------------------------------
Observations                   69   
------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001

请注意,这里我使用的是社区贡献的命令esttab来导出结果。

如果弄弄它的选项,可以做进一步的调整。


编辑:

此解决方案手动为esttab创建虚拟变量,但您也可以使用反向编码创建新变量,并使用与@NickCox在其解决方案中演示的相反的基本级别。

答案 1 :(得分:1)

您可以反转编码并应用值标签以坚持您将看到的内容:

sysuse auto, clear 

generate rep78_2 = 6 - rep78    

label define new 1 "5" 2 "4" 3 "3" 4 "2" 5 "1"
label values rep78_2 new 

regress mpg i.rep78_2 

      Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(4, 64)        =      4.91
       Model |  549.415777         4  137.353944   Prob > F        =    0.0016
    Residual |  1790.78712        64  27.9810488   R-squared       =    0.2348
-------------+----------------------------------   Adj R-squared   =    0.1869
       Total |   2340.2029        68  34.4147485   Root MSE        =    5.2897

------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     rep78_2 |
          4  |   -5.69697    2.02441    -2.81   0.006    -9.741193   -1.652747
          3  |  -7.930303    1.86452    -4.25   0.000    -11.65511   -4.205497
          2  |  -8.238636   2.457918    -3.35   0.001    -13.14889    -3.32838
          1  |  -6.363636   4.066234    -1.56   0.123    -14.48687    1.759599
             |
       _cons |   27.36364   1.594908    17.16   0.000     24.17744    30.54983
------------------------------------------------------------------------------

regress mpg ib5.rep78_2 

      Source |       SS           df       MS      Number of obs   =        69
-------------+----------------------------------   F(4, 64)        =      4.91
       Model |  549.415777         4  137.353944   Prob > F        =    0.0016
    Residual |  1790.78712        64  27.9810488   R-squared       =    0.2348
-------------+----------------------------------   Adj R-squared   =    0.1869
       Total |   2340.2029        68  34.4147485   Root MSE        =    5.2897

------------------------------------------------------------------------------
         mpg |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     rep78_2 |
          5  |   6.363636   4.066234     1.56   0.123    -1.759599    14.48687
          4  |   .6666667   3.942718     0.17   0.866    -7.209818    8.543152
          3  |  -1.566667   3.863059    -0.41   0.686    -9.284014    6.150681
          2  |     -1.875   4.181884    -0.45   0.655    -10.22927    6.479274
             |
       _cons |         21   3.740391     5.61   0.000     13.52771    28.47229
------------------------------------------------------------------------------

如果您希望看到与以前相同的变量名,则还可以执行以下操作:

drop rep78
rename rep78_2