Skip to content

what is Non-numeric argument to mathematical function in prediction ?

3 messages · Petr Savicky, kiinalist

#
On Sat, May 05, 2012 at 09:21:10AM +0300, kiinalist wrote:
Hi.

A untested suggestion is to try

  m <- naiveBayes(iris[,1, drop=FALSE], iris[,5])

The difference is that iris[,1] is not a dataframe, while
both iris[,1:2] and iris[,1, drop=FALSE] are.

Hope this helps.

Petr Savicky.
1 day later
#
Thanks. It does work now.

I also get another problem when I use naivebayes and prediction in myapplication
There was anerror message:
  
Error in table(predict(nb.obj, test.data[, subset, drop = FALSE]),  test.data[,  :
     all arguments must have the same length
  
The length of test.data[,subset, drop=FALSE] and  test.data[,ncol(expr.matrix)] of are the same. I further checked length ofpredict(nb.obj, test.data[, subset, drop = FALSE]) is 0. I do not know why.
  
The script is shown below. I also attached test data

  evaluator<- function(subset){
       # k-fold cross validation
       k<- 5
       splits<- runif(nrow(expr.matrix))
       results<- sapply(1:k, function(i){
                                   test.indx<- (splits>= (i-1)/k)&    (splits<i/k)
                                   train.indx<- !test.indx
                                   test.data<- expr.matrix[test.indx, ,  drop=FALSE]
                                   train.data<- expr.matrix[train.indx, ,  drop=FALSE]
                                   nb.obj<-  naiveBayes(train.data[,subset, drop=FALSE], train.data[,ncol(expr.matrix)])
                                   error.rate<- sum(predict(nb.obj,test.data[,subset, drop=FALSE]) ==  test.data[,ncol(expr.matrix)])/nrow(test.data)
                                   return (error.rate)
                               })
       print(subset)
       print(mean(results))
       return(mean(results))
  }
  
  
  subset<- best.first.search(colnames(expr.matrix)[-ncol(expr.matrix)],  evaluator)


  Br,
  Luffy
On 05/05/2012 12:53 PM, Petr Savicky wrote: