Error in svm forecast

I prepared a svm model. I would like to test it, but I ran into an error in the pred () function. For simplicity, here I divided the test data and training here in a non-random 70/30 split.

library(e1071) train <- mydata[1:9731, ] test <- mydata[(9731+1):13901, ] mysvm <- svm(formula = outcome ~ BW + GA, data = train, type = "C-classification", kernel = "linear", gamma = 1, cost = 2) predict(mysvm, newdata=test) 

The error message from pred () is:

 Error in names(ret2) <- rowns : 'names' attribute [4170] must be the same length as the vector [4106] 

The data head looks like ...

 > head(mydata) BW outcome GA 1 2.00 Survived 34 2 2.81 Survived 41 3 1.85 Survived 35 4 2.23 Survived 32 5 1.21 Survived 34 6 2.91 Survived 37 

This user had the same error message. The problem was that he / she did not use dataframes. This is not a problem in my case.

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

I am not sure why this error occurs or what it means. Tracking () and debugging (prediction) also did not help.

+4
source share
2 answers

It is difficult to provide a solution because you did not provide your data. However, I suppose you have 64 lines with NA values ​​for GA or BW in your test data. If you delete the lines with any NA, I think your prediction will be triggered:

 predict(mysvm, newdata = test[!rowSums(is.na(test)), ]) 

This, of course, means that you will not get any predictions for these lines. How you handle this is up to you (for example, you can attribute missing values ​​or something suitable for your specific use case).

+6
source

I use:

model <- svm (y = tags, x = data, ...)

pred <- forecasting (model, data)

and everything is in order.

-one
source

All Articles