1

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

对于标准回归,我可以使用texregfunction或. 但是,如果我想在包中获得具有稳健标准误差的回归,我需要在函数中添加选项,因此,我想知道在我这里提到的情况下如何使用包导出回归表?请参阅下面的演示代码。screenregtexreghtmlreglferobust=Tsummarytexreg

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

2 回答 2

1

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

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

默认值将是聚集标准误差。也就是说,不需要超车。

于 2018-05-23T07:29:06.913 回答
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))
于 2018-05-25T13:38:25.220 回答