2

My question falls somewhere between this one and this one.

I want to add a line in the regression output for the reference category of a factor variable. Stargazer doesn't seem to have an easy way of doing this. My current approach is to add a line with add.lines and then manually change the order of that new line, in my Word document. This is of course tedious.

x <- as.factor(c("a","b","c"))
x1 <- c(1,2,3)

# Estimate a model
m1 <- lm(x1~x)

#Create output
stargazer(m1, type = "text", style="ajs", add.lines=c("a (ref.)"))

This is where I stand now:

> stargazer(m1, type = "text", style="ajs", add.lines=c("a (ref.)"))


========================
                 X1     
------------------------
xb              1.000   


xc              2.000   


Constant        1.000   


a (ref.)                
Observations      3     
R2              1.000   
------------------------
Notes:       *P < .05   
             **P < .01  
             ***P < .001

My desired output is this:

========================
                 X1     
------------------------
a (ref.)                
   xb           1.000   


   xc           2.000   


Constant        1.000   


Observations      3     
R2              1.000   
------------------------
Notes:       *P < .05   
             **P < .01  
             ***P < .001

What's an automatic way of customising the order of an added line? Or, if you prefer, a more general question: What's an easy way of adding the reference category of factor variables in its correct order?

NBK
  • 887
  • 9
  • 20
  • FYI, in this case the `Constant` **is** the coefficient for the reference category, so ordering it like you have might be confusing to say the least. – acylam Aug 09 '18 at 16:48
  • Good point. In my actual model I have many other variables, so the constant is not capturing only the reference value. Thanks for the response! – NBK Aug 09 '18 at 16:58

1 Answers1

3

You can use the table.layout argument to customize your table layout:

library(stargazer)
stargazer(m1, type = "text", style="ajs", add.lines=c("a (ref.)"), table.layout = "=ldc-ats-n")

Result:

========================
                 X1     
------------------------
a (ref.)                
xb              1.000   


xc              2.000   


Constant        1.000   



Observations      3     
R2              1.000   
------------------------
Notes:       *P < .05   
             **P < .01  
             ***P < .001

Note:

"=ldc-ats-n" determines what elements and in what order they appear in the output, each character is an element. For instance, "t" represents "coefficient table" while "a" represents "additional lines", so placing "a" before "t" gives you the correct order. See ?stargazer and go to table.layout for more details.

acylam
  • 18,231
  • 5
  • 36
  • 45