如何在 R 中的 Stargazer 中填写缺失的列?

How to fill in missing columns in Stargazer in R?

我正在使用 lm 命令和 coeftest 命令。我想把它们拼成一个stargazertable。问题是 stargazer 输出不完整。

这是我目前所拥有的。左侧是 OLS 模型。右边是通过coeftest命令传过来的OLS模型

=================================================
                         Dependent Variable:     
                    -----------------------------
                     VAP Turnout (%)             
                           (1)            (2)    
-------------------------------------------------
X                         -0.010         -0.010  
                         (0.031)        (0.035)  
                                                 
Constant                495.158***     495.158***
                         (17.493)       (20.561) 
                                                 
-------------------------------------------------
Observations              1,000                  
Adjusted R2               -0.001                 
Residual Std. Error 287.218 (df = 998)           
=================================================
Note:                 *p<0.1; **p<0.05; ***p<0.01
  1. 如何将 VAP Turnout (%) 移回观星器 table 的中心?
  2. 如何将ObservationsAdjusted R2Residual Std. Error手动输入到模型2(通过coeftest命令传递的OLS对象)下的空白区域。

感谢您的帮助!

这是重现 table 的代码:

#Packages
library(stargazer)
library(lmtest)
library(sandwich)

#Set Seed
set.seed(1993)

#Create Sample Data
X <- sample(seq(1, 1000), 1000, replace = T)
Y <- sample(seq(1, 1000), 1000, replace = T)
Cluster <- sample(letters, 1000, replace = T)

df <- data.frame(X, Y, Cluster)

#Model 1 - OLS
Base <- lm(Y ~ X, data = df)

#Model 2 - Coeftest
Coeff_Test <- coeftest(Base, vcovCL, cluster = df$Cluster)

#Stargazer table
stargazer(list(Base, Coeff_Test), type = "text",omit=c("Cluster"),
          model.names = FALSE,
          dep.var.caption = c("Dependent Variable:"),
          dep.var.labels   = c("VAP Turnout (%)"), #Write the DV Here
          omit.stat = c("f","rsq"))

使用 broom 包中的 tidy 函数从 coeftest 中提取标准误差和 p 值。从那里,您可以使用 sep 参数将标准误差和 p 值手动输入到 stargazer 函数中。

请注意,当您在 sep 参数中使用 NULL 时,它会手动默认为相应位置列出的任何对象。在这种情况下,我们在第一个位置使用它来默认为基础对象,但在第二个位置手动输入标准误差和 p 值。

最后,由于您只是对标准误差进行聚类,因此系数和模型规格值保持不变。所以我们可以 可以将相应的模型,即对象 base,放入第二个参数中,因此当我们自己覆盖 se 和 p 值时,它会提供系数和模型规范值。

#Packages
library(stargazer)
library(lmtest)
library(sandwich)

#Set Seed
set.seed(1993)

#Create Sample Data
X <- sample(seq(1, 1000), 1000, replace = T)
Y <- sample(seq(1, 1000), 1000, replace = T)
Cluster <- sample(letters, 1000, replace = T)

df <- data.frame(X, Y, Cluster)

#Model 1 - OLS
Base <- lm(Y ~ X, data = df)

#Model 2 - Coeftest
Coeff_Test <- coeftest(Base, vcovCL, cluster = df$Cluster)

#Make into a DF and extract the SE and P-Values
library(broom)
Coeff_Test_DF <- tidy(Coeff_Test)
#Coeff_Test_DF$std.error
#Coeff_Test_DF$p.value

#Stargazer table
stargazer(list(Base, Base), type = "text",omit=c("Cluster"),
          model.names = FALSE,
          se = list(NULL, Coeff_Test_DF$std.error),
          p = list(NULL, Coeff_Test_DF$p.value),
          dep.var.caption = c("Dependent Variable:"),
          dep.var.labels   = c("VAP Turnout (%)"), #Write the DV Here
          omit.stat = c("f","rsq"))

t test of coefficients:

               Estimate  Std. Error t value            Pr(>|t|)    
(Intercept) 495.1580645  20.5610666 24.0823 <0.0000000000000002 ***
X            -0.0098368   0.0346685 -0.2837              0.7767    
---
===========================================================
                                   Dependent Variable:     
                               ----------------------------
                                     VAP Turnout (%)       
                                    (1)            (2)     
-----------------------------------------------------------
X                                  -0.010        -0.010    
                                  (0.031)        (0.035)   
                                                           
Constant                         495.158***    495.158***  
                                  (17.493)      (20.561)   
                                                           
-----------------------------------------------------------
Observations                       1,000          1,000    
Adjusted R2                        -0.001        -0.001    
Residual Std. Error (df = 998)    287.218        287.218   
===========================================================
Note:                           *p<0.1; **p<0.05; ***p<0.01