[R] 10- fold cross validation for naive bayes(e1071)

Wayne.W.Jones at shell.com Wayne.W.Jones at shell.com
Tue Sep 25 11:22:18 CEST 2007


Hi there, 

The tune function is extremely useful for quickly cross validating a model. However, you often need to modify some of the predict functions. 

Here is an example of tuning an svm and naiveBayes with the iris data set: 

naiveBayes: 

For some reason the naivebayes predict function (getS3method("predict","naiveBayes")) doesnt like it when you pass through the classification variable in the data you want to predict for. See the example in help(naiveBayes). Hence I modify the predict function to delete this variable:


"my.predict.NB"<-function(object,newdata){
predict(object,newdata=newdata[,-5],type="class")  
### Note here this will be data set specific. 
### Here the classification variable "Species" in this case appears in the 5th column of the data, hence I delete it.
### Apologies but culdne tquickly find a way to automate this!

}

tune(naiveBayes,Species ~ ., data = iris,predict.fun=my.predict.NB)


SVM: 

The predict function for svm seems to work fine. Hence use: 

obj <- tune(svm, Species~., data = iris, ranges = list(gamma = 2^(-1:1), cost = 2^(2:4)))
plot(obj)



Hope this helps, 

Wayne









-----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: 25 September 2007 09:54
To: R-help at r-project.org
Subject: [R] 10- fold cross validation for naive bayes(e1071)


Hallo!

I would need a code for 10-fold cross validation for the classifiers Naive Bayes and svm (e1071) package. Has there already been done something like that?

I tried to do it myself by applying the tune function first:

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

model <- naiveBayes(code~., mydata, tune.control)
pred <- predict(model, mydata)
table(pred, mydata$code)
chisq.test(code, pred)


but I get the same results as without tuning it.

It would be great if someone could help me with this.

thx a lot, 
Juila
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/multimessenger

______________________________________________
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