Damian Krstajic
2009-Jun-30 15:31 UTC
[R] NaiveBayes fails with one input variable (caret and klarR packages)
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)); > 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]]
Max Kuhn
2009-Jun-30 18:12 UTC
[R] NaiveBayes fails with one input variable (caret and klarR packages)
I'm figuring this out now and I'll let you know when it is resolved... On Tue, Jun 30, 2009 at 11:31 AM, Damian Krstajic<dkrstajic at hotmail.com> 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)); >> 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. >-- Max
Uwe Ligges
2009-Jun-30 21:56 UTC
[R] NaiveBayes fails with one input variable (caret and klarR packages)
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.