R tutorial 18 - Logistic Regression 逻辑回归 (1)

/*
Logistic Regression 逻辑回归
薪金与房屋补贴的关系。
假设月薪是17150,那预测他会不会同时申请房屋补贴。
逻辑回归用来预测0与1、是与否的模型。
得到的答案会以0~1表示。
Salary = 17150 、预期结果是 = 0.7105441。
*/

salary <- c(5500, 5800, 6400, 6700, 7100, 7500, 8800, 9500, 11000, 11500, 12000, 12500, 13100, 13800, 13900, 15000)
claimNum <- c(0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1)

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

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

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

newdata = data.frame(salary = 17150)

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


===============Result=============== 
   claimNum salary
1         0   5500
2         1   5800
3         1   6400
4         0   6700
5         0   7100
6         0   7500
7         0   8800
8         1   9500
9         0  11000
10        0  11500
11        0  12000
12        1  12500
13        1  13100
14        1  13800
15        0  13900
16        1  15000

===============Model=============== 

Call:  glm(formula = claimNum ~ salary, family = binomial, data = result)

Coefficients:
(Intercept)       salary  
 -1.8998592    0.0001631  

Degrees of Freedom: 15 Total (i.e. Null);  14 Residual
Null Deviance:	    21.93 
Residual Deviance: 20.96 	AIC: 24.96

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

Call:
glm(formula = claimNum ~ salary, family = binomial, data = result)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-1.3371  -1.0240  -0.8249   1.0885   1.5997  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.8998592  1.8070847  -1.051    0.293
salary       0.0001631  0.0001696   0.962    0.336

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 21.930  on 15  degrees of freedom
Residual deviance: 20.958  on 14  degrees of freedom
AIC: 24.958

Number of Fisher Scoring iterations: 4


===============Predict=============== 
        1 
0.7105441 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值