Search code examples
pythonpandasstatastatsmodels

Show more significant figures of coefficients


I'm running a number of regressions I want to superficially compare with Stata, to ensure my code works as I migrate from Stata to Python.

i.e statsmodels.formula.api('x ~ y') outputs

                coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
Intercept      2.9420      0.287     10.265      0.000       2.380       3.504

Whereas Stata provides

------------------------------------------------------------------------------
         med |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
       _cons |    2.94203   .2866093    10.26   0.000     2.380268    3.503792

I'd essentially like the output to match exactly. I know this can probably done under the hood by saving the coefficients, but it's far easier at a glance.


Solution

  • The (hacky) way I have achieved this is by editing the statsmodels library file iolib\summary.py, and including Stata-like rounding. It's not perfect, but it will do the job for the moment.

    def forg(x, prec=3):
        if prec == 3:
        #for 3 decimals
            if (abs(x) >= 1e4) or (abs(x) < 1e-4):
                return '%9.3g' % x
            else:
                return '%9.3f' % x
        elif prec == 4:
            if (abs(x) >= 1e4) or (abs(x) < 1e-4):
                return '%10.4g' % x
            else:
                return '%10.4f' % x
        elif prec == 10: #stata like coefficient rounding
            return round(x,2)
        elif prec == 11: #stata like t-stat rounding
            return round(x,7)