我正在运行一个朴素贝叶斯分类模型,我注意到插入符号包返回的结果与 klaR(插入符号引用)或 e1071 不同。
我的问题是:我的插入符号语法是否有问题,我无法恢复与 klaR(或 e1071)相同的结果?
请注意,我在插入符号中运行了一个未调整的模型,并为它提供了与我提供给 klaR 的相同规格(usekernal = FALSE 和 fL = 0)。
在下面的示例中,e1071 和 klaR 返回相同的混淆矩阵(这是有道理的,因为 klaR 基于 e1071 但添加了内核和拉普拉斯平滑器,我已在此处禁用)。奇怪的是,当 caret 被要求运行与 klaR 模型具有相同规格的未调整模型时,结果很接近但不完全相同,但我希望结果与 klaR 相同。
这是一个可重现的示例:
# Load Libraries
library(kernlab); #for spam data
library(caret)
library(e1071)
library(klaR)
# Load Data
data(spam)
# e1071 naiveBayes
set.seed(3456)
fit1 <- naiveBayes(spam, spam$type, type="raw")
pred1 <- predict(fit1, spam, type="class")
confusionMatrix(pred1, spam$type)
# klaR NaiveBayes
set.seed(3456)
fit2 <- NaiveBayes(spam, spam$type, usekernal = FALSE, fL = 0)
pred2 <- predict(fit2, spam)
#Warnings that probability is 0 for some cases
confusionMatrix(pred2$class, spam$type)
# caret with no tuning, usekernal = FALSE, fL = 0
set.seed(3456)
fit3 <- train(type ~ .,
data=spam,
method = "nb",
trControl = trainControl(method="none"),
tuneGrid = data.frame(fL=0, usekernel=FALSE))
pred3 <- predict(fit3, spam, type="raw")
#Warnings that probability is 0 for some cases
confusionMatrix(pred3, spam$type)
以下是从混淆矩阵中选择的输出。
对于 e1071:
Accuracy : 0.7266
Sensitivity : 0.5814
Specificity : 0.9498
对于克拉:
Accuracy : 0.7266
Sensitivity : 0.5814
Specificity : 0.9498
对于插入符号:
Accuracy : 0.7135
Sensitivity : 0.5610
Specificity : 0.9482
非常感谢有关为什么会发生这种情况以及我可以做些什么(如果有的话)的任何信息。
谢谢!
编辑:以防万一这有帮助,来自 sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] klaR_0.6-12 MASS_7.3-45 e1071_1.6-7 caret_6.0-58 ggplot2_1.0.1 lattice_0.20-33 kernlab_0.9-22
loaded via a namespace (and not attached):
[1] Rcpp_0.12.1 magrittr_1.5 splines_3.2.2 munsell_0.4.2 colorspace_1.2-6 foreach_1.4.3
[7] minqa_1.2.4 stringr_1.0.0 car_2.1-0 plyr_1.8.3 tools_3.2.2 parallel_3.2.2
[13] nnet_7.3-11 pbkrtest_0.4-2 grid_3.2.2 gtable_0.1.2 nlme_3.1-122 mgcv_1.8-7
[19] quantreg_5.19 class_7.3-14 MatrixModels_0.4-1 iterators_1.0.8 lme4_1.1-10 digest_0.6.8
[25] Matrix_1.2-2 nloptr_1.0.4 reshape2_1.4.1 codetools_0.2-14 stringi_1.0-1 scales_0.3.0
[31] combinat_0.0-8 stats4_3.2.2 SparseM_1.7 proto_0.3-10