Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I use outreg2 to output value labels of interactions of factor variables?

    Dear all,

    I am running a few regressions that include factor variables and their interactions, and I would like outreg2 to display factor value labels instead of factor values in the interactions. Here's an example of what I mean.

    I create a fictitious MAXSPEED variable with 4 levels:
    Code:
    sysuse auto, clear
    set seed 1234
    
    gen maxspeed = round(runiform()*3)+1
    label define speed 1 "Light" 2 "Ridiculous" 3 "Ludicrous" 4 "Plaid"
    label values maxspeed speed
    Run the following regression and save the results to 'myfile.txt':
    Code:
    reg price mpg weight i.foreign i.foreign#i.maxspeed
    outreg2 using myfile, seeout replace label
    The regression produces the following output
    Code:
          Source |       SS       df       MS              Number of obs =      74
    -------------+------------------------------           F(  9,    64) =    8.10
           Model |   338256360     9    37584040           Prob > F      =  0.0000
        Residual |   296809036    64  4637641.19           R-squared     =  0.5326
    -------------+------------------------------           Adj R-squared =  0.4669
           Total |   635065396    73  8699525.97           Root MSE      =  2153.5
    
    --------------------------------------------------------------------------------------
                   price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    ---------------------+----------------------------------------------------------------
                     mpg |   54.82039   77.72798     0.71   0.483    -100.4591    210.0999
                  weight |   3.458109   .6816211     5.07   0.000     2.096414    4.819804
                         |
                 foreign |
                Foreign  |   5116.416    1318.91     3.88   0.000      2481.59    7751.242
                         |
        foreign#maxspeed |
    Domestic#Ridiculous  |   600.8878   850.1522     0.71   0.482    -1097.487    2299.262
     Domestic#Ludicrous  |   410.7065   809.4843     0.51   0.614    -1206.424    2027.837
         Domestic#Plaid  |   1054.297   981.5494     1.07   0.287    -906.5734    3015.167
     Foreign#Ridiculous  |  -1792.409   1304.103    -1.37   0.174    -4397.654    812.8358
      Foreign#Ludicrous  |  -1487.636   1435.987    -1.04   0.304    -4356.349    1381.078
          Foreign#Plaid  |   762.8078   1876.151     0.41   0.686    -2985.236    4510.851
                         |
                   _cons |  -6947.346   3678.932    -1.89   0.064    -14296.86    402.1645
    -----------------------------------------------------------------------------------
    But outreg2 only returns factor values for the interactions:
    Code:
        (1)    
    VARIABLES    Price    Standard errors in parentheses
            *** p<0.01, ** p<0.05, * p<0.1
    Mileage (mpg)    54.82    
        (77.73)    
    Weight (lbs.)    3.458***    
        (0.682)    
    Car type = 1, Foreign    5,116***    
        (1,319)    
    0b.foreign#1b.maxspeed    0    
        (0)    
    0b.foreign#2.maxspeed    600.9    
        (850.2)    
    0b.foreign#3.maxspeed    410.7    
        (809.5)    
    0b.foreign#4.maxspeed    1,054    
        (981.5)    
    1o.foreign#1b.maxspeed    0    
        (0)    
    1.foreign#2.maxspeed    -1,792    
        (1,304)    
    1.foreign#3.maxspeed    -1,488    
        (1,436)    
    1.foreign#4.maxspeed    762.8    
        (1,876)    
    Constant    -6,947*    
        (3,679)    
            
    Observations    74    
    R-squared    0.533
    I am aware of the fact that when the option 'label' is used, the value labels are printed with the main effects (as with Car Type above). But what if we did not want to include the main effects? Regardless of this, some of us might find the output in Stata's results window more aesthetically pleasing.

    A similar question was asked recently by another user (Michael Anbar) and got no reply here but he did get a suggestion from Stacktoverflow to use estout instead (http://stackoverflow.com/q/29679981/2766558). I post it again because I couldn't find an answer in the pre-April archives and, if there is no option to do this, it would be a great addition to outreg2.

    Thanks in advance.

    P.S. This is my first post. My apologies for any rookie mistakes I may have made (including not searching through this forum or the archives more thoroughly).
    Last edited by Carlos Zambrana; 30 Jun 2015, 05:03. Reason: Forgot to include tags

  • #2
    [Update] For other rookies like me, esttab does the trick. I would still find it helpful to know whether this is possible with outreg2.

    Code:
    esttab using myfile, label replace
    
    
    ------------------------------------
                                  (1)   
                                Price   
    ------------------------------------
    Mileage (mpg)               54.82   
                               (0.71)   
    
    Weight (lbs.)               3.458***
                               (5.07)   
    
    Domestic                        0   
                                  (.)   
    
    Foreign                    5116.4***
                               (3.88)   
    
    Domestic # Light                0   
                                  (.)   
    
    Domestic # Ridicul~s        600.9   
                               (0.71)   
    
    Domestic # Ludicrous        410.7   
                               (0.51)   
    
    Domestic # Plaid           1054.3   
                               (1.07)   
    
    Foreign # Light                 0   
                                  (.)   
    
    Foreign # Ridiculous      -1792.4   
                              (-1.37)   
    
    Foreign # Ludicrous       -1487.6   
                              (-1.04)   
    
    Foreign # Plaid             762.8   
                               (0.41)   
    
    Constant                  -6947.3   
                              (-1.89)   
    ------------------------------------
    Observations                   74   
    ------------------------------------
    t statistics in parentheses
    * p<0.05, ** p<0.01, *** p<0.001

    Comment


    • #3
      outreg2 (SSC, as you are asked to explain) is much downloaded, but in practice little supported by its users. Its author is not a member here but is believed to be responsive to direct email.

      Comment


      • #4
        Thanks for the prompt reply Nick. I'll try that.

        Comment


        • #5
          Hello Carlos

          Did you ever find a solution to display factor value labels instead of factor values in the interactions in outreg2?

          Best, Lars

          Comment


          • #6
            Not yet, but I'll post here if I do.

            Comment


            • #7
              OK, thank you

              Comment


              • #8
                Hi Carlos/Lars -- I was having the exact same issue recently, and I think I found a workaround (though it's cumbersome).

                Step 1: Run the same code as in #1 above:
                Code:
                sysuse auto, clear
                set seed 1234
                gen maxspeed = round(runiform()*3)+1
                label define speed 1 "Light" 2 "Ridiculous" 3 "Ludicrous" 4 "Plaid"
                label values maxspeed speed
                reg price mpg weight i.foreign i.foreign#i.maxspeed
                outreg2 using "$out\outreg_attempt1.txt", replace label
                Step 2: For each categorical variable you're interacting, create a little crosswalk that matches the numeric values of that variable to its value labels (recast as strings). In this context:
                Code:
                foreach v in foreign maxspeed {
                preserve decode `v', gen(`v'_label) // creates a string variable containing the value labels of `v' gen `v'_id = `v' // creates an un-encoded numeric variable equal to `v' keep `v'_label `v'_id duplicates drop save "$tempDir\\`v'_mapping", replace restore
                }
                Step 3: Read in the outreg file created in Step 1 and create a temporary variable to preserve the sort order of the file:
                Code:
                import delimited using "$out\outreg_attempt1.txt", clear
                gen sort_order = _n
                Step 4: Extract the values of the interacted categorical variables from the first column, then use those values to merge in your crosswalks from Step 2:
                Code:
                gen foreign_id = substr(v1,1,1) // the first character of "v1" contains the numeric values of "foreign"
                replace foreign_id = "" if !regexm(v1,"foreign#")
                destring foreign_id, replace
                merge m:1 foreign_id using "$tempDir\\foreign_mapping", keep(1 3) nogen // merge in crosswalk from Step 2
                
                gen maxspeed_id = substr(v1,strpos(v1,"#")+1,1) // the first character after "#" in "v1" contains the numeric values of "maxspeed"
                replace maxspeed_id = "" if !regexm(v1,"foreign#")
                destring maxspeed_id, replace
                merge m:1 maxspeed_id using "$tempDir\\maxspeed_mapping", keep(1 3) nogen // merge in crosswalk from Step 2
                Step 5: Use the *_label variables from your crosswalks to overwrite the unreadable variable names currently in the first column of your outreg file. Then, resort the data and export a new text (or XLSX) file:
                Code:
                replace v1 = foreign_label+" # "+maxspeed_label if foreign_label != "" & maxspeed_label != ""
                
                sort sort_order
                drop foreign_id foreign_label maxspeed_id maxspeed_label sort_order
                export delimited using "$out\outreg_attempt2.txt", delim(tab) replace
                export excel "$out\outreg_attempt2.xlsx", sheetreplace sheet("outreg_attempt2")
                After all this, my outreg file looks a lot like what -esttab- produced in Carlos' post (#2):
                Code:
                v1    v2    v3
                        (1)
                VARIABLES    LABELS    Price
                        
                mpg    Mileage (mpg)    14.58
                        (81.76)
                weight    Weight (lbs.)    3.563***
                        (0.707)
                1.foreign    Car type = 1, Foreign    3,492***
                        (1,250)
                Domestic # Light        0
                        (0)
                Domestic # Ridiculous        -884.2
                        (923.6)
                Domestic # Ludicrous        -1,528*
                        (877.6)
                Domestic # Plaid        -243.7
                        (914.2)
                Foreign # Light        0
                        (0)
                Foreign # Ridiculous        -58.39
                        (1,195)
                Foreign # Ludicrous        -725.9
                        (1,366)
                Foreign # Plaid        -1,732
                        (1,628)
                Constant    Constant    -5,295
                        (3,746)
                        
                Observations        74
                R-squared        0.541
                Standard errors in parentheses        
                *** p<0.01, ** p<0.05, * p<0.1
                Hopefully, someone can point out inefficiencies in the above approach -- otherwise, I hope that helps somewhat!

                (EDIT) Weird that my estimates don't match yours...though I don't think that affects the method above.
                Last edited by Jon Kroah; 11 Dec 2017, 22:32.

                Comment


                • #9
                  Jon,

                  This is WONDERFUL. I will tweak it a bit and turn it into a command or an ado file, or something. To answer your question about why we get different results, it's because I ran that code on Stata version 12. I had no idea that RNGs could produce such different results with the same seed but under different versions of Stata. Thanks again!

                  Comment


                  • #10
                    Glad it helped! This a synthesis of ideas from a couple of my coworkers (not members here) so a big thanks to them.

                    Comment

                    Working...
                    X