R tutorial 19 - handle error in Logistic Regression 逻辑回归 (2)

glm会显示数据有以下问题

Warning messages:
1: glm.fit: algorithm did not converge
2: glm.fit: fitted probabilities numerically 0 or 1 occurred


这是因为使用glm要计算数据的方差、假如数据是平滑的直线、经过迭代後、仍然无法分辨数据、就会有这个错误。


userName <- c("Lam Wei Wei", "Zheng Da Shi", "Lin Da You", "Fei Gu Man", "Chen Kuang", "Wong wei yun")
salary <- c(8500, 9800, 12500, 15000, 8700, 7500)
jobPosition <- c("Staff", "Manger", "BOSS", "CEO", "Staff", "Staff")
bonus <- c(2300, 1350, 3285, 1035, 3285, 1035)
welfare <- c(600, 750, 1100, 1600, 680, 550)
claim <- c(800, 950, 1400, 1900, 780, 850)
claimNum <- c(0, 1, 1, 1, 0, 0)

cat("\n")
cat("===============Result===============","\n")
result <- data.frame(claimNum, salary, bonus, welfare, claim)
result

cat("\n")
cat("===============Model===============","\n")
model <- glm(formula= claimNum ~ salary+bonus+welfare+claim, data=result, family=binomial)
model

cat("\n")
cat("===============Summary Model===============","\n")
summary(model)

newdata = data.frame(salary = 12150, bonus = 1200, welfare = 1155, claim = 900)

cat("\n")
cat("===============Predict===============","\n")
predict(model, newdata, type="response")



===============Result=============== 
  claimNum salary bonus welfare claim
1        0   8500  2300     600   800
2        1   9800  1350     750   950
3        1  12500  3285    1100  1400
4        1  15000  1035    1600  1900
5        0   8700  3285     680   780
6        0   7500  1035     550   850

===============Model=============== 
glm.fit: fitted probabilities numerically 0 or 1 occurred

Call:  glm(formula = claimNum ~ salary + bonus + welfare + claim, family = binomial, data = result)

Coefficients:
(Intercept)       salary        bonus      welfare        claim  
 -325.63803      0.07100     -0.02325     -0.58741      0.13180  

Degrees of Freedom: 5 Total (i.e. Null);  1 Residual
Null Deviance:	    8.318 
Residual Deviance: 5.348e-10 	AIC: 10

===============Summary Model=============== 

Call:
glm(formula = claimNum ~ salary + bonus + welfare + claim, family = binomial, data = result)

Deviance Residuals: 
         1           2           3           4           5           6  
-1.724e-05   1.166e-05   9.420e-06   3.466e-06  -2.110e-08  -1.033e-06  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.256e+02  8.347e+05       0        1
salary       7.100e-02  2.067e+02       0        1
bonus       -2.325e-02  1.555e+02       0        1
welfare     -5.874e-01  6.426e+03       0        1
claim        1.318e-01  5.741e+03       0        1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 8.3178e+00  on 5  degrees of freedom
Residual deviance: 5.3483e-10  on 1  degrees of freedom
AIC: 10

Number of Fisher Scoring iterations: 23


===============Predict=============== 
           1 
2.220446e-16 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值