r stargazer - 将行添加到回归输出并调整它们的顺序

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

我的问题介于 this one and this one 之间。

我想在回归输出中为因子变量的参考类别添加一行。 Stargazer 似乎没有一种简单的方法可以做到这一点。我目前的方法是在我的 Word 文档中添加一行 add.lines,然后手动更改该新行的顺序。这当然很乏味。

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.)"))

这是我现在的位置:

> 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

我想要的输出是这样的:

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


   xc           2.000   


Constant        1.000   


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

自定义添加行顺序的自动方法是什么?或者,如果您愿意,可以问一个更笼统的问题:按正确顺序添加因子变量的参考类别的简单方法是什么?

您可以使用 table.layout 参数自定义您的 table 布局:

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

结果:

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


xc              2.000   


Constant        1.000   



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

注:

"=ldc-ats-n" 决定输出中出现的元素和顺序,每个字符都是一个元素。例如,"t" 代表 "coefficient table" 而 "a" 代表 "additional lines",因此将 "a" 放在 "t" 之前会给您正确的顺序。有关详细信息,请参阅 ?stargazer 并转到 table.layout