6

I'm testing the kernlab package in a regression problem. It seems it's a common issue to get 'Error in .local(object, ...) : test vector does not match model ! when passing the ksvm object to the predict function. However I just found answers to classification problems or custom kernels that are not applicable to my problem (I'm using a built-in one for regression). I'm running out of ideas here, my sample code is:

data <- matrix(rnorm(200*10),200,10)
tr <- data[1:150,]
ts <- data[151:200,]

mod <- ksvm(x = tr[,-1],
            y = tr[,1],
            kernel = "rbfdot", type = 'nu-svr',
            kpar = "automatic", C = 60, cross = 3)

pred <- predict(mod, 
                ts
                )
nopeva
  • 1,583
  • 5
  • 22
  • 38

2 Answers2

5

You forgot to remove the y variable in the test set, and so it fails because the number of predictors don't match. This will work:

predict(mod,ts[,-1])
nograpes
  • 18,623
  • 1
  • 44
  • 67
  • sometimes the function requires the response, other times doesn't... thanks! – nopeva Sep 24 '13 at 19:14
  • @eccehomo When does it require the response? – nograpes Sep 24 '13 at 19:16
  • I was referring to other packages like `caret`. If you try function `train`with `method = rf` for instance you should have to supply the response as well for predictions. – nopeva Sep 24 '13 at 19:21
1

You can use pred <- predict(mod, ts) if ts is a dataframe.

It would be

    data <- setNames(data.frame(matrix(rnorm(200*10),200,10)),
                     c("Y",paste("X", 1:9, sep = "")))
    tr <- data[1:150,]
    ts <- data[151:200,]

    mod <- ksvm(as.formula("Y ~ ."), data = tr,
        kernel = "rbfdot", type = 'nu-svr',
        kpar = "automatic", C = 60, cross = 3)

    pred <- predict(mod, ts)