【问题标题】:How to export the regression table for the results with robust standard error or clustered standard error with package lfe?如何使用包 lfe 导出具有稳健标准误差或聚集标准误差的结果的回归表?
【发布时间】:2018-05-22 14:54:17
【问题描述】:

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

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

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

【问题讨论】:

    标签: r regression texreg lfe


    【解决方案1】:

    您可以在texreg 包的提取函数中将s &lt;- summary(model) 行更改为s &lt;- 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))
    

    【讨论】:

      【解决方案2】:

      也许您可以考虑在screenreg 函数中使用override.seoverride.pvalues 的解决方法。也就是说,我们首先保存稳健标准误差和相应的 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)
      

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

      【讨论】:

        猜你喜欢
        • 2013-05-06
        • 2018-11-15
        • 2011-05-24
        • 2015-01-31
        • 2016-12-16
        • 2017-10-17
        • 2012-03-04
        • 2019-09-14
        • 1970-01-01
        相关资源
        最近更新 更多