I am performing logistic regression using this page. My code is as below.
我正在使用此页面执行逻辑回归。我的代码如下。
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
After running this code mydata dataframe has two columns - 'admit' and 'prob'. Shouldn't those two columns sufficient to get the ROC curve?
运行此代码后,mydata dataframe有两列 - 'admit'和'prob'。这两列不应该足以获得ROC曲线吗?
How can I get the ROC curve.
如何获得ROC曲线。
Secondly, by loooking at mydata, it seems that model is predicting probablity of admit=1
.
其次,通过在mydata上闲逛,模型似乎预测了admit = 1的可能性。
Is that correct?
那是对的吗?
How to find out which particular event the model is predicting?
如何找出模型预测的特定事件?
Thanks
谢谢
UPDATE: It seems that below three commands are very useful. They provide the cut-off which will have maximum accuracy and then help to get the ROC curve.
更新:似乎以下三个命令非常有用。它们提供了最大精度的截止点,然后有助于获得ROC曲线。
coords(g, "best")
mydata$prediction=ifelse(prob>=0.3126844,1,0)
confusionMatrix(mydata$prediction,mydata$admit
3 个解决方案
#1
25
The ROC curve compares the rank of prediction and answer. Therefore, you could evaluate the ROC curve with package pROC
as follow:
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)
#2
7
another way to plot ROC Curve...
另一种绘制ROC曲线的方法......
library(Deducer)
modelfit <- glm(formula=admit ~ gre + gpa, family=binomial(), data=mydata, na.action=na.omit)
rocplot(modelfit)
#3
1
#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
#1
25
The ROC curve compares the rank of prediction and answer. Therefore, you could evaluate the ROC curve with package pROC
as follow:
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)
#2
7
another way to plot ROC Curve...
另一种绘制ROC曲线的方法......
library(Deducer)
modelfit <- glm(formula=admit ~ gre + gpa, family=binomial(), data=mydata, na.action=na.omit)
rocplot(modelfit)
#3
1
#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