R tutorial 20 - Logistic Regression 逻辑回归 (3)

来源:互联网 发布:云计算 erp 编辑:程序博客网 时间:2024/04/28 01:11
/*Logistic Regression 逻辑回归薪金与房屋补贴的关系。假设月薪是12150、那预测他会不会同时申请房屋补贴。逻辑回归用来预测0与1、是与否的模型。*/salary <- c(5500, 5800, 6400, 6700, 7100, 7500, 8800, 9500, 11000, 11500, 12000, 12500, 13100, 13800, 13900, 15000)# salary <- rnorm(16, 10000, 8000)claimNum <- c(0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1)cat("\n")cat("===============Result===============","\n")result <- data.frame(claimNum, salary)result# Drawplot(salary, claimNum, main="Salary & claimNum", xlab="Salary", ylab="claimNum", col = "red")cat("\n")cat("===============Model===============","\n")model <- glm(formula= claimNum ~ salary, data=result, family=binomial)modelcat("\n")cat("===============Summary Model===============","\n")summary(model)# Drawcurve(predict(model, data.frame(salary = x), type="resp"), add=TRUE)points(salary, fitted(model), pch=20, col = "blue")cat("\n")cat("===============Predict===============","\n")newdata = data.frame(salary = 12150)predict(model, newdata, type="response")


===============Result===============    claimNum salary1         0   55002         0   58003         0   64004         0   67005         0   71006         1   75007         0   88008         1   95009         0  1100010        1  1150011        0  1200012        0  1250013        1  1310014        1  1380015        1  1390016        1  15000===============Model=============== Call:  glm(formula = claimNum ~ salary, family = binomial, data = result)Coefficients:(Intercept)       salary   -5.0425584    0.0004657  Degrees of Freedom: 15 Total (i.e. Null);  14 ResidualNull Deviance:    21.93 Residual Deviance: 16.05 AIC: 20.05===============Summary Model=============== Call:glm(formula = claimNum ~ salary, family = binomial, data = result)Deviance Residuals:     Min       1Q   Median       3Q      Max  -1.5208  -0.6299  -0.4147   0.6946   1.8668  Coefficients:              Estimate Std. Error z value Pr(>|z|)  (Intercept) -5.0425584  2.4936676  -2.022   0.0432 *salary       0.0004657  0.0002285   2.038   0.0415 *---Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1(Dispersion parameter for binomial family taken to be 1)    Null deviance: 21.930  on 15  degrees of freedomResidual deviance: 16.046  on 14  degrees of freedomAIC: 20.046Number of Fisher Scoring iterations: 4===============Predict===============         1 0.6492079 


0 0
原创粉丝点击