r - 如何在 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
解决方案
推荐阅读
- maven - 如何为企业/项目/应用程序执行基于硒的测试/脚本?
- java - 简单 GET/POST 的“WebTarget”与“HttpClient.newHttpClient”
- c++ - C++:使用反射来循环结构中的元素?
- c++ - C ++,映射插入&&文件流读取导致麻烦
- ssrs-2012 - 在 Excel 中呈现 SSRS 时,有没有办法在不同的选项卡上有不同的列?
- google-apps-script - Google 表格 - 跨多个表格使用脚本
- reactjs - 如何为 React.Children.map 定义打字稿
- logstash - 如何在 Logstash 中的消息事件前面添加新字段?
- reactjs - 类型“窗口”上不存在属性“谷歌”
- qt - 无法在 Zynq 中为嵌入式 Linux 编译 Qt 图表