【问题标题】:RoC Curve with Logistic Regression具有逻辑回归的 RoC 曲线
【发布时间】:2018-06-02 11:56:57
【问题描述】:

我想知道如何用 R 绘制 ROC 图。 我创建了一个带有 k 折交叉验证的逻辑回归模型。

dt3 - 主数据集

dt3Training - 从主数据集分割训练

dt3Test - 从主数据集进行的测试拆分

以下是用于逻辑回归的代码:

ctrl<- trainControl (method="repeatedcv", number = 10, repeats =5, savePredictions="TRUE"

modelfit <- train (Attrition~., data=dt3, method="glm", family="binomial", trControl=ctrl)

pred = predict (modelfit, newdata=dt3Test)

confusionMatrix(data=pred, dt3Test$Attrition)

我的问题是,pred 没有显示为预测,而是显示为数据表。因此下面的代码给出了一个错误。

perf1 <- performance(pred,"tpr","fpr")

plot(perf1)

如果您能帮我解决这个问题,我将不胜感激。

更新:查看k-fold cross validation - how to get the prediction automatically? 后,我将代码更改为以下:

library("caret", lib.loc="~/R/win-library/3.4")
load (df) ## load main dataset "df"
tc <- trainControl("cv",10,savePred=T) ##create folds
(fit<-train(Attrition~.,data=df,method="glm",family="binomial",trControl=tc)) ##train model, predict Attrition with all other variables

我想试试 Claus Wilke 下面的代码,但是我很困惑,因为我只有我的主要数据 (df) 和我的模型 (fit)。

data.frame(predictor = predict(fit, df),
known.truth = fit$Attrition,
model = "fit") 

data.frame(predictor = predict(fit, tc),
known.truth = tc$Attrition,
model = "fit") 

对不起,如果我问了一个非常愚蠢的问题,但我没有太多时间来完成我的项目。而且我以前没有使用 R 的经验。

【问题讨论】:

标签: r prediction roc


【解决方案1】:

由于您没有提供可重现的示例,我将使用不同的数据集和模型。对于 ggplot2,plotROC 包提供了适用于任何拟合模型的通用 ROC 绘图功能。您只需将已知事实和预测概率(或其他数字预测变量)放入数据框中,然后交给 geom。示例如下。

library(MASS) # for Pima data sets
library(ggplot2)
library(plotROC)

# train model on training data
glm.out.train <- glm(type ~ npreg + glu + bp + bmi + age,
                     data = Pima.tr,
                     family = binomial)

# combine linear predictor and known truth for training and test datasets into one data frame
df <- rbind(data.frame(predictor = predict(glm.out.train, Pima.tr),
                       known.truth = Pima.tr$type,
                       model = "train"),
            data.frame(predictor = predict(glm.out.train, Pima.te),
                       known.truth = Pima.te$type,
                       model = "test"))

# the aesthetic names are not the most intuitive
# `d` (disease) holds the known truth
# `m` (marker) holds the predictor values 
ggplot(df, aes(d = known.truth, m = predictor, color = model)) + 
  geom_roc(n.cuts = 0)

【讨论】:

    【解决方案2】:

    我找到了一种绘制 ROC 曲线的方法 - 我将从一开始就写下代码 - 创建模型然后是 ROC 曲线:

    用 k 折创建逻辑回归:

    library("caret", lib.loc="~/R/win-library/3.4")
    load (df) 
    ## load main dataset "df"
    
    tc <- trainControl("cv",10,savePred=T)
    ##create folds
    
    (fit<-train   (Attrition~.,data=df,method="glm",family="binomial",trControl=tc)) 
    ##train model, predict Attrition with all other variables
    

    对于 ROC 曲线:

    library(ggplot2)
    library(ROCR)
    
    predict0 <- predict(fit, type = 'raw')
    
    ROCRpred0 <- prediction(as.numeric(predict0),as.numeric(df$Attrition))
    
    ROCRperf0<- performance(ROCRpred0, 'tpr', 'fpr')
    
    plot(ROCRperf0, colorize=TRUE, text.adj=c(-0.2,1.7))
    

    我可以用这段代码得到一个情节,我希望我可以帮助其他有同样问题的人。Sample ROC Curve - discrete values

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 1970-01-01
      • 2018-10-13
      • 2013-12-12
      • 2015-02-19
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 2013-08-29
      相关资源
      最近更新 更多