How to Turn Statsmodels Warning into Exception

3.2k views Asked by At

Python and Panda newbie here. I'm trying to use statsmodels to fit a logistic regression to calculate the probability that a voter casts a ballot. I'm working at the precinct level; so sometimes the function doesn't converge, and I get the following error: Warning: Maximum number of iterations has been exceeded.

I have already increased the maximum number of iterations to 1000. I then tried to turn that "Warning" into an exception. I have imported warnings and included warnings.simplefilter('error', Warning) to try to capture it, but it doesn't seem to be a true Python warning. Rather, it's something that statsmodels prints out when it hits the maximum number of iterations.

So now I'm wondering if there's a way to say:

if sm.Logit(y, covs).fit(maxiter=1000) doesn't converge:
    do something else
1

There are 1 answers

0
jseabold On BEST ANSWER

Edit: You can also check the converged flag in the returned results class and raise this exception yourself it the model did not converge. For example,

dta = sm.datasets.spector.load_pandas()

y = dta.endog
X = dta.exog
X['const'] = 1

mod = sm.Logit(y, X).fit()

if not mod.mle_retvals['converged']:
    do something else

Indeed, these warnings are printed. That's bad form. I filed an issue. PRs welcome on this.

https://github.com/statsmodels/statsmodels/issues/1281

Alternatively, try using another solver via the method keyword. Hopefully they'll raise a proper warning or an exception on the way.

If you can share the data that leads to this on that issue, then that would be helpful. There might be something else going on.