Solved – SMOTE throws error for multi class imbalance problem

classificationoversamplingrunbalanced-classes

I am trying to use SMOTE to correct imbalance in my multi-class classification problem.
Although SMOTE works perfectly on the iris dataset as per the SMOTE help document, it does not work on a similar dataset.
Here is how my data looks. Note it has three classes with values 1, 2, 3.

> data
   looking risk every status
1        0    1     0      1
2        0    0     0      1
3        0    0     0      2
4        0    0     0      1
5        0    0     0      1
6        3    0     0      1
7        0    0     0      1
8        0    0     0      1
9        0    1     0      1
10       0    0     0      1
11       0    0     0      3
12       0    0     0      1
13       0    0     0      1
14       0    0     0      1
15       0    0     0      2

It is in the form of dataframe, same as iris:

> class(data)
[1] "data.frame"

Here is my code using SMOTE and the error that it throws:

> newData <- SMOTE(status ~ ., data, perc.over = 600,perc.under=100)
Error in scale.default(T, T[i, ], ranges) : subscript out of bounds
In addition: Warning messages:
1: In FUN(newX[, i], ...) :
  no non-missing arguments to max; returning -Inf
2: In FUN(newX[, i], ...) :
  no non-missing arguments to max; returning -Inf
3: In FUN(newX[, i], ...) :
  no non-missing arguments to max; returning -Inf
4: In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf
5: In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf
6: In FUN(newX[, i], ...) : no non-missing arguments to min; returning Inf

Best Answer

I have encountered a similar problem, and I solved it by transferring the class values ("status" in your case) into factor type. After using data$status=factor(data$status), newData prints as follows:

     looking risk every status
7          0    0     0      1
2          0    0     0      1
7.1        0    0     0      1
12         0    0     0      1
4          0    0     0      1
12.1       0    0     0      1
11         0    0     0      3
8         NA   NA    NA      3
9         NA   NA    NA      3
10        NA   NA    NA      3
111       NA   NA    NA      3
121       NA   NA    NA      3
13        NA   NA    NA      3

No errors!

Related Question