生物信息学数据科学与R语言R. python新手日记

R语言 ROC曲线

2018-01-15  本文已影响136人  柳叶刀与小鼠标
setwd("E:\\Rwork")
library(ROCR)
data(ROCR.simple)
pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels) 
#ROCR.simple$predictions为预测标签,ROCR.simple$labels为真实标签
perf <- performance(pred,"tpr","fpr")
auc <- performance(pred,'auc')
auc = unlist(slot(auc,"y.values"))
plot(perf,
     xlim=c(0,1), ylim=c(0,1),col='red', 
     main=paste("ROC curve (", "AUC = ",auc,")"),
     lwd = 2, cex.main=1.3, cex.lab=1.2, cex.axis=1.2, font=1.2)
abline(0,1)
data(Affairs, package="AER")
summary(Affairs)

Affairs$ynaffair[Affairs$affairs > 0] <- 1
Affairs$ynaffair[Affairs$affairs == 0] <- 0
Affairs$ynaffair <- factor(Affairs$ynaffair,
                             levels=c(0,1),
                             labels=c("No","Yes"))
table(Affairs$ynaffair)
fit.full <- glm(ynaffair ~ gender + age + yearsmarried + children +
                  religiousness + education + occupation +rating,
                data=Affairs, family=binomial())
summary(fit.full)

# 做一个logistic回归,生成概率预测值

pre <- predict(fit.full,type='response')
# 将预测概率prob和实际结果y放在一个数据框中
data <- data.frame(prob=pre,obs=Affairs$ynaffair)
# 按预测概率从低到高排序

library(pROC)
modelroc <- roc(Affairs$ynaffair,pre)
plot(modelroc, print.auc=TRUE, auc.polygon=TRUE, grid=c(0.1, 0.2),
     grid.col=c("green", "red"), max.auc.polygon=TRUE,
     auc.polygon.col="skyblue", print.thres=TRUE)

0.9 ~ 1.0 ( 优秀)
0.8 ~ 0.9 (良好)
0.7 ~ 0.8(一般)
0.6 ~ 0.7(很差)
0.5 ~ 0.6(无法区分)

上一篇下一篇

猜你喜欢

热点阅读