[R] Naive Bayes Issue - Can't Predict - Error is "Error in log(sapply(attribs...)

Peter Ehlers ehlers at ucalgary.ca
Tue Feb 8 15:50:03 CET 2011


Mike,

I think that predict.naiveBayes() wants 'newdata' in the
form of a list or data.frame. You'll find the same problem
if you reduce the HouseVotes84 data set to just one predictor.

This should work for you:

  predict(model,temp[,-1, drop=FALSE])

or, if you prefer:

  predict(model, list(var = temp[, -1])

(With a two-variable data.frame, you might prefer
temp[, 2] over temp[, -1].)

Peter Ehlers


On 2011-02-07 17:20, Mike Schumacher wrote:
> Hey guys,
>
> I can't get my Naive Bayes model to predict.  Forgive me if its simple...
> I've tried about everything and can't get it to work.  Reproduceable code
> below.
>
> Thank you,
>
> Mike

Functional Example Code from UCLA:
http://www.stat.ucl.ac.be/ISdidactique/Rhelp/library/e1071/html/predict.naiveBayes.html
*

install.packages('e1071')
install.packages('mlbench')
library(e1071)
library(mlbench)

data(HouseVotes84)
model <- naiveBayes(Class ~ ., data = HouseVotes84)
predict(model, HouseVotes84[1:10,-1])


*My Code That Errors:*
resp<-c(0,0,0,0,0,0,0,0,0,1,1,1,1,1,1)
var <-c('y','n','y','y','y','n','y','y','y','n','n','n','n','n','y')
temp<-data.frame(resp,var)
print(temp)

model<-naiveBayes(resp~.,data=temp)
model
predict(model,temp[,-1])

#Error:
#Error in log(sapply(attribs, function(v) { :
#Non-numeric argument to mathematical function



More information about the R-help mailing list