lucashusted / pystout

A Package To Make Publication Quality Latex Tables From Python Regression Output
GNU General Public License v3.0
42 stars 12 forks source link

Problem using results.get_robustcov_results() #4

Closed mwood7 closed 3 years ago

mwood7 commented 3 years ago

I love the tables this produces, but have run into a problem today. I'm needing to use the method '.get_robustcov_results()' and it evidently returns 'OLSResults object' whereas OLS returns 'RegressionResultsWrapper' object.

In the Wrapper object, 'params' is a Series object, whereas in the OLSResults object, params is a NumPy array. Pystout crashes on line 324 (my version may be old)

paramlist += m.params.index.to_list()

AttributeError: 'numpy.ndarray' object has no attribute 'index'

So this isn't a problem with pystout at all, and if you want to just close it for that reason, I understand. If you know how to put a OLSResults object into a RegressionResultsWrapper, I'd be grateful (my limited searching hasn't turned up an answer). Thank you again for pystout - it's very useful.

Full example if you'd like to try it out:

import pandas as pd
import statsmodels.formula.api as smf
from pystout import pystout

repo = 'https://raw.githubusercontent.com/astromattwood/SMtests/main/'
food = pd.read_csv(repo+'food.csv').sort_values(by=['income'])

mod1 = smf.ols('food_exp ~ income',data=food).fit()
mod2 = mod1.get_robustcov_results(cov_type='HC1')

#print(mod1.summary2())
#print(mod2.summary2())

pystout(models=[mod1,mod2],
        file='Table.tex',
        digits=4,
        stars={.05:'*',0.01:"**",0.001:'***'},
        addnotes=['Standard errors in parentheses',
                  '* $p<0.05$, ** $p<0.01$, *** $p<0.001$'])
mwood7 commented 3 years ago

Hey I found a workaround, so you can close and are welcome to include it in your documentation if that would be useful.

You can put the OLSResults object into the Wrapper object via (first 2 lines included for completeness - last 2 lines are the workaround):

mod1 = smf.ols('food_exp ~ income',data=food).fit() 
mod2 = mod1.get_robustcov_results(cov_type='HC1')

import statsmodels.regression.linear_model  as lm
mod2a = lm.RegressionResultsWrapper(mod2)

This then works with 'pystout(models=[mod1,mod2a], ...'

Cheers, Matt

lucashusted commented 3 years ago

Hey Matt,

Thanks for this! I am glad the solution worked. For full disclosure, I haven't had the time to update this in some while. I hope to expand it at some point in the near future though and make it more flexible. I will add this to the documentation next time I roll out an update.

Best, Lucas