Coercing felm R-object into lm form or some other way of getting it printed in a LaTeX table

StackOverflow https://stackoverflow.com/questions/9759089

  •  24-05-2021
  •  | 
  •  

Question

I am using the lfe package in R to do some regression with lots of fixed effects, so direct lm is out of the question (and since the fixed effects are not individual-level, so is plm). I get the output with no problems but now I'd like to use it in a LaTeX table. However, none of the packages I tried (like xtable, apsrtable, the latex command in Hmisc and so on) have methods for objects of class felm. So my question is, what do I do? Is there a way to access the lm.method and twist it so it can read felm objects? Is there a way to coerce felm objects into lm form? Any ideas?

Was it helpful?

Solution

It is possible that doing some transplant surgery on the felm-object will succeed. Determining whether creation of such a chimera is not doing violence to important underlying assumptions is your responsibility:

# with the first example in the lfe::
est <- lfe::felm(y ~ x+x2+G(id)+G(firm))
class(est) <- c("felm", "lm")
require(xtable)
 xtable(est)
#----------------
% latex table generated in R 2.14.0 by xtable 1.6-0 package
% Sun Mar 18 10:42:04 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
  \hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ 
  \hline
x & 1.0937 & 0.0971 & 11.26 & 0.0000 \\ 
  x2 & 0.4597 & 0.1177 & 3.91 & 0.0002 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

Before doing that class engraftment, I did look at the felm-object to see if it resembled an lm-object and it did. It also appears that summary(est) returns the output that would be expected by a user of lm. (This does not actually do what you asked. The only thing it does is allow lm-targetted functions to attempt to do their work.)

I'm not a particularly successful user of S4 methods but following a couple of links in the help page and making mods this is what I got after ignoring the warning:

 require(stats)
 setOldClass(c("felm", "lm"))
 setMethod("modelInfo", "summary.felm", function(x) {
   env <- sys.parent()
   digits <- evalq(digits, env)
   model.info <- list(
                      "$N$"=formatC(sum(x$df[1:2]),format="d"),
                      "Resid. sd" = formatC(x$sigma,format="f",digits=digits))
   class(model.info) <- "model.info"
   return(model.info)
 } )
#in method for ‘modelInfo’ with signature ‘"summary.felm"’: no definition for class “summary.felm”
#[1] "modelInfo"
 apsrtable(est,est, digits=1, align="l", 
           stars=1, model.counter=0, order="rl",
           coef.rows=1, col.hspace="3em", float="sidewaystable")
#----------------------
\begin{sidewaystable}[!ht]
\caption{}
\label{} 
\begin{tabular}{ l D{.}{.}{1}D{.}{.}{1}@{\hspace{3em}}D{.}{.}{1}D{.}{.}{1} } 
\hline 
  & \multicolumn{ 2 }{ c }{ Model 0 } & \multicolumn{ 2 }{ c }{ Model 1 } \\ \hline
 x      & 1.1 ^* & (0.1)  & 1.1 ^* & (0.1) \\ 
x2     & 0.5 ^* & (0.1)  & 0.5 ^* & (0.1)  \\
 $N$       & \multicolumn{2}{c}{172} & \multicolumn{2}{c}{172}\\ 
Resid. sd & \multicolumn{2}{c}{   } & \multicolumn{2}{c}{   } \\ \hline
 \multicolumn{5}{l}{\footnotesize{Robust standard errors in parentheses}}\\
\multicolumn{5}{l}{\footnotesize{$^*$ indicates significance at $p< 0.05 $}} 
\end{tabular} 
 \end{sidewaystable}

OTHER TIPS

As the author of the lfe-package, I can shed some light on this. It is true that the felm-object mimics the lm-object, but with some differences.

It does not contain a copy of the data-matrix. The lfe-package is intended for very large datasets, in the gigabyte-class. Thus, some effort has been made to conserve memory. Another difference is that the felm-object does not contain information about the rank of the system in the same manner as lm, nor the qr-decomposition used by lm. felm does not use a qr-decomposition, and the rank can be a somewhat complicated matter. The upshot of this is that some standard methods will work on felm-objects, whereas others will not. An early version of lfe actually had 'lm' as an inherited class for 'felm', but since this creates the impression that it actually is an lm-object, I removed it.

A little late to this party, but I recommend using the stargazer package for producing LaTeX output from regression packages. Stargazer works well on felm objects (as well as the others, lm, etc).

You can find the package through CRAN here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top