2

simple question: Can I extract the final degrees of freedom after a feols estimation using the fixest package?

res = feols(Sepal.Length ~ Sepal.Width + Petal.Length | Species, iris)
summary(res)

I know that there are many fancy ways to specify the df in fixest, but I'd just want the result (depending on which option I choose).

For example in the lfe package using felm, I can do so easily like this:

res = felm(Sepal.Length ~ Sepal.Width + Petal.Length | Species | 0 | 0, iris)
res$df

Thanks!

Moritz Schwarz
  • 2,019
  • 2
  • 15
  • 33

3 Answers3

4

What you're looking for is: degrees_freedom(res, "resid", vcov = "iid")


The VCOV has an influence on the values produced by degrees_freedom. Here's an explanation.

By default:

  • the VCOV is clustered wrt the first fixed-effect (if FEs are present)
  • when the VCOV is clustered, FEs that are nested with the cluster are dropped in the DoF calculation (to avoid over-penalization)

The number 3 obtained in degrees_freedom(res, "k") is not the number of fixed-effects. It's the number of regressors, excluding the nested FEs (and adding the intercept).

By changing the way the VCOV is computed, you change the output of degrees_freedom. Here the nestedness is the problem, so you can remove it:

degrees_freedom(res, "resid", vcov = cluster ~ ssc(fixef.K = "full"))
#> [1] 145

But the easiest way is to use an "iid" VCOV.


Agreed the help page only implicitly refers to this by saying that the values depend on the type of VCOV. This could be clearer, I'll try to improve that!

Laurent Bergé
  • 1,292
  • 6
  • 8
  • 2
    The beauty of SO - getting the developer to answer your question :)) Thanks for your work on this excellent package Laurent, much appreciated!! Also thanks for making this clearer in the help page! – Moritz Schwarz Dec 21 '21 at 18:49
3

There is a degrees_freedom

> degrees_freedom(res, type = "k")
[1] 3
> degrees_freedom(res, type = "resid")
[1] 147
> degrees_freedom(res, type = "t")
[1] 2

By changing the type, can get different degrees of freedom i.e ?degrees_freedom shows

type - Character scalar, equal to "k", "resid", "t". If "k", then the number of regressors is returned. If "resid", then it is the "residuals degree of freedom", i.e. the number of observations minus the number of regressors. If "t", it is the degrees of freedom used in the t-test. Note that these values are affected by how the VCOV of x is computed, in particular when the VCOV is clustered.


Or may use

fitstat(res, "g", simplify = TRUE)

Or could be

unname(res$fixef_sizes)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks for the quick reply - but are you sure it really shows the degrees of freedom? The felm df for the same estimation are 145 - while your answer gives 3. Doesn't your solution just show me the fixed effect number? Also, which package is the `degrees_freedom` from? – Moritz Schwarz Dec 19 '21 at 20:39
  • 1
    @MoritzSchwarz it is from the `fixest` package. According to `?degrees_freedom` - `Simple utility to extract the degrees of freedom from a fixest estimation.` – akrun Dec 19 '21 at 20:40
  • @MoritzSchwarz I updated the post. I am not sure if the 145 is based 147 - 2 or so – akrun Dec 19 '21 at 20:44
  • Ha, note to self: always update the packages you are asking a question about... ;) Thanks, now I found the function. Interesting though that the df are different... When using `lm` and `lm_robust`, I also get 145. And for felm, df.residual and df are the same (also 145) – Moritz Schwarz Dec 19 '21 at 20:56
  • @MoritzSchwarz I don't know all the details. But I find that they use different algos in `exactDOF` – akrun Dec 19 '21 at 20:59
  • @MoritzSchwarz Can you check this statement `The standard errors are adjusted for the reduced degrees of freedom coming from the dummies which are implicitly present.`. I am not sure if the 0, 0, will have any effect – akrun Dec 19 '21 at 21:01
2

We may find them after summary in the attributes of the scaled covariance matrix, check str(summary(res)).

attr(summary(res)$cov.scaled, 'dof.K')
# [1] 3
jay.sf
  • 60,139
  • 8
  • 53
  • 110