lrberge / fixest

Fixed-effects estimations
https://lrberge.github.io/fixest/
377 stars 59 forks source link

Feature request/bug: predict/fitted for multiple lhs regressions #372

Open MatthieuStigler opened 1 year ago

MatthieuStigler commented 1 year ago

Would it be possible to have a predict/fitted methods for the output of a feols call with multiple left-hand side variable, of class fixest_multi, such as feols(c(y1, y2) ~ x1 + x2, base)? For now, there are two issues:

Admittedly, naming the columns of the predict/fitted when there are multiple X combos would be a bit tricky (and even more complicated to define which x-combination to use with newdata in predict), but maybe at least the case with multiple-Y single X-combo could be covered?

Thanks!

library(fixest)
library(lfe)
#> Loading required package: Matrix

base = iris
names(base) = c("y1", "y2", "x1", "x2", "species")

res_feols = feols(c(y1, y2) ~ x1 + x2, base)
res_mlm = lm(cbind(y1, y2) ~ x1 + x2, base)
res_lfe = felm(y1+ y2 ~ x1 + x2, base)

fitted(res_mlm)|>head()
#>         y1       y2
#> 1 4.885160 3.299865
#> 2 4.885160 3.299865
#> 3 4.830983 3.325579
#> 4 4.939338 3.274151
#> 5 4.885160 3.299865
#> 6 4.983783 3.295532
predict(res_mlm)|>head()
#>         y1       y2
#> 1 4.885160 3.299865
#> 2 4.885160 3.299865
#> 3 4.830983 3.325579
#> 4 4.939338 3.274151
#> 5 4.885160 3.299865
#> 6 4.983783 3.295532

fitted(res_feols)|>head()
#> NULL
predict(res_feols)|>head()
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': no applicable method for 'predict' applied to an object of class "fixest_multi"

fitted(res_lfe)|>head()
#>            y1       y2
#> [1,] 4.885160 3.299865
#> [2,] 4.885160 3.299865
#> [3,] 4.830983 3.325579
#> [4,] 4.939338 3.274151
#> [5,] 4.885160 3.299865
#> [6,] 4.983783 3.295532
predict(res_lfe)|>head()
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'head': no applicable method for 'predict' applied to an object of class "felm"

Created on 2022-11-16 with reprex v2.0.2