lrberge / fixest

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

tidy method for fixest_multi objects #440

Closed RoyalTS closed 5 months ago

RoyalTS commented 10 months ago

broom::tidy() works great on regular old fixest result objects but fails on fixest_multi. It would be great if this worked out of the box rather than having to hack it together manually.

lrberge commented 10 months ago

Hi, you can get that kind of information with coeftable. See ?coeftable.fixest_multi.

Arguments wide and long can be of interest.

base = setNames(iris, c("y", "x1", "x2", "x3", "species"))
est = feols(y ~ csw(x1, x2, x3) | species, base)

# default output
coeftable(est)
#>   id          rhs coefficient   Estimate Std. Error   t value    Pr(>|t|)
#> 1  1           x1          x1  0.8035609 0.07139743 11.254759 0.007802281
#> 2  2      x1 + x2          x1  0.4322172 0.16130825  2.679449 0.115623419
#> 3  2      x1 + x2          x2  0.7756295 0.12654554  6.129252 0.025600825
#> 4  3 x1 + x2 + x3          x1  0.4958889 0.12061779  4.111242 0.054382298
#> 5  3 x1 + x2 + x3          x2  0.8292439 0.09701068  8.547965 0.013411238
#> 6  3 x1 + x2 + x3          x3 -0.3151552 0.10961007 -2.875239 0.102669993

# wide = TRUE
# => returns a list giving a wide table for each component
coeftable(est, wide = TRUE)
#> $coef
#>   id          rhs        x1        x2         x3
#> 1  1           x1 0.8035609        NA         NA
#> 2  2      x1 + x2 0.4322172 0.7756295         NA
#> 3  3 x1 + x2 + x3 0.4958889 0.8292439 -0.3151552
#> 
#> $se
#>   id          rhs         x1         x2        x3
#> 1  1           x1 0.07139743         NA        NA
#> 2  2      x1 + x2 0.16130825 0.12654554        NA
#> 3  3 x1 + x2 + x3 0.12061779 0.09701068 0.1096101
#> 
#> $tstat
#>   id          rhs        x1       x2        x3
#> 1  1           x1 11.254759       NA        NA
#> 2  2      x1 + x2  2.679449 6.129252        NA
#> 3  3 x1 + x2 + x3  4.111242 8.547965 -2.875239
#> 
#> $pvalue
#>   id          rhs          x1         x2      x3
#> 1  1           x1 0.007802281         NA      NA
#> 2  2      x1 + x2 0.115623419 0.02560082      NA
#> 3  3 x1 + x2 + x3 0.054382298 0.01341124 0.10267

# long = TRUE
# => returns all the values stacked
coeftable(est, long = TRUE)
#>    id          rhs coefficient  param        value
#> 1   1           x1          x1   coef  0.803560901
#> 2   1           x1          x1     se  0.071397432
#> 3   1           x1          x1  tstat 11.254759089
#> 4   1           x1          x1 pvalue  0.007802281
#> 5   2      x1 + x2          x1   coef  0.432217209
#> 6   2      x1 + x2          x1     se  0.161308248
#> 7   2      x1 + x2          x1  tstat  2.679448904
#> 8   2      x1 + x2          x1 pvalue  0.115623419
#> 9   2      x1 + x2          x2   coef  0.775629458
#> 10  2      x1 + x2          x2     se  0.126545542
#> 11  2      x1 + x2          x2  tstat  6.129251549
#> 12  2      x1 + x2          x2 pvalue  0.025600825
#> 13  3 x1 + x2 + x3          x1   coef  0.495888938
#> 14  3 x1 + x2 + x3          x1     se  0.120617789
#> 15  3 x1 + x2 + x3          x1  tstat  4.111242167
#> 16  3 x1 + x2 + x3          x1 pvalue  0.054382298
#> 17  3 x1 + x2 + x3          x2   coef  0.829243912
#> 18  3 x1 + x2 + x3          x2     se  0.097010683
#> 19  3 x1 + x2 + x3          x2  tstat  8.547964933
#> 20  3 x1 + x2 + x3          x2 pvalue  0.013411238
#> 21  3 x1 + x2 + x3          x3   coef -0.315155173
#> 22  3 x1 + x2 + x3          x3     se  0.109610070
#> 23  3 x1 + x2 + x3          x3  tstat -2.875239220
#> 24  3 x1 + x2 + x3          x3 pvalue  0.102669993

To access just one of the components, use the functions coef/tstat/pvalue. For example:

coef(est, long = TRUE)
#>   id          rhs coefficient   estimate
#> 1  1           x1          x1  0.8035609
#> 4  2      x1 + x2          x1  0.4322172
#> 5  2      x1 + x2          x2  0.7756295
#> 7  3 x1 + x2 + x3          x1  0.4958889
#> 8  3 x1 + x2 + x3          x2  0.8292439
#> 9  3 x1 + x2 + x3          x3 -0.3151552

On broom

I'm not a broom user, it's a different repo. It's a golden opportunity for you to contribute ;-) You may (or may not) use the function above to make it smooth.