【问题标题】:R logistic regression area under curve曲线下的 R 逻辑回归面积
【发布时间】:2013-08-29 05:32:54
【问题描述】:

我正在使用这个page 执行逻辑回归。我的代码如下。

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mylogit <- glm(admit ~ gre, data = mydata, family = "binomial")
summary(mylogit)
prob=predict(mylogit,type=c("response"))
mydata$prob=prob

运行此代码后,mydata 数据框有两列 - 'admit' 和 'prob'。 这两列不应该足以获得 ROC 曲线吗?

如何获得 ROC 曲线。

其次,通过查看 mydata,该模型似乎在预测 admit=1 的概率。

正确吗?

如何找出模型预测的特定事件?

谢谢

更新: 看来下面三个命令很有用。它们提供了具有最大精度的截止值,然后有助于获得 ROC 曲线。

coords(g, "best")

mydata$prediction=ifelse(prob>=0.3126844,1,0)

confusionMatrix(mydata$prediction,mydata$admit

【问题讨论】:

  • 用小数据集测试您对预测内容的不确定性不是很简单吗?还是只看with(mydata, table(admit,gre))的结果?逻辑回归只是对一堆表进行估计。)
  • 是的...我们可以这样做..我按照相同的方法得出结论,它预测的当前案例承认 = 1..但认为 R 会有一些捷径这将证实我的想法。关于找出能够从 roc 对象中获得最大准确性的阈值有什么评论吗?
  • 关于“关于找出能够从 roc 对象中获得最大准确性的阈值的任何评论?”:我认为答案是 coords(g, "best")...

标签: r regression roc confusion-matrix


【解决方案1】:

ROC 曲线比较预测和答案的排名。因此,您可以使用 pROC 包评估 ROC 曲线,如下所示:

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")
mylogit <- glm(admit ~ gre, data = mydata, family = "binomial")
summary(mylogit)
prob=predict(mylogit,type=c("response"))
mydata$prob=prob
library(pROC)
g <- roc(admit ~ prob, data = mydata)
plot(g)    

【讨论】:

  • 这是有道理的。如果可能,请回答“其次,通过查看 mydata,模型似乎预测了承认 = 1 的概率。正确吗?如何找出模型预测的特定事件?”也。我查看了 roc 对象并了解 g$sensitive 和 g$specificities 会给我特定的值,但是如果我想找出可以提供最大准确度的阈值,那么我可以从 roc 对象中获取该数字吗?
  • @wush978 “admit”变量是预测类还是实际类?
  • 获取数据的那个 URL 现在似乎已经过时了。对于有兴趣复制此示例的其他任何人,现在似乎可行的是 mydata stats.idre.ucla.edu/stat/data/binary.csv") (使用不想出现在评论中的 https:// 前缀 tho')
【解决方案2】:

另一种绘制 ROC 曲线的方法...

library(Deducer)
modelfit <- glm(formula=admit ~ gre + gpa, family=binomial(), data=mydata, na.action=na.omit)
rocplot(modelfit)

【讨论】:

  • 您需要为此安装 Java,否则您会收到错误消息,仅供参考。 Error: .onLoad failed in loadNamespace() for 'rJava', details: call: fun(libname, pkgname) error: JAVA_HOME cannot be determined from the Registry
【解决方案3】:
#Another way to plot ROC

mydata <- read.csv("http://www.ats.ucla.edu/stat/data/binary.csv")   
mylogit <- glm(admit ~ gre, data = mydata, family = "binomial")    
summary(mylogit)     
prob=predict(mylogit,type=c("response"))    
library("ROCR")    
pred <- prediction(prob, mydata$admit)    
perf <- performance(pred, measure = "tpr", x.measure = "fpr")     
plot(perf, col=rainbow(7), main="ROC curve Admissions", xlab="Specificity", 
     ylab="Sensitivity")    
abline(0, 1) #add a 45 degree line

【讨论】:

  • 你能对你的答案添加一些解释吗?
  • @Conny AUC 可以计算为auc = performance(pred, "auc")
  • @SIslam 感谢您的评论!问题的标题是 AUC,而每个人都在谈论 ROC。它们是相关的概念,但并不相同。
猜你喜欢
  • 2016-08-09
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 2016-07-26
  • 2013-06-26
  • 1970-01-01
  • 2018-10-13
  • 1970-01-01
相关资源
最近更新 更多