0

I want to perform sample classification using the ksvm function from the kernlab library.

library(kernlab)

# PCA analysis on the first 3 component vectors
pca       <- prcomp(t(top.ranked.genes))
dat.loadings <- pca$x[, 1:3] 

# Sample classification

svp <- ksvm(dat.loadings, label, type="C-svc", scaled=T, kernel="rbfdot", kpar="automatic", prob.model=F, class.weights=NULL, fit=T, shrinking=T)

Error in if ((type(ret) == "C-svc" || type(ret) == "nu-svc" || type(ret) == : missing value where TRUE/FALSE needed In addition: Warning message: In .local(x, ...) : NAs introduced by coercion

    > dput(top.ranked.genes[1:3,1:3])
    structure(c(4120.8, 1073.2, 1434.3, 3785.7, 1305.3, 1550.5, 3326.5, 
    1163.6, 1017.7), .Dim = c(3L, 3L), .Dimnames = list(c("221918_at", 
    "201554_x_at", "214722_at"), c("NB_GSM97800", "NB_GSM97803", 
    "NB_GSM97804")))

    

> dput(dat.loadings[1:3,1:3])
    structure(c(-158664.494929915, -148977.612734589, -163264.320664849, 
    -3583.353411796, -14921.765919203, -20224.318452977, 61652.7194473044, 
    18971.6967789661, 27273.153856793), .Dim = c(3L, 3L), .Dimnames = list(
        c("NB_GSM97800", "NB_GSM97803", "NB_GSM97804"), c("PC1", 
        "PC2", "PC3")))
melil
  • 81
  • 8

1 Answers1

0

Seems like label is not a factor as required for classification. From the documentation of ksvm:

y Can be either a factor (for classification tasks) or a numeric vector (for regression). [...] Depending on whether y is a factor or not, the default setting for type is C-svc or eps-svr.

Try label <- as.factor(label).

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22
  • When I use `label <- as.factor(label)`, I get `Error in indexes[[j]] : subscript out of bounds` – melil Aug 12 '21 at 08:34
  • `> dput(label) structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Glioma", class = "factor")` – melil Aug 12 '21 at 08:36
  • Well, `label` has just one level. Can you try running `ksvm` with `label <- factor(c("Glioma", "else", "else"))` and your example data (`dat.loadings[1:3, 1:3]`) ? – Martin C. Arnold Aug 12 '21 at 08:55