It is the error message on the topic. I am getting this error when I try to run naive.bayes classifier. Here is the summary of my train data:
'data.frame': 7269 obs. of 193 variables:
$ pid : int 2 4 5 7 10 11 14 18 25 31 ...
$ acquir : int 0 0 0 0 1 1 0 0 0 0 ...
$ addit : int 0 0 0 0 2 2 0 0 0 0 ...
$ agre : int 0 0 0 0 0 0 0 0 0 0 ...
$ agreement : int 0 0 0 0 0 0 0 0 0 0 ...
$ also : int 1 0 0 0 2 2 0 0 0 0 ...
$ american : int 0 0 0 0 0 0 0 0 0 0 ...
$ announc : int 0 0 0 0 0 0 0 0 0 0 ...
$ annual : int 0 0 0 0 0 0 0 0 2 0 ...
$ approv : int 0 3 0 0 0 0 0 0 0 0 ...
$ april : int 0 0 0 0 0 0 0 0 1 0 ...
$ bank : int 0 7 0 0 0 0 0 0 0 0 ...
$ base : int 0 0 0 0 0 0 0 0 0 0 ...
.
.
$... all of them are integer, except the class column
.
.
$ class : Factor w/ 10 levels "acq","corn","crude",..: 1 1 4 4 9 1 4 3 1 4 ...
And this is the naive.bayes()
line:
model <- naiveBayes(as.factor(class) ~ ., data = as.matrix(train), laplace = 3)
Can anyone tell me why it is happening?:
Error in sum(x) : invalid 'type' (character) of argument
Eventually your data is converted to character because of as.matrix(train)
. Try
model <- naiveBayes(class ~ ., data=train, laplace = 3)
or eventually
model <- naiveBayes(train$class ~ ., data=train[, -c("class")], laplace = 3)
The second variant is more or less the same as the first variant. The .
in the RHS of the formula is expanded to 'all other variables'; so it excludes the column class
mentioned on the LHS. (More information is in the documentation of formula
)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments