Does anyone have a nice clean way to get predict
behavior for felm
models?
library(lfe)
model1 <- lm(data = iris, Sepal.Length ~ Sepal.Width + Species)
predict(model1, newdata = data.frame(Sepal.Width = 3, Species = "virginica"))
# Works
model2 <- felm(data = iris, Sepal.Length ~ Sepal.Width | Species)
predict(model2, newdata = data.frame(Sepal.Width = 3, Species = "virginica"))
# Does not work
As a workaround, you could combine
felm
,getfe
, anddemeanlist
as follows:The idea is that you use
demeanlist
to center the variables, thenlm
to estimate the coefficient onSepal.Width
using the centered variables, giving you anlm
object over which you can runpredict
. Then runfelm
+getfe
to get the conditional mean for the fixed effect, and add that to the output ofpredict
.I think what you're looking for might be the
lme4
package. I was able to get a predict to work using this:You may have to play around a little to specify the particular effects you're looking for, but the package is well-documented so it shouldn't be a problem.
This might not be the answer that you are looking for, but it seems that the author did not add any functionality to the
lfe
package in order to make predictions on external data by using the fittedfelm
model. The primary focus seems to be on the analysis of the group fixed effects. However, it's interesting to note that in the documentation of the package the following is mentioned:Hence, it might be possible to coerce the
felm
object to anlm
object in order to obtain some additionallm
functionality (if all the required info is present in the object to perform the necessary computations).The lfe package is intended to be run on very large datasets and effort was made to conserve memory: As a direct result of this, the
felm
object does not use/contain a qr decomposition, as opposed to thelm
object. Unfortunately, thelm
predict
procedure relies on this information in order to compute the predictions. Hence, coercing thefelm
object and executing the predict method will fail:If you really must use this package to perform the predictions then you could maybe write your own simplified version of this functionality by using the information that you have available in the
felm
object. For example, the OLS regression coëfficients are available viamodel2$coefficients
.This should work for cases where you wish to ignore the group effects in the prediction, are predicting for new X's, and and only want confidence intervals. It first looks for a
clustervcv
attribute, thenrobustvcv
, thenvcv
.