NBK
NBK

Reputation: 905

r stargazer - add lines to regression output and costumise their order

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?

Upvotes: 2

Views: 3907

Answers (1)

acylam
acylam

Reputation: 18681

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.

Upvotes: 3

Related Questions