use*_*622 15 regression r confusion-matrix 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
Run Code Online (Sandbox Code Playgroud)
运行此代码后,mydata dataframe有两列 - 'admit'和'prob'.这两列不应该足以获得ROC曲线吗?
如何获得ROC曲线.
其次,通过嘲笑mydata,似乎模型正在预测可能性admit=1.
那是对的吗?
如何找出模型预测的特定事件?
谢谢
更新:似乎以下三个命令非常有用.它们提供了最大精度的截止点,然后有助于获得ROC曲线.
coords(g, "best")
mydata$prediction=ifelse(prob>=0.3126844,1,0)
confusionMatrix(mydata$prediction,mydata$admit
Run Code Online (Sandbox Code Playgroud)
wus*_*978 30
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)
Run Code Online (Sandbox Code Playgroud)
另一种绘制ROC曲线的方法......
library(Deducer)
modelfit <- glm(formula=admit ~ gre + gpa, family=binomial(), data=mydata, na.action=na.omit)
rocplot(modelfit)
Run Code Online (Sandbox Code Playgroud)
小智 5
#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
Run Code Online (Sandbox Code Playgroud)