rregressiontexreglfe

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


Using the package lfe, I can generate the regression results with either robust standard error or clustered standard error using command felm.

For the standard regression, I can export the regression table with the texreg package using function screenreg, texreg or htmlreg. However, if I want to get the regression with the robust standard error in lfe package, I need to add the option robust=T in the summary function, therefore, I wonder how I can export the regression table using texreg package in the case I mentioned here? See below for the demo code.

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

Solution

  • You can change the line s <- summary(model) to s <- summary(model, ...) in the extract function in the texreg package:

    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)
    

    Then you should be able to hand over a robust = TRUE argument to screenreg or texreg calls:

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