/*
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
...
留言列表