1

I'm trying to get probabilities for each observation in my test dataset test_k. I'm using a precomputed kernel matrix and C classification. "C-svc" should allow me to get a probability model. However, I get the following error code: Error: $ operator is invalid for atomic vectors. Why? Why does the source code require oldco != newncols? Isn't the object in the source code my ksvm.model?

  ksvm.model
  # the kernel parameter is set to "matrix", calling the kernel matrix interface.
 
  test_k <- as.kernelMatrix(k_mat[(train_row + 1):(train_row + test_row), SVindex(ksvm.model), drop = F])
  # SVindex(ksvm.model) returns the indexes of the support vectors. This ensures that test_k only has the rows of the test data and the columns that correspond to the support vectors.

predict(ksvm.model, test_k, type='probabilities') 

### This is the error
Error: $ operator is invalid for atomic vectors 


### This is the source code of kernlab 
##**************************************************************#
## predict for matrix, data.frame input

setMethod("predict", signature(object = "ksvm"),
function (object, newdata, type = "response", coupler = "minpair")
{
  type <- match.arg(type,c("response","probabilities","votes","decision"))
  if (missing(newdata) && type=="response" & !is.null(fitted(object)))
    return(fitted(object))
  else if(missing(newdata))
    stop("Missing data !")

 if(!is(newdata,"list")){ 
  if (!is.null(terms(object)) & !is(newdata,"kernelMatrix"))
    {
      if(!is.matrix(newdata))
        newdata <- model.matrix(delete.response(terms(object)), as.data.frame(newdata), na.action = n.action(object))
    }
  else
    newdata  <- if (is.vector(newdata)) t(t(newdata)) else as.matrix(newdata)

newnrows <- nrow(newdata)
    newncols <- ncol(newdata)
    if(!is(newdata,"kernelMatrix") && !is.null(xmatrix(object))){
      if(is(xmatrix(object),"list") && is(xmatrix(object)[[1]],"matrix")) oldco <- ncol(xmatrix(object)[[1]])
      if(is(xmatrix(object),"matrix")) oldco <- ncol(xmatrix(object))
      if (oldco != newncols) stop ("test vector does not match model !")
`
<https://github.com/cran/kernlab/blob/master/R/ksvm.R> ```

 

Here it is working? What is he doing there iris[c(3, 10, 56, 68,107, 120), -5]?, Why is this required?

irismodel <- ksvm(Species ~ ., data = iris,type = "C-bsvc", kernel = "rbfdot",kpar = list(sigma = 0.1), C = 10, prob.model = TRUE) 

predict(irismodel, iris[c(3, 10, 56, 68,107, 120), -5], type = "probabilities")
 <https://www.researchgate.net/publication/5142924_Support_Vector_Machines_in_R>
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
p.bdns
  • 11
  • 1

0 Answers0