Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

predict method for felm from lfe package

Tags:

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 
like image 655
kennyB Avatar asked May 27 '15 19:05

kennyB


2 Answers

UPDATE (2020-04-02): The answer from Grant below using the new package fixest provides a more parsimonious solution.

As a workaround, you could combine felm, getfe, and demeanlist as follows:

library(lfe)  lm.model <- lm(data=demeanlist(iris[, 1:2], list(iris$Species)), Sepal.Length ~ Sepal.Width) fe <- getfe(felm(data = iris, Sepal.Length ~ Sepal.Width | Species)) predict(lm.model, newdata = data.frame(Sepal.Width = 3)) + fe$effect[fe$idx=="virginica"] 

The idea is that you use demeanlist to center the variables, then lm to estimate the coefficient on Sepal.Width using the centered variables, giving you an lm object over which you can run predict. Then run felm+getfe to get the conditional mean for the fixed effect, and add that to the output of predict.

like image 152
pbaylis Avatar answered Sep 28 '22 06:09

pbaylis


Late to the party, but the new fixest package (link) has a predict method. It supports high-dimensional fixed effects (and clustering, etc.) using a very similar syntax to lfe. Somewhat remarkably, it is also considerably faster than lfe for the benchmark cases that I've tested.

library(fixest)  model_feols <- feols(data = iris, Sepal.Length ~ Sepal.Width | Species) predict(model_feols, newdata = data.frame(Sepal.Width = 3, Species = "virginica")) # Works 
like image 23
Grant Avatar answered Sep 28 '22 08:09

Grant