user9439811
user9439811

Reputation: 69

Show categorical variables in reverse order in regression results

I am running a regression on categorical variables in Stata:

regress y i.age i.birth  

Part of the regression results output is below:

     coef
age  
28     .1
29    -.2

birth
1958   .2
1959   .5

I want the above results to be shown in the reverse order, so that I can export them to Excel using the putexcel command:

     coef
age  
29    -.2
28     .1

birth
1959   .5
1958   .2

I tried sorting the birth and age variables before regression, but this does not work.

Can someone help?

Upvotes: 0

Views: 393

Answers (2)

Nick Cox
Nick Cox

Reputation: 37208

You can reverse the coding and apply value labels to insist on what you will see:

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
------------------------------------------------------------------------------

If you wanted to see the same variable name as before, you could also do the following:

drop rep78
rename rep78_2 

Upvotes: 0

user8682794
user8682794

Reputation:

You cannot directly reverse the factor levels of a variable in the regression output.

However, if your end goal is to create a table in Microsoft Excel one way to do this is the following:

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

Normal results:

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

Reversed results:

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

Note that here I am using the commmunity-contributed command esttab to export the results.

You can make further tweaks if you fiddle with its options.


EDIT:

This solution manually creates dummies for esttab but instead you can also create a new variable with the reverse coding and use the opposite base level as @NickCox demonstrates in his solution.

Upvotes: 1

Related Questions