0
votes

I'm getting the above error when trying to run my R code (Naive Bayes model using e1071).

1) Setup: I created a csv table with the following six fields:

dep variable    -> cncl_flag: 0,1

indep variables -> Channel:  red, blue, green, black 
                -> Age:      <1, 1-2, >2
                -> Cases:    0,1
                -> Products: product 1, product 2, ... , product 8
                -> Dollars:  <100, 100-200, ... , >1000

2) R Code: Using the table detailed above I run the following R code:

myData<-read.csv("C:/Users/me/Desktop/R/NBC.csv", header = TRUE, sep = ",")

myData[,"train"] <- ifelse(runif(nrow(myData))<0.80,1,0)
trainColNum <- grep("train",names(myData))
trainmyData <- myData[myData$train==1,-trainColNum]
testmyData <- myData[myData$train==0,-trainColNum]


library(e1071)
nb_model <- naiveBayes(cncl_flag~.,data = trainmyData)

summary(nb_model)
nb_test_predict <- predict(nb_model,testmyData[,-1])
table(pred=nb_test_predict,true=testmyData$cncl_flag)

3) Output: Error in table(pred = nb_test_predict, true = testmyData$cncl_flag) : all arguments must have the same length

4) What I've tried: I removed all rows containing any "NA" or blank results

1

1 Answers

-1
votes

If you can check the lengths of nb_test_predict and testmyData$cncl_flag by using length() function, you will understand the reason for the error.