简体   繁体   中英

GLM in statsmodel returning error

Now that I have figured out how to use OLS ( Pandas/Statsmodel OLS predicting future values ), I am trying to fit a nicer curve to my data...GLM should work similarly I assumed.

import statsmodels.api as sma
df1['intercept'] = 1
y = df1[['intercept', 'date_delta']]
X = df1['monthly_data']
smaresults_normal = sma.GLM(X,y, family=sma.families.Binomial()).fit()

returns ValueError: The first guess on the deviance function returned a nan. This could be a boundary problem and should be reported. ValueError: The first guess on the deviance function returned a nan. This could be a boundary problem and should be reported. which was a known issue in 2010. I've also tried:

import statsmodels.api as sm
import statsmodels.formula.api as smf

glm_unsmoothed = smf.GLM('monthly_data ~ date_delta', df1, family=sm.families.Binomial() )

glm_unsmoothed.fit()

which raises the error 'builtin_function_or_method' object has no attribute 'equals'

I want to graph the model as well as future values as I was able to do with the ols model:

#ols model
df1['intercept'] = 1
X = df1[['intercept', 'date_delta']]
y = df1['monthly_data']

smresults_normal = sm.OLS(y, X).fit()
#future values
smresults_normal.predict(df_future12[['intercept', 'future_monthly']])

#model in sample data
import statsmodels.formula.api as smf

smresults_unsmoothed = smf.ols('monthly_data ~ date_delta', df1).fit()

df1['ols_preds_unsmoothed'] = smresults_unsmoothed.predict()

edit I abandoned trying to use GLM and instead used OLS with a formula for a polynomial fit which I think worked quite well...(though getting future predictions apparently does not work the same as in my other OLS, someday I will hopefully write some code without endless fiddling!)!unfortunately my reputation is too low to post the nice pic! :(

I think I had the same issue, all you need is to secure that your data frame doesn't contain lines where both cases and not cases are equal to zero. Just before estimating the glm, run:

data=data[(data.cases !=0) | (data.notcases!=0)]

Apparently R does it automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM