首页 > 解决方案 > 如何在 R 中为朴素贝叶斯模型制作绘图和 ROC 图?

问题描述

我正在尝试制作我的第一个朴素贝叶斯模型,我的问题是我不确定我的 ROC 图或绘图是否正确,这是到目前为止的代码,最后是错误:stat_bin() using bins = 30. Pick better value with binwidth但我没有找到当我查找错误时,我不知道我的函数是否正确,它是 qplot。我也不知道我的朴素贝叶斯设置是否正确,这可能不是正确的输出我不确定朴素贝叶斯通常是如何呈现的,底部是我得到的 ROC 图和绘图结果这是我正在使用的数据集的公开 Google 电子表格。

 library(ElemStatLearn)
#Error in library(ElemStatLearn) : 
#  there is no package called ‘ElemStatLearn’
 library(e1071)
 library(ROCR)
 library(dplyr)
 library(ggplot2)
 library(scales)
 library(reshape)
 
 # Read study file
 getwd()
 bank <- read.csv("attemptCSV.csv")
 #bank

 theme_set(theme_bw())
 
 set.seed(100)
 samp <- sample(nrow(bank), 150, replace=FALSE)
 train <- bank[samp,]
 test <- bank[-samp,]
 
 ## GOOD ONE
 # split into train/test data
 xTrain <- train[,-150]
 #xTrain
 yTrain <- train$y
 yTrain
#  [1] "no"  "no"  "no"  "yes" "yes" "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  
 
 xTest <- test[,-150]
 yTest <- test$y

 # fit naive bayes model with default params
 model <- naiveBayes(xTrain, yTrain)

 # confusion matrix
 table(predict(model, xTest), yTest)
 #    yTest
 #       no  yes
 # no  3834  150
 # yes   30  357

  #histogram of predicted probabilities
  probs <- predict(model, xTest, type="raw")
  qplot(x=probs[, "yes"], geom="histogram")
# `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. //Error

# plot ROC curve
pred <- prediction(probs[, "yes"], yTest)
perf_nb <- performance(pred, measure='tpr', x.measure='fpr')
plot(perf_nb)

performance(pred, 'auc')

这是我的绘图和 ROC 图,当我一次运行部分代码时它们分别工作,但我仍然收到该错误:stat_bin() using bins = 30. Pick better value with binwidth

在此处输入图像描述

在此处输入图像描述

标签: rggplot2bayesiannaivebayesroc

解决方案


推荐阅读