如何在R中提取簡單邏輯迴歸模型的截距和斜率係數的優勢比?


要建立簡單的邏輯迴歸模型,我們需要使用`glm`函式,並設定`family = binomial`,因為簡單邏輯迴歸模型或二項邏輯迴歸模型中的因變數只有兩類,如果超過兩類,則該模型稱為多項邏輯迴歸模型。如果要提取簡單邏輯迴歸模型中斜率和截距的優勢比,則需要使用`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

更新於: 2020年10月16日

256 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告