[Python] How to convert SE robust (HC2) model results in statsmodels to Latex using Stargazer?

Discussão em 'Python' iniciado por Stack, Setembro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I ran a SE robust regression on my data using in statsmodels

    covariates = ['age', 'male', 'income', 'edu']
    X = sm.add_constant(df[['t1'] + covariates])
    y = df['mean_belief']
    model = sm.OLS(y, X).fit().get_robustcov_results(cov_type='HC2')

    stargazer = Stargazer([model])


    I get this error within the stargazer function: name 'pd' is not defined

    I dug deeper to see why the classic OLS works with stargazer and why the SE robust does not, and the difference is that the two regressions produce different objects. sm.OLS(y, X).fit() produces a statsmodels.regression.linear_model.RegressionResultsWrapper object. sm.OLS(y, X).fit().get_robustcov_results(cov_type='HC2') produces a statsmodels.regression.linear_model.OLSResults object.

    I am assuming stargazer cannot work with OLSResults object. How can I convert the OLSResults object into RegressionResultsWrapper for stargazer to accept?

    Continue reading...

Compartilhe esta Página