首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何导出具有稳健标准错误的结果的回归表,或者用软件包lfe导出聚集标准错误?

如何导出具有稳健标准错误的结果的回归表,或者用软件包lfe导出聚集标准错误?
EN

Stack Overflow用户
提问于 2018-05-22 14:54:17
回答 2查看 1.3K关注 0票数 1

使用包lfe,我可以使用命令felm生成具有健壮标准错误或集群标准错误的回归结果。

对于标准回归,我可以使用texreg包使用screenregtexreghtmlreg函数导出回归表。但是,如果我想在lfe包中获得具有健壮标准错误的回归,我需要在summary函数中添加选项robust=T,因此,我想知道在这里提到的情况下,如何使用texreg包导出回归表?有关演示代码,请参阅下面。

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-25 13:38:25

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

代码语言:javascript
复制
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调用:

代码语言:javascript
复制
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))
票数 1
EN

Stack Overflow用户

发布于 2018-05-23 07:29:06

也许您可以考虑在override.se函数中使用screenregoverride.pvalues的变通方法。也就是说,我们首先保存稳健的标准误差和相应的p-值.当打印出表时,我们会忽略默认值.您会发现,表示重要性值的星星将自动更新。

以下是转载的例子。我有意创建了iris2。当您运行回归,显着水平是不同的稳健(p=0.004 -2星)和非稳健标准误差(p=0.015 -1星)。在你超越了标准误差和p值之后,screenreg给出了2颗星.

代码语言:javascript
复制
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中指定了群集

代码语言:javascript
复制
OLS2<-felm(Sepal.Length~Sepal.Width |0|0|Species, data = iris)

默认值将是群集标准错误。也就是说,没有必要过火.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50470734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档