如何使用包lfe导出具有强大标准错误或聚簇标准错误的结果的回归表?

时间:2018-05-22 14:54:18

标签: r regression texreg lfe

使用包lfe,我可以使用命令felm生成具有强大标准错误或聚簇标准错误的回归结果。

对于标准回归,我可以使用函数texregscreenregtexreg使用htmlreg包导出回归表。但是,如果我想在lfe包中使用强大的标准错误进行回归,我需要在robust=T函数中添加选项summary,因此,我想知道如何导出我在这里提到的案例中使用texreg包的回归表?请参阅下面的演示代码。

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

2 个答案:

答案 0 :(得分:1)

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

以下是复制的例子。我有意创建了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)

默认值为群集标准错误。也就是说,没有必要超越。

答案 1 :(得分:1)

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

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