3
$\begingroup$

I have the following data set and am trying to predict on the naive bayes values

> head(dat)
   lead_id                      email owns_home credit_type buyer_returned
1 12073402              b@hotmail.com         0        GOOD            \\0
2 12073514                 mo@cox.net         0        GOOD            \\0
3 12073541                v@yahoo.com         0   EXCELLENT            \\0
4 12073591              rob@yahoo.com         1   EXCELLENT           \001
5 12073653                c@ymail.com         0   EXCELLENT            \\0
6 12073768                r@yahoo.com         0   EXCELLENT            \\0

So I run the following command to run NB and to predict on the desired values.

m <- naiveBayes(buyer_returned ~ owns_home + credit_type, dat)
m

> predict(m, dat[1:10, -5])
Error in FUN(1:2[[2L]], ...) : subscript out of bounds
> predict(m, dat[1:10, -5], type="raw")
Error in FUN(1:2[[2L]], ...) : subscript out of bounds

> predict(m, dat[1:10, 5])
Error in log(sapply(attribs, function(v) { : 
  Non-numeric argument to mathematical function
> predict(m, dat[1:10, 5], type="raw")
Error in log(sapply(attribs, function(v) { : 
  Non-numeric argument to mathematical function

I'm not sure why I get the subscript out of bounds error. What is the issue.

Here's info that might be useful:

> str(dat)
'data.frame':   50000 obs. of  5 variables:
 $ lead_id       : int  12073402 12073514 12073541 12073591 12073653 12073768 12073809 12073818 12073897 12073943 ...
     $ email         : Factor w/ 49755 levels "0011werbicki1100@gmail.com",..: 6231 32482 47424 38883 6784 37311 33483 37525 41427 35459 ...
 $ owns_home     : int  0 0 0 1 0 0 0 0 0 0 ...
     $ credit_type   : Factor w/ 5 levels "\\N","EXCELLENT",..: 4 4 2 2 2 2 2 4 2 4 ...
 $ buyer_returned: Factor w/ 2 levels "\001","\\0": 2 2 2 1 2 2 2 2 2 2 ...
    > str(m)
    List of 4
     $ apriori: 'table' int [1:2(1d)] 2865 47135
  ..- attr(*, "dimnames")=List of 1
  .. ..$ Y: chr [1:2] "\001" "\\0"
     $ tables :List of 2
  ..$ owns_home  : num [1:2, 1:2] 0.358 0.268 0.479 0.443
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ Y        : chr [1:2] "\001" "\\0"
  .. .. ..$ owns_home: NULL
      ..$ credit_type: table [1:2, 1:5] 0.1888 0.086 0.3218 0.4358 0.0932 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ Y          : chr [1:2] "\001" "\\0"
      .. .. ..$ credit_type: chr [1:5] "\\N" "EXCELLENT" "FAIR" "GOOD" ...
 $ levels : chr [1:2] "\001" "\\0"
     $ call   : language naiveBayes.default(x = X, y = Y, laplace = laplace)
 - attr(*, "class")= chr "naiveBayes"

I am trying to get the predicted values for each lead_id.

$\endgroup$
3
$\begingroup$

I guess you are referring to the e1071 implementation of Naive Bayes?

It looks like there are two possibilities that can cause this error. The first one is that the factor variables may have value in the prediction data set, which did not occur in the training set. To avoid this, you would have to extend the factor levels already in the training data set.

However there seems to be another bug that causes this error even if you use the training data itself in the prediction. I was looking into the details of the implementation and it turns out that it cannot deal with columns that are of the type "Date". Internally the implementation relies on table, and indexing a table by a "Date" value is not possible (at least that is where the prediction failed for me). A simple solution is to convert any date features to numerics. Note that in this case the implementation will make a Gaussian assumption, which is maybe also not what you want for a date variable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.