要创建简单逻辑模型,我们需要使用family =二项式的glm函数,因为简单逻辑模型或二项式逻辑模型中的因变量有两个类别,如果有两个以上类别,则该模型称为多项式逻辑模型。如果要从简单的逻辑模型中提取斜率和截距的优势比,则需要对模型对象使用exp函数,如以下示例所示。
set.seed(999) x1<-rpois(1000,10) y1<-sample(0:1,1000,replace=TRUE) LogisticModel_1<-glm(y1~x1,family=binomial) summary(LogisticModel_1)
输出结果
Call: glm(formula = y1 ~ x1, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.177 -1.122 -1.088 1.234 1.319 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.03144 0.21467 0.146 0.884 x1 -0.01630 0.02044 -0.797 0.425 (Dispersion parameter for binomial family taken to be 1) Null deviance: 1381.9 on 999 degrees of freedom Residual deviance: 1381.3 on 998 degrees of freedom AIC: 1385.3 Number of Fisher Scoring iterations: 3
x2<-rpois(100000,15) y2<-sample(c(TRUE,FALSE),100000,replace=TRUE) LogisticModel_2<-glm(y2~x2,family=binomial) summary(LogisticModel_2)
输出结果
Call: glm(formula = y2 ~ x2, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.181 -1.180 1.174 1.175 1.177 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.0084037 0.0252237 0.333 0.739 x2 -0.0002083 0.0016286 -0.128 0.898 (Dispersion parameter for binomial family taken to be 1) Null deviance: 138629 on 99999 degrees of freedom Residual deviance: 138629 on 99998 degrees of freedom AIC: 138633 Number of Fisher Scoring iterations: 3
x3<-sample(0:9,5000,replace=TRUE) y3<-sample(0:1,5000,replace=TRUE) LogisticModel_3<-glm(y3~x3,family=binomial) summary(LogisticModel_3)
输出结果
Call: glm(formula = y3 ~ x3, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.171 -1.168 -1.166 1.186 1.189 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -0.026424 0.052975 -0.499 0.618 x3 0.001242 0.009895 0.126 0.900 (Dispersion parameter for binomial family taken to be 1) Null deviance: 6930.9 on 4999 degrees of freedom Residual deviance: 6930.9 on 4998 degrees of freedom AIC: 6934.9 Number of Fisher Scoring iterations: 3
x4<-sample(1:100,5000,replace=TRUE) y4<-sample(c(TRUE,FALSE),5000,replace=TRUE) LogisticModel_4<-glm(y4~x4,family=binomial) summary(LogisticModel_4)
输出结果
Call: glm(formula = y4 ~ x4, family = binomial) Deviance Residuals: Min 1Q Median 3Q Max -1.183 -1.169 -1.155 1.185 1.200 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) -0.0530051 0.0567387 -0.934 0.350 x4 0.0006682 0.0009722 0.687 0.492 (Dispersion parameter for binomial family taken to be 1) Null deviance: 6931.0 on 4999 degrees of freedom Residual deviance: 6930.5 on 4998 degrees of freedom AIC: 6934.5 Number of Fisher Scoring iterations: 3