Reference category in regression table
Asked Answered
F

2

7

I've got results from a linear regression model with a factor variable in R that I would like pretty up and then output into LaTeX. Ideally the factor variable would be presented in the table via a row that gives the name of the variable and the reference category but is otherwise blank and then rows with indented text below that give the levels of the factor together with the corresponding estimates.

I've long used the stargazer package to get regression results from R into LaTeX but see no way of achieving the result I want with it. An example:

library(ggplot2)
library(stargazer)

levels(diamonds$cut)

options(contrasts = c("contr.treatment", "contr.treatment"))
model1 <- lm(price~cut,data=diamonds)
stargazer(model1,type='text')

This yields the default output:

===============================================
                        Dependent variable:    
                    ---------------------------
                               price           
-----------------------------------------------
cutGood                     -429.893***        
                             (113.849)         

cutVery Good                -376.998***        
                             (105.164)         

cutPremium                   225.500**         
                             (104.395)         

cutIdeal                    -901.216***        
                             (102.412)         

Constant                   4,358.758***        
                             (98.788)          

-----------------------------------------------
Observations                  53,940           
R2                             0.013           
Adjusted R2                    0.013           
Residual Std. Error   3,963.847 (df = 53935)   
F Statistic         175.689*** (df = 4; 53935) 
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01

Here's what I want:

===============================================
                        Dependent variable:    
                    ---------------------------
                               price           
-----------------------------------------------

Cut (Reference: Fair)

   Good                     -429.893***        
                             (113.849)         

   Very Good                -376.998***        
                             (105.164)         

   Premium                   225.500**         
                             (104.395)         

   Ideal                    -901.216***        
                             (102.412)         

Constant                   4,358.758***        
                             (98.788)          

-----------------------------------------------
Observations                  53,940           
R2                             0.013           
Adjusted R2                    0.013           
Residual Std. Error   3,963.847 (df = 53935)   
F Statistic         175.689*** (df = 4; 53935) 
===============================================
Note:               *p<0.1; **p<0.05; ***p<0.01

Is there any way to achieve this in stargazer without too much hackery? Are there other packages in which this would be simpler to do?

Fiorenze answered 2/4, 2014 at 13:23 Comment(0)
C
2

Not entirely what you wanted, but you're able to manually specify covariate labels via the covariate.labels argument. I haven't been able to find out how you could add a header, though, requiring you to manually add the linebreak.

stargazer(model1,type='text',
          covariate.labels=c("Cut (Reference: Fair) Good",
                             ".  Very good",
                             ".  Premium",
                             ".  Ideal"))


======================================================
                               Dependent variable:    
                           ---------------------------
                                      price           
------------------------------------------------------
Cut (Reference: Fair) Good         -429.893***        
                                    (113.849)         

. Very good                        -376.998***        
                                    (105.164)         

. Premium                           225.500**         
                                    (104.395)         

. Ideal                            -901.216***        
                                    (102.412)         

Constant                          4,358.758***        
                                    (98.788)          

------------------------------------------------------
Observations                         53,940           
R2                                    0.013           
Adjusted R2                           0.013           
Residual Std. Error          3,963.847 (df = 53935)   
F Statistic                175.689*** (df = 4; 53935) 
======================================================
Note:                      *p<0.1; **p<0.05; ***p<0.01
Chablis answered 6/4, 2014 at 11:42 Comment(0)
I
2

This gives reasonably close to what was desired as an ASCII output. Whether it succeeds in Latex will require that you test it. The handling of \n may not have the same side-effects there.

stargazer(model1,type='text', column.labels="\nCut (Reference: Fair)",
          covariate.labels=c(".  Good",
                             ".  Very good",
                             ".  Premium",
                             ".  Ideal"))

Console:

=================================================
                          Dependent variable:    
                      ---------------------------
                                 price           
Cut (Reference: Fair) 
-------------------------------------------------
. Good                        -429.893***        
                               (113.849)         

. Very good                   -376.998***        
                               (105.164)         

. Premium                      225.500**         
                               (104.395)         

. Ideal                       -901.216***        
                               (102.412)         

Constant                     4,358.758***        
                               (98.788)          

-------------------------------------------------
Observations                    53,940           
R2                               0.013           
Adjusted R2                      0.013           
Residual Std. Error     3,963.847 (df = 53935)   
F Statistic           175.689*** (df = 4; 53935) 
=================================================
Note:                 *p<0.1; **p<0.05; ***p<0.01
Ironlike answered 19/8, 2014 at 21:21 Comment(3)
Nice! This only works if there's only a single categorical variable though, right?Fiorenze
Right. And it's more of a side-effect rather than what the authors intended, since the "column.labels" are really supposed to be aligned under the dep.var column. I think you really will need to edit the Latex output for multiple covariates.Ironlike
Still, not a bad hack!Fiorenze

© 2022 - 2024 — McMap. All rights reserved.