如何使用包 lfe 为具有稳健标准误差或聚类标准误差的结果导出回归 table?

How to export the regression table for the results with robust standard error or clustered standard error with package lfe?

使用包 lfe,我可以使用命令 felm 生成具有稳健标准误差或聚类标准误差的回归结果。

对于标准回归,我可以使用函数 screenregtexreghtmlreg 通过 texreg 包导出回归 table。但是,如果我想在 lfe 包中获得具有稳健标准错误的回归,我需要在 summary 函数中添加选项 robust=T,因此,我想知道如何导出在我在这里提到的案例中使用 texreg 包的回归 table?演示代码见下方。

library(lfe);library(texreg)
OLS1<-felm(Sepal.Length~Sepal.Width |0|0|0, data = iris)
summary(OLS1, robust=TRUE)
summary(OLS1)

OLS2<-felm(Sepal.Length~Sepal.Width |0|0|Species, data = iris)
summary(OLS2)

screenreg(list(OLS1,OLS2),caption = "Linear regression")

也许您可以考虑在 screenreg 函数中使用 override.seoverride.pvalues 的解决方法。也就是说,我们首先保存稳健的标准误差和相应的 p 值。当打印出 table 时,我们覆盖了默认值。你会发现代表重要性值的星星会自动更新。

下面是复制的例子。我有意创造了 iris2。当您 运行 回归时,稳健(p=0.004 -- 2 星)和非稳健标准误差(p=0.015 -- 1 星)的显着性水平不同。覆盖标准误差和 p 值后,screenreg 给出 2 星。

library(lfe);library(texreg)
# Create the data iris2 which would have difference significance levels
# for robust and non-robust standard errors
iris2 = rbind(iris[1:100,], iris)
OLS1<-felm(Sepal.Length~Sepal.Width|0|0|0, data = iris2)

# you will see the difference in significance level below
summary(OLS1)
summary(OLS1, robust=TRUE)

###############################################
# Save the robust standard errors and p-values
###############################################
RSE1 = coef(summary(OLS1, robust=TRUE))[,"Robust s.e"]
RpVlaue1 = coef(summary(OLS1, robust=TRUE))[,"Pr(>|t|)"]

# the second regression
OLS2<-felm(Sepal.Length~Sepal.Width|0|0|0, data = iris)

RSE2 = coef(summary(OLS2, robust=TRUE))[,"Robust s.e"]
RpVlaue2 = coef(summary(OLS2, robust=TRUE))[,"Pr(>|t|)"]

screenreg(list(OLS1, OLS2), override.se = list(RSE1, RSE2),
          override.pvalues = list(RpVlaue1, RpVlaue2),
          caption = "Linear regression")

你会发现对于第一个回归 OLS1,有两颗星来自稳健的标准误差!

对于聚类标准错误,如果您像您一样在 felm 中指定了聚类

OLS2<-felm(Sepal.Length~Sepal.Width |0|0|Species, data = iris)

默认值将是聚类标准错误。也就是不需要override

您可以在 texreg 包的提取函数中将行 s <- summary(model) 更改为 s <- summary(model, ...)

library("texreg")

extract.felm <- function(model, include.nobs = TRUE, include.rsquared = TRUE, 
                         include.adjrs = TRUE, include.fstatistic = FALSE, ...) {

  s <- summary(model, ...)
  nam <- rownames(s$coefficients)
  co <- s$coefficients[, 1]
  se <- s$coefficients[, 2]
  pval <- s$coefficients[, 4]

  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  if (include.nobs == TRUE) {
    gof <- c(gof, s$N)
    gof.names <- c(gof.names, "Num.\ obs.")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  if (include.rsquared == TRUE) {
    gof <- c(gof, s$r2, s$P.r.squared)
    gof.names <- c(gof.names, "R$^2$ (full model)", "R$^2$ (proj model)")
    gof.decimal <- c(gof.decimal, TRUE, TRUE)
  }
  if (include.adjrs == TRUE) {
    gof <- c(gof, s$r2adj, s$P.adj.r.squared)
    gof.names <- c(gof.names, "Adj.\ R$^2$ (full model)", 
                   "Adj.\ R$^2$ (proj model)")
    gof.decimal <- c(gof.decimal, TRUE, TRUE)
  }
  if (include.fstatistic == TRUE) {
    gof <- c(gof, s$F.fstat[1], s$F.fstat[4], 
             s$P.fstat[length(s$P.fstat) - 1], s$P.fstat[1])
    gof.names <- c(gof.names, "F statistic (full model)", 
                   "F (full model): p-value", "F statistic (proj model)", 
                   "F (proj model): p-value")
    gof.decimal <- c(gof.decimal, TRUE, TRUE, TRUE, TRUE)
  }

  tr <- createTexreg(
    coef.names = nam, 
    coef = co, 
    se = se, 
    pvalues = pval, 
    gof.names = gof.names, 
    gof = gof, 
    gof.decimal = gof.decimal
  )
  return(tr)
}

setMethod("extract", signature = className("felm", "lfe"), 
          definition = extract.felm)

那么你应该能够将 robust = TRUE 参数传递给 screenregtexreg 调用:

library("lfe")
OLS1 <- felm(Sepal.Length ~ Sepal.Width |0|0|0, data = iris
OLS2 <- felm(Sepal.Length ~ Sepal.Width |0|0|Species, data = iris)

# regular standard errors
screenreg(list(OLS1, OLS2), caption = "Linear regression")

# robust standard errors
screenreg(list(OLS1, OLS2), caption = "Linear regression", robust = TRUE)

# mixing regular and robust standard errors
tr1 <- extract(OLS1)
tr2 <- extract(OLS1, robust = TRUE)
screenreg(list(tr1, tr2))