[R] NAIVE BAYES with 10-fold cross validation

Kuhn, Max Max.Kuhn at pfizer.com
Tue Oct 30 22:03:49 CET 2007


> am trying to implement the code of the e1071 package for naive bayes, 
> but it doens't really work, any ideas??
> am very glad about any help!!
> need a naive bayes with 10-fold cross validation:

The caret package will do this. Use

 fit <- train(
   x, y, method = "nb", 
   trControl = trainControl(method = "cv", number = 10))

(there is no formula interface yet).

It will use the naïve Bayes implementation in klaR. Unless you specify otherwise, it will train naïve Bayes models with and without using kernel density estimation (but you can change that). 

The object fit$finalModel will contain the model fit that is "cv optimal".

For example:

 > fit <- train(
 +    iris[,-5], iris$Species, "nb", 
 +    trControl = trainControl(method = "cv", number = 10))
 Iter 1  Values: TRUE 
 Loading required package: MASS
 Loading required package: class
 Iter 2  Values: FALSE 
 > 
 > fit
 
 Call:
 train.default(x = iris[, -5], y = iris$Species, method = "nb", 
     trControl = trainControl(method = "cv", number = 10))
 
 150 samples
 4 predictors
 
 summary of cross-validation (10 fold) sample sizes:
     135, 135, 135, 135, 135, 135, ... 
 
 cv resampled training results across tuning parameters:
 
   usekernel  Accuracy  Kappa  Accuracy SD  Kappa SD  Optimal
   FALSE      0.953     0.93   0.0706       0.106            
   TRUE       0.96      0.94   0.0562       0.0843    *      
 
 Accuracy was used to select the optimal model
 

Max


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of "Julia Kröpfl"
Sent: Tuesday, October 30, 2007 4:46 PM
To: r-help at r-project.org
Subject: [R] NAIVE BAYES with 10-fold cross validation

hi there!!

i am trying to implement the code of the e1071 package for naive bayes, but it doens't really work, any ideas??
i am very glad about any help!!
i need a naive bayes with 10-fold cross validation:

code:
library(e1071)

model <- naiveBayes(code ~ ., mydata)

tune.control <- tune.control(random = FALSE, nrepeat = 1, repeat.aggregate = min,
sampling = c("cross"), sampling.aggregate = mean,
cross = 10, best.model = TRUE, performances = TRUE)

pred <- predict(model, mydata[,-12], type="class")
tune(naiveBayes, code~., mydata, predict.fun=pred, tune.control)


thx for your help!
cheers, julia
--

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list