[R] NaiveBayes fails with one input variable (caret and klarR packages)

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Jun 30 23:56:16 CEST 2009



Damian Krstajic wrote:
> Hello,
> 
> We have a system which creates thousands of regression/classification models and in cases where we have only one input variable  NaiveBayes throws an error. Maybe I am mistaken and I shouldn't expect to have a model with only one input variable.
> 
> We use R version 2.6.0 (2007-10-03). We use caret (v4.1.19), but have tested similar code with klaR (v.0.5.8), because caret relies on NaiveBayes implementation from klaR. I get different error messages from caret than from klaR so I will provide the code for caret usage and klaR usage.
> 
> Here is the code which uses the iris dataset.
> 
>> library(klaR);
> Loading required package: MASS
>> X<-iris["Sepal.Length"];
>> Y<-iris["Species"];
>> mnX<-as.matrix (X);
>> mnY<-as.matrix (Y);
>> cY<-factor(mnY);
>> d <- data.frame (cbind(mnX,cY));

In the line above you coerce cY to numeric (in a matrix) again.

Rather use
d <- data.frame(mnX=mnX[,1], cY=cY)
and proceed.

I will add some more error checking into NaiveBayes().

Best wishes,
Uwe Ligges



>> m<-NaiveBayes(cY~mnX, data=d);
>> predict(m);
> Error in as.vector(x, mode) : invalid argument 'mode'
>> library(caret);
> Loading required package: lattice
>> mCaret<-train(mnX,cY,method="nb",trControl = trainControl(method = "cv", number = 10));
> Loading required package: class
> Fitting: usekernel=TRUE
> Fitting: usekernel=FALSE
>> predicted <- predict(mCaret, newdata=mnX);
> Error in 1:nrow(newdata) : NA/NaN argument
> 
> We use caret to call NaiveBayes and we don't have any error messages in cases where the number of input variables is greater than 1.
> 
> Cheers
> DK
> 
> _________________________________________________________________
> [[elided Hotmail spam]]
> 
> 	[[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.




More information about the R-help mailing list