广义线性模型
1 广义线性模型
1.1 Logisitic回归:妊娠糖尿病分析
因变量:糖尿病(Diabetes){阳性:pos,阴性:neg},设阳性为1、阴性为0
自变量:年龄(Age)、体重指数(BMI,kg/m2 )、血糖浓度(Glucose )、舒张压(Diastolic blood pressure,(mm)Hg )、怀孕次数(Number of times pregnant )
数据文件:diabetes.csv,共 724个观察值
1.1.1 划分训练集和测试集
- 前450条个案为训练集,用于估计Logist模型
- 后274条个案为测试集,用于评价模型的估计效果
- 训练集糖尿病率36.44%,测试集糖尿病率为31.02%,两者大致相等。
1.1.2 训练集估计回归方程
term | estimate | std.error | statistic | p.value |
---|---|---|---|---|
(Intercept) | -7.950 | 0.97 | -8.21 | 0.00 |
Age | 0.012 | 0.01 | 1.00 | 0.32 |
BMI | 0.089 | 0.02 | 4.81 | 0.00 |
Glucose | 0.032 | 0.00 | 7.39 | 0.00 |
Pressure | -0.005 | 0.01 | -0.50 | 0.61 |
Pregnant | 0.098 | 0.04 | 2.45 | 0.01 |
\[ log(\frac{p}{1-p}) =-7.95+0.012\times Age+0.089\times BMI+0.032\times Glucose-0.005\times Pressure+0.098\times Pregnant\]
1.1.3 测试集预测效果评价
pos_pred | neg_pred | |
---|---|---|
pos | 53 | 32 |
neg | 21 | 168 |
回归方程的AIC值为461.36,由训练集预测混淆矩阵可知:
- 准确率(accuracy):80.66%
- 精确率(precision):62.35%
- 召回率(recall):71.62%
- \(F_1\)得分(\(F_1\) score):66.67%
1.1.4 回归模型边际效应
Age BMI Glucose Pressure Pregnant
0.26 1.94 0.70 -0.11 2.14
年龄每增加一岁患病风险提高0.26%;体重指数每增加1患病风险提高1.94%;血糖浓度每增加1患病风险提高0.7%;舒张压每增加1患病风险降低0.11%;怀孕次数每增加一次患病风险提高2.14%。
体重指数、血糖浓度、怀孕次数对患病风险呈正向影响,符合预期。
年龄和舒张压对患病风险影响小且不显著,考虑逐步回归选择更合适的模型
1.2 Logisitic回归逐步回归
1.2.1 逐步回归的回归方程
=step(glm.fits) step.glm
Start: AIC=461.36
Diabetes ~ Age + BMI + Glucose + Pressure + Pregnant
Df Deviance AIC
- Pressure 1 449.62 459.62
- Age 1 450.35 460.35
<none> 449.36 461.36
- Pregnant 1 455.53 465.53
- BMI 1 474.67 484.67
- Glucose 1 515.90 525.90
Step: AIC=459.62
Diabetes ~ Age + BMI + Glucose + Pregnant
Df Deviance AIC
- Age 1 450.43 458.43
<none> 449.62 459.62
- Pregnant 1 455.69 463.69
- BMI 1 475.95 483.95
- Glucose 1 515.92 523.92
Step: AIC=458.43
Diabetes ~ BMI + Glucose + Pregnant
Df Deviance AIC
<none> 450.43 458.43
- Pregnant 1 461.76 467.76
- BMI 1 476.19 482.19
- Glucose 1 522.88 528.88
summary(step.glm)
Call:
glm(formula = Diabetes ~ BMI + Glucose + Pregnant, family = binomial,
data = diabetes, subset = id.train)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -7.939663 0.819306 -9.691 < 2e-16 ***
BMI 0.085129 0.017658 4.821 1.43e-06 ***
Glucose 0.032675 0.004262 7.666 1.77e-14 ***
Pregnant 0.115033 0.034649 3.320 9e-04 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 590.34 on 449 degrees of freedom
Residual deviance: 450.43 on 446 degrees of freedom
AIC: 458.43
Number of Fisher Scoring iterations: 4
1.2.2 逐步回归的预测效果
=predict(step.glm,data.test,type="response")
glm.probs<- rep("neg", 274)
y.pred >.5]<-"pos"
y.pred[glm.probs<- factor(y.pred,levels=c("pos","neg"))
y.pred <- factor(data.test$Diabetes,levels=c("pos","neg"))
y =table(y,y.pred);colnames(t2)=c("pos_pred","neg_pred")
t2 t2
y.pred
y pos_pred neg_pred
pos 53 32
neg 22 167
=mean(y.pred==y)
Accuracy=t2[1,1]/sum(t2[1,])
Precision=t2[1,1]/sum(t2[,1])
Recall=2*Precision*Recall/(Precision+Recall)
F1=c(Accuracy=Accuracy,Precision=Precision,Recall=Recall,F1=F1);op2 op2
Accuracy Precision Recall F1
0.8029197 0.6235294 0.7066667 0.6625000
准确率(accuracy):80.29%
精确率(precision):62.35%
召回率(recall):70.67%
F1得分(F1 score):66.25%
1.2.3 逐步回归的边际效应
=as.matrix(cbind(1,data.train[,-c(1,2,5)]))
X<-(apply(X,2,mean))
xbat=coef(step.glm)
Wdlogis(sum(xbat*W))*W)[-1] (
BMI Glucose Pregnant
0.018661159 0.007162621 0.025216463
#平均边际效应 AME
mean(dlogis(X %*% W))*W)[-1] (
BMI Glucose Pregnant
0.014024913 0.005383113 0.018951593
- 体重指数每增加1患病风险提高1.87%;血糖浓度每增加1患病风险提高0.72%;怀孕次数每增加一次患病风险提高2.52%。