Reputation: 85
I am trying to get fitted values out of a linear model with many factors, which I would like to estimate using the felm
function from the R package lfe
. Unless I am misinterpreting what is meant by the fitted.values
returned by the function, it looks like these values do not match the output I get when I build them manually. Here is an example adapted from the package documentation:
library(lfe)
set.seed(42)
nn = 10
n1 = 3
x <- rnorm(nn)
f1 <- sample(n1, length(x), replace=TRUE)
y <- 2.13*x + cos(f1) + rnorm(length(x), sd=0.5)
est <- felm(y ~ x | f1)
estb <- lm(y~x+factor(f1)-1)
# we have exactly the same coefficients
getfe(est)['effect']/estb$coefficients[2:(n1+1)]
est$coefficients/estb$coefficients[1]
# but different fitted values -- in fact all having the same group offset
estb$fitted.values-est$fitted.values
What are these offsets? Does felm
intend to return a different kind of fitted value? Thanks for looking
Upvotes: 3
Views: 2300
Reputation: 339
This is no longer an issue in lfe 2.8-5.1
. In the OP's example, estb$fitted.values
will return the correct fitted values, including the fixed effects. The difference between felm()
and lm()
output is only from rounding.
Upvotes: 0
Reputation: 76
It looks like the fitted values spitted out by felm are calculated using only the regressors in the first part of the felm equation (excluding the fixed effects). This explains the same group offset you see in your data.
You can derive fitted values for the whole model by subtracting the residuals in the felm object from your observed values. Unlike the fitted values, the residuals are calculated using the full model (see help for the felm function).
Upvotes: 1