logistic回归的时候报错问题包括下面两种

Warning: glm.fit: algorithm did not converge

Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred

Warning messages:

1: glm.fit:算法没有聚合

2: glm.fit:拟合機率算出来是数值零或一

做logistic回归的时候这个问题比较常见,下面来举例,为什么会出现这些问题。

首先是glm函数介绍:

glm(formula, family=family.generator, data,control = list(...))

family:每一种响应分布(指数分布族)允许各种关联函数将均值和线性预测器关联起来。

常用的family:

binomal(link='logit')         ----响应变量服从二项分布,连接函数为logit,即logistic回归

binomal(link='probit')       ----响应变量服从二项分布,连接函数为probit

poisson(link='identity')     ----响应变量服从泊松分布,即泊松回归

control:控制算法误差和最大迭代次数

glm.control(epsilon = 1e-8, maxit = 25, trace = FALSE)

-----maxit:算法最大迭代次数,改变最大迭代次数:control=list(maxit=100)

glm函数使用:

library("ggplot2")
data<-iris[1:100,]
samp<-sample(100,80)
names(data)<-c('sl','sw','pl','pw','species')
testdata<-data[samp,]
traindata<-data[-samp,]
lgst<-glm(testdata$species~pl,binomial(link='logit'),data=testdata)
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(lgst)
##
## Call:
## glm(formula = testdata$species ~ pl, family = binomial(link = "logit"),
##     data = testdata)
##
## Deviance Residuals:
##        Min          1Q      Median          3Q         Max
## -2.202e-05  -2.100e-08  -2.100e-08   2.100e-08   3.233e-05
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)   -97.30   87955.20  -0.001    0.999
## pl             39.56   34756.04   0.001    0.999
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 1.1045e+02  on 79  degrees of freedom
## Residual deviance: 2.0152e-09  on 78  degrees of freedom
## AIC: 4
##
## Number of Fisher Scoring iterations: 25

 

注意在使用glm函数就行logistic回归时,出现警告:

Warning messages:
1: glm.fit:算法没有聚合 
2: glm.fit:拟合機率算出来是数值零或一

同时也可以发现两个系数的P值都为0.999,说明回归系数不显著。

第一个警告:算法不收敛。
     由于在进行logistic回归时,依照极大似然估计原则进行迭代求解回归系数,glm函数默认的最大迭代次数 maxit=25,当数据不太好时,经过25次迭代可能算法 还不收敛,所以可以通过增大迭代次数尝试解决算法不收敛的问题。但是当增大迭代次数后算法仍然不收敛,此时数据就是真的不好了,需要对数据进行奇异值检验等进一步的处理。

lgst<-glm(testdata$species~pl,binomial(link='logit'),data=testdata,control=list(maxit=100))
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(lgst)
##
## Call:
## glm(formula = testdata$species ~ pl, family = binomial(link = "logit"),
##     data = testdata, control = list(maxit = 100))
##
## Deviance Residuals:
##        Min          1Q      Median          3Q         Max
## -8.134e-06  -2.110e-08  -2.110e-08   2.110e-08   1.204e-05
##
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)
## (Intercept)   -106.14  237658.98       0        1
## pl              43.16   93735.01       0        1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 1.1070e+02  on 79  degrees of freedom
## Residual deviance: 2.7741e-10  on 78  degrees of freedom
## AIC: 4
##
## Number of Fisher Scoring iterations: 27

如上,通过增加迭代次数,解决了第一个警告,此时算法收敛。

但是第二个警告仍然存在,且回归系数P=1,仍然不显著。

第二个警告:拟合概率算出来的概率为0或1

首先,这个警告是什么意思?
我们先来看看训练样本的logist回归结果,拟合出的每个样本属于'setosa'类的概率为多少?

lgst<-glm(testdata$species~pl,binomial(link='logit'),data=testdata,control=list(maxit=100))
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
p<-predict(lgst,type='response')
qplot(seq(-2,2,length=80),sort(p),col='predict')

可以看出训练样本为'setosa'类的概率不是几乎为0,就是几乎为1,并不是我们预想中的logistic模型的S型曲线,这就是第二个警告的意思。

那么问题来了,为什么会出现这种情况?
 (以下内容只是本人参考一些解释的个人理解)

这种情况的出现可以理解为一种过拟合,由于数据的原因,在回归系数的优化搜索过程中,使得分类的种类属于某一种类(y=1)的线性拟合值趋于大,分类种类为另一   类(y=0)的线性拟合值趋于小。

由于在求解回归系数时,使用的是极大似然估计的原理,即回归系数在搜索过程中使得似然函数极大化:

所以在搜索过程中偏向于使得y=1的h(x)趋向于大,而使得y=0的h(x)趋向于小。

即系数Θ使得 Y=1类的 -ΘTX 趋向于大,使得Y=0类的 -ΘTX 趋向于小。而这样的结果就会导致P(y=1|x;Θ)-->1  ; P(y=0|x;Θ)-->0  .

那么问题又来了,什么样的数据会导致这样的过拟合产生呢?

先来看看上述logistic回归中种类为setosa和versicolor的样本pl值的情况。(横轴代表pl值,为了避免样本pl数据点叠加在一起,增加了一个无关的y值使样本点展开)

testdata$y <- c(1:80)
qplot(pl,y,data =testdata,colour =factor(species))

可以看出两类数据明显的完全线性可分。

故在回归系数搜索过程中只要使得一元线性函数h(x)的斜率的绝对值偏大,就可以实现y=1类的h(x)趋向大,y=0类的h(x)趋向小。

所以当样本数据完全可分时,logistic回归往往会导致过拟合的问题,即出现第二个警告:拟合概率算出来的概率为0或1。

出现了第二个警告后的logistic模型进行预测时往往是不适用的,对于这种线性可分的样本数据,其实直接使用规则判断的方法则简单且适用(如当pl<2.5时则直接判断为setosa类,pl>2.5时判断为versicolor类)。

以下,对于不完全可分的二维训练数据展示logistic回归过程。

data<-iris[51:150,]
samp<-sample(100,80)
names(data)<-c('sl','sw','pl','pw','species')
testdata<-data[samp,]
traindata<-data[-samp,]
lgst<-glm(testdata$species~sw+pw,binomial(link='logit'),data=testdata)
summary(lgst)
##
## Call:
## glm(formula = testdata$species ~ sw + pw, family = binomial(link = "logit"),
##     data = testdata)
##
## Deviance Residuals:
##      Min        1Q    Median        3Q       Max
## -1.68123  -0.12839  -0.01807   0.07783   2.24191
##
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -12.792      5.828  -2.195 0.028168 *
## sw            -4.214      1.970  -2.139 0.032432 *
## pw            15.229      3.984   3.823 0.000132 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
##     Null deviance: 110.854  on 79  degrees of freedom
## Residual deviance:  21.382  on 77  degrees of freedom
## AIC: 27.382
##
## Number of Fisher Scoring iterations: 7

拟合概率曲线图:(基本上符合logistic模型的S型曲线)

p<-predict(lgst,type='response')
qplot(seq(-2,2,length=80),sort(p),col="response")

训练样本散点图及分类边界:

(画logistic回归的分类边界即画曲线h(x)=0.5)

x3<-seq(1.5,4,length=80)
y3<-(4.284/15.656)*x3+13.447/15.656
aaa<-data.frame(x3,y3)p <- ggplot()
p+geom_point(data = testdata,aes(x=sw,y=pw,colour=factor(species)))+
geom_line(data = aaa,aes(x = x3,y = y3,colour="line"))

内容参考于原博主,为加深印象,我自己做了一遍,图换成了ggplot2,原文参考如下连接:

来源: http://www.cnblogs.com/runner-ljt/p/4574275.html

logistic回归报错问题:Warning messages: 1: glm.fit:算法没有聚合 2: glm.fit:拟合機率算出来是数值零或一相关推荐

  1. cmd输入pip报错_安装pip报错:WARNING: Retrying (Retry(total=4,...

    安装pip报错:WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) afte ...

  2. 安装MySQL报错:[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defau

    win10安装mysql5.7.23(免安装版本)过程中 在执行mysqld --initialize命令时 报错: [Warning] TIMESTAMP with implicit DEFAULT ...

  3. vscode git报错 git warning: redirecting to https://xxx.xxx.xxxx/xxx/xxx.git/

    背景:最近从websotrm转到vscode 刚开始两周开发没有问题,后面有两个常用的项目push到远程库或者拉去远程库的更新都会报错: vscode git报错 git warning: redir ...

  4. antd From 表单报错:warning.js:6 Warning: [antd: Switch] value is not validate prop, do you mean checke?

    同样的是适用于Checkbox组件 在使用 antd 时,form 表单中使用 switch 报错: warning.js:6 Warning: [antd: Switch] value is not ...

  5. ansa导出NASTRAN的nas文件报错:warning: MATERIAL(s) were not output(undefined)

    我在练习官方文档算例nastran fatigeu时,ansa导出NASTRAN的nas文件报错:warning: MATERIAL(s) were not output(undefined),计算的 ...

  6. R报错:WARNING: Rtools is required to build R packages, but is not currently installed.

    今天在安装IRkernel时使用如下命令: devtools::install_github('IRkernel/IRkernel') 出现报错: WARNING: Rtools is require ...

  7. 【解决方案】报错:WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))

    报错:WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) 原因是位于国内的的 ...

  8. 已解决(pip报错)WARNING: The repository located at mirrors .aliyun.com is not a trusted or secure host and

    成功解决:WARNING: The repository located at mirrors .aliyun.com is not a trusted or secure host and is b ...

  9. ssh或scp报错:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    报错信息 在工作中,如果使用ssh.scp命令做远程操作报错"WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!" [root@ sa ...

最新文章

  1. IIS配置跨服务器迁移
  2. FastDFS服务重启
  3. Automatic Diagnostic Repository Command-Interpreter
  4. 常用的汇编系统功能指令(包含字符功能类,中断终止类)
  5. SAP Hybris Commerce,CRM和C4C的登录语言选择
  6. CSS 属性 - 伪类和伪元素的区别
  7. 中查出所有姓张的学生为啥查不出来_只有笔试成绩没有面试成绩是什么原因 教师资格面试成绩怎么查...
  8. 为什么我的mysql比redis快_为什么redis是单线程的以及为什么这么快?
  9. 腾讯为60亿美元债券定价 为1年来亚洲最大美元债发行交易
  10. 2017年苹果企业开发者账号申请完整指南
  11. 关于运行微信小程序报错 [微信小程序开发者工具] Error: read EBADF
  12. 如何把img格式转换成vmdk格式
  13. 经典sql语句 行专列 统计部门男女人数 统计员工入职时常 根据出生日期计算年龄
  14. 几种基本放大电路详解
  15. bootstrap进度条媒体对象和Well组件
  16. MusicTools下载 v3.4.0 全网免费无损音乐下载器
  17. 人工智能与神经生理学:差异为何重要
  18. python程序的控制结构思维导图_python学习之路2(程序的控制结构)
  19. U盘装系统(Linux)
  20. 骨骼控制(一)——动画动态节点(AnimDynamics)

热门文章

  1. 大学要考的证书英语计算机,大学里必考的证书盘点 英语四六级、计算机证书上榜...
  2. python怎样画动态文字_Python之pygame学习绘制文字制作滚动文字
  3. 加冕爱情的八枚梦想戒指
  4. dialog出现Uncaught RangeError: Maximum call stack size exceeded错误
  5. 备份数据 宝塔linux_宝塔面板教程大全--宝塔linux面板数据备份教程
  6. ORACLE从一列包含中文以及数字的数据中筛选出中文/或者筛选出包含英文的列
  7. 龙门阵179期实录:技术专场之Android安全现状
  8. 64位win7系统下安装USB下载器驱动FriendlyArm-usb-dnw-driver-的解决方案
  9. 《朗读者》的读后感优秀范文4000字
  10. ubuntu系统学习4——安装engin和opencv3