“SMOTE” function in R – Error in T[i, ] : subscript out of bounds

Error message that came up while using SMOTE function from DMwR package in R is captured below, along with its reason and fix.

Error message:
Error in T[i, ] : 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 max; 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
7: In FUN(newX[, i], …) : no non-missing arguments to min; returning Inf
8: In FUN(newX[, i], …) : no non-missing arguments to min; returning Inf

Reason:
This error occurs when the target variable you use for SMOTE function is of INT data type. SMOTE can only work with factor target variable.

R line of code that gives error is

> Traindata_SMOTE = SMOTE(Target ~ .,Traindata[,-1], 
perc.over = 300, perc.under = 500)

STR command reveals that the target variable is INT.

> str(Traindata)
'data.frame':  4994 obs. of  6 variables: 
$ Casenum                             : int  1 2 3 4 5 6 7 8 9 10 ... 
$ Target                              : int  1 0 0 0 0 0 0 0 0 0 ... 
$ ..

Fix:
Simply convert the target variable to “factor” as below

> Traindata$Target = as.factor(Traindata$Target)

Rerun SMOTE and it will go through fine.

Advertisements

0 thoughts on ““SMOTE” function in R – Error in T[i, ] : subscript out of bounds

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s