|
选取了350炉数据,分为两组,分别用于建模与预测
a=data[1:250,]
b=data[251:350,]
a[1:3,]
SI0 SI1 LS1 FL1 LS2
1 1 0.5825 0.8998203 0.9494003 0.7821347
2 0 0.5000 0.7933116 0.9334815 0.8998203
3 0 0.5700 0.7205215 0.8978194 0.7933116
建立logistic回归模型
glm.logit=glm(SI0~.,data=a,family=binomial(link=logit))
summary(glm.logit)
计算预测值
p=predict(glm.logit,b)
p=exp(p)/(1+exp(p))
将阈值定为0.5,得到分类结果
b$SI0pred=1*(p>0.5)
计算频数
table(b[,c(1,6)])
SI0pred
SI0 0 1
0 28 18
1 12 42
于是
TPR=77.8%
FPR=39.1%
PV=70%
k=54%
lift=1.3
绘制ROC曲线
TPR=rep(0,1000)
FPR=rep(0,1000)
for(i in 1:1000){
p0=i/1000;
SI0.true=b$SI0
SI0.pred=1*(p>p0)
TPR=sum(SI0.pred*SI0.true)/sum(SI0.true)
FPR=sum(SI0.pred*(1-SI0.true))/sum(1-SI0.true)}
plot(FPR,TPR,type="l",col=2)
points(c(0,1),c(0,1),type="l",lty=2)
|