[R] Bug in predict function for naiveBayes?

Liaw, Andy andy_liaw at merck.com
Thu Feb 26 21:21:58 CET 2009


You need to set "Class" as factor before you call naiveBayes(); i.e.,

    mixture.train$Class <- factor(mixture.train$Class)

Then you can just do:

    pred.bayes <-predict(Bayes.res, mixture.test, type="class")

Andy 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of joris meys
> Sent: Thursday, February 26, 2009 1:36 PM
> To: r-help at r-project.org
> Subject: Re: [R] Bug in predict function for naiveBayes?
> 
> Dear all,
> 
> I've sent a mail last week already. I've been trying to get 
> this predict
> function work, but somehow I keep on getting the same error. 
> Read the help
> files, searched the internet, but I don't seem to get what 
> I'm doing wrong.
> 
> Anybody who has experience with this function? It's contained 
> in the package
> e1071.
> Thank you in advance
> 
> On Thu, Feb 19, 2009 at 2:16 PM, joris meys 
> <jorismeys at gmail.com> wrote:
> 
> > Dear all,
> >
> > I tried a simple naive Bayes classification on an 
> artificial dataset, but I
> > have troubles getting the predict function to work with the 
> type="class"
> > specification. With type= "raw", it works perfectly, but 
> with type="class" I
> > get following error :
> >
> > Error in as.vector(x, mode) : invalid 'mode' argument
> >
> > Data : mixture.train is a training set with 100 points 
> originating from 2
> > multivariate gaussian distributions (class 0 and class 1), 
> with X1 and X2 as
> > coordinates in a 2-dimensional space. Mixture.test is a 
> grid going from -15
> > to +15 in both dimensions. Stupid data, but it's just to test.
> >
> > Code :
> > Sigma <- matrix(c(10,3,3,2),2,2)
> > mixture.train <- cbind(mvrnorm(n=50, c(0, 2), Sigma),rep(0,50))
> > mixture.train <- 
> as.data.frame(rbind(mixture.train,cbind(mvrnorm(n=50, c(2,
> > 0), Sigma),rep(1,50))))
> > names(mixture.train) <-c("X1","X2","Class")
> > X1 <- rep(seq(-15,15,by=1),31)
> > X2 <- rep(seq(-15,15,by=1),each = 31)
> > mixture.test <- data.frame(X1,X2)
> >
> > Bayes.res <- naiveBayes(Class ~ X1 + X2, data=mixture.train)
> > pred.bayes <-predict(Bayes.res, cbind(mixture.test$X1,
> > mixture.test$X2),type="class")
> >
> > Tried it also with pred.bayes <-predict(Bayes.res,
> > mixture.test,type="class"), but that gives the same effect. 
> Is this a bug or
> > am I missing something?
> >
> > Kind regards
> > Joris Meys
> > University Ghent
> >
> >
> >
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 
Notice:  This e-mail message, together with any attachme...{{dropped:12}}




More information about the R-help mailing list