[R] How to do Naive Bayes in R?

Dylan Beaudette debeaudette at ucdavis.edu
Wed May 6 22:20:29 CEST 2009


On Wednesday 06 May 2009, Tim LIU wrote:
> I am wondering if anybody here have a simple example in R for Naive
> Bayes.
>
> For example, I can do k-means clustering on the "iris" data -
>
> data(iris)
> cl <- kmeans(iris[,1:4], 3)
> cl$cluster
> cbind(1:150,iris$Species)
>
> ===========
>
> But how to do Naive Bayes classification in the same "iris" data?
>
> Many thanks!

# start here:
help.search('bayes')

# in which you will find:
e1071::naiveBayes

# which means:
library(e1071)
?naiveBayes

# and compared to your example:
cl <- kmeans(iris[,1:4], 3)
table(cl$cluster, iris[,5])

m <- naiveBayes(iris[,1:4], iris[,5])
table(predict(m, iris[,1:4]), iris[,5])

Dylan

-- 
Dylan Beaudette
Soil Resource Laboratory
http://casoilresource.lawr.ucdavis.edu/
University of California at Davis
530.754.7341




More information about the R-help mailing list