4

I have trained an svm model. I would like to test it but I'm facing an error in the predict() function. For simplicity's sake, here I've split up the test and train data here in 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 is from predict() is:

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

The head of the data 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. Problem was that s/he wasn't using dataframes. This is not the problem in my case.

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

I'm not sure why this error happens or what it means. A traceback() and debug(predict) were also not helpful.

Community
  • 1
  • 1
Nirvan
  • 623
  • 7
  • 19

2 Answers2

6

It's hard to provide a solution because you have not provided your data. However, my guess is you have 64 rows with NA values for GA or BW in your test data. If you remove rows with any NAs, I think your prediction will run:

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

That of course means you won't get any predictions for those rows. How you handle that is up to you (e.g. you could impute the missing values or whatever is appropriate for your particular use case).

Ciarán Tobin
  • 7,306
  • 1
  • 29
  • 45
-1

I use:

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

   pred <- predict(model, data)

and everything is fine.

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Tan Dat
  • 79
  • 1
  • 3