21

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?

How can I get the ROC curve.

Secondly, by loooking at mydata, it seems that model is predicting probablity of 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.

coords(g, "best")

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

confusionMatrix(mydata$prediction,mydata$admit
4

3 回答 3

42

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

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)    
于 2013-08-26T17:00:07.213 回答
11

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

library(Deducer)
modelfit <- glm(formula=admit ~ gre + gpa, family=binomial(), data=mydata, na.action=na.omit)
rocplot(modelfit)
于 2014-09-10T13:07:53.293 回答
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
于 2015-11-24T17:25:09.843 回答