1

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")
patL
  • 2,259
  • 1
  • 17
  • 38
johnsonzhj
  • 517
  • 1
  • 5
  • 23

2 Answers2

2

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))
Philip Leifeld
  • 2,253
  • 15
  • 24
1

Maybe you can consider a workaround way using override.se and override.pvalues in the screenreg function. That is, we first save the robust standard error and the corresponding p-values. When print out the table, we over-ride the default values. You will find that the stars representing the significance value will be automatically updated.

Below is the reproduced example. I intentionally created iris2. When you run the regression, the significance level is different for robust (p=0.004 -- 2 stars) and non-robust standard errors (p=0.015 -- 1 star). After you over-ride the standard errors and p-values, screenreg gives 2 stars.

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

You will find for the first regression OLS1, there are two stars resulted from the robust standard error!

For clustered standard error, if you have specified the clustered in felm as you did

OLS2<-felm(Sepal.Length~Sepal.Width |0|0|Species, data = iris)

The default value will be the clustered standard error. That is, there is no need to over-ride.

Xing Zhang
  • 175
  • 1
  • 5