统计模型中的 GLM 返回错误 [英] GLM in statsmodel returning error

查看:34
本文介绍了统计模型中的 GLM 返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经知道如何使用 OLS(Pandas/Statsmodel OLS 预测未来值 ),我正在尝试为我的数据拟合更好的曲线……GLM 应该像我假设的那样工作.

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:对偏差函数的第一次猜测返回了一个 nan.这可能是一个边界问题,应该报告. 这是 2010 年的一个已知问题.我也试过:

returns 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()

引发错误'builtin_function_or_method'对象没有属性'equals'

我想用 ols 模型绘制模型以及未来值:

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()

编辑我放弃尝试使用 GLM,而是使用 OLS 和多项式拟合的公式,我认为它工作得很好......(尽管获得未来的预测显然与我的另一个 OLS,希望有一天我会写一些代码而不用无休止地摆弄!)!不幸的是,我的声誉太低,无法发布这张漂亮的照片!:(

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! :(

推荐答案

我想我遇到了同样的问题,您只需要确保您的数据框不包含 case 和 not case 都为零的行.在估计 glm 之前,运行:

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)]

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

显然 R 会自动完成.

Apparently R does it automatically.

这篇关于统计模型中的 GLM 返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆