-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
hi!
I would like to refer to @jfhawkin comment and ask if you could look at the convergence of ZINB model because it seems to not be able to converge for zero-inflated count data.
Here is a small example I made:
import pandas as pd
import numpy as np
import statsmodels.api as sm
from scipy.stats import nbinom
### Generate random data
size = 100 # size of the data
n = 10 # number of successes for negative binomial
p = 0.5 # probability of success for negative binomial and Poisson
zero_prob = 0.5 # probability of zero for zero-inflated distributions
zinb_data = nbinom.rvs(n, p, size=size)
zinb_data[np.random.random(size) < zero_prob] = 0
df = pd.DataFrame({
'nb': np.nan,
'zinb': zinb_data,
})
x = df['zinb']
intercept = sm.add_constant(np.ones(len(x)))
mle_zinb = sm.ZeroInflatedNegativeBinomialP(x, intercept).fit(method='bfgs', maxiter=5000, gtol=1e-12)
In my tests, I've noticed that depending on the seed sometimes model is able to converge.
I use this setting of the model because I'm trying to replicate the result from zeroinfl() method in R.
I appreciate your time, please, let me know if the example is not clear, I would be happy to explain it better.
Thank you!
OUTPUT:
gtol=1e-12)= sm.ZeroInflatedNegativeBinomialP(x, intercept).fit(method='bfgs', maxiter=5000
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
/python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:4325: RuntimeWarning: overflow encountered in exp
return np.exp(linpred)
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in multiply
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: divide by zero encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3975: RuntimeWarning: invalid value encountered in multiply
dparams = (a4 * dgterm -
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3972: RuntimeWarning: invalid value encountered in log
dgterm = dgpart + np.log(a1 / a2) + 1 - a3 / a2
python3.10/site-packages/statsmodels/discrete/discrete_model.py:3935: RuntimeWarning: invalid value encountered in log
a1 * np.log(a1) + y * np.log(mu) -
python3.10/site-packages/statsmodels/discrete/count_model.py:167: RuntimeWarning: invalid value encountered in log
llf[zero_idx] = (np.log(w[zero_idx] +
Warning: Desired error not necessarily achieved due to precision loss.
Current function value: nan
Iterations: 16
Function evaluations: 127
Gradient evaluations: 127
python3.10/site-packages/statsmodels/base/model.py:595: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available
warnings.warn('Inverting hessian failed, no bse or cov_params '
python3.10/site-packages/statsmodels/base/model.py:607: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
warnings.warn("Maximum Likelihood optimization failed to "