0

I am getting an error while using naiveBayes() method in R. I am passing the the as.matrix(train_matrix)as first parameter and as.factor(train_data$subcategory) to the naiveBayes function.

I am getting below error :

model <- naiveBayes(as.matrix(trainmatrix),as.factor(traindata$subCategory)); Error in tapply(var, y, mean, na.rm = TRUE) : arguments must have same length

Can anyone help me on this?

model <- naiveBayes(as.matrix(trainmatrix),as.factor(traindata$subCategory));

Where trainMatrix is term document matrix, traindata - is the data on which in need the model to get trained on and subCategory is the class (different levels).

Am i doing it the right way?

Madhav pandey
  • 351
  • 1
  • 4
  • 12

2 Answers2

0

The error clearly says that your matrix(train matrix) is not the same length as the data frame(train data) which is obviously expected. use the syntax model<-naiveBayes(traindata$subCategory~.,data=traindata)

  • @ i have got the prediction result in the form of a factor but now i am not able to calculate the confustion matrix!! Can anyone share the way to do it?? – Madhav pandey Apr 14 '16 at 14:17
0

Done with creating model and confusion matrix. Below are the code snippet:

model <- naiveBayes(as.matrix(trainmatrix),as.factor(traindata$subCategory));

confusionMatrix(results, traindata$subCategory)

Where results is the prediction outcome. Note : The length of results and traindatat$subcategory has to be same,(which i was missing).

Madhav pandey
  • 351
  • 1
  • 4
  • 12