py-econometrics / pyfixest

Fast High-Dimensional Fixed Effects Regression in Python following fixest-syntax
https://py-econometrics.github.io/pyfixest/pyfixest.html
MIT License
117 stars 27 forks source link

Extract covariance matrix from regression object #526

Open greenguy33 opened 4 days ago

greenguy33 commented 4 days ago

I have been using pyfixest for a few weeks now and up until now it has been great - I love being able to do the stats modeling I would do in R alongside the nice syntax and integration that Python offers. Overall, fantastic project and two thumbs up!

I did have one quick question which I haven't been able to find an answer to as of yet. Is there an easy way to extract the covariance matrix from a regression object, ideally into a Pandas dataframe, or is that feature not implemented?

The original fixest package has the method vcov() which returns a K * K matrix where K == number of model variables. https://rdrr.io/cran/fixest/man/vcov.fixest.html

In pyfixest, I see that .vcov("HC1") can be called as an attribute of a regression object, but it seems to work in conjunction with .summary() which updates the standard errors using the provided standard error technique. But I haven't seen how to get the entire K * K matrix out of this.

Consider this a feature request if this doesn't exist yet!

s3alfisc commented 4 days ago

Hi Hayden, thanks for your nice words and awesome that you enjoy using pyfixest! =)

I did have one quick question which I haven't been able to find an answer to as of yet. Is there an easy way to extract the covariance matrix from a regression object, ideally into a Pandas dataframe, or is that feature not implemented?

The estimated variance covariance is stored as an attribute in all classes derived from Feols - you can get it by calling Feols._vcov.

I think it would likely be the .vcov() method would simply return the estimated variance covariance matrix? This should be easy enough to implement =)

s3alfisc commented 4 days ago

I.e. you can access the vcov matrix as

import pyfixest as pf
data = pf.get_data()
fit = pf.feols("Y ~ X1", data = data)
fit._vcov
# array([[ 0.01250398, -0.00749663],
#        [-0.00749663,  0.00718007]])
greenguy33 commented 4 days ago

Awesome! Thank you so much.

greenguy33 commented 3 days ago

@s3alfisc I can take a stab at this quick change if it's helpful. Meaning, having the .vcov() method return the covariance matrix. Let me know and I'll fork the code

s3alfisc commented 3 days ago

Hey @greenguy33 that would be amazing!

greenguy33 commented 3 days ago

@s3alfisc Happy to do it! I took a quick look at the code. Unless I'm missing something, this change is as simple as changing the line return self to return self._vcov on line 481 of file feols_.py

s3alfisc commented 2 days ago

Yes, that would essentially be it!