as.kernelMatrix.Rdas.kernelMatrix in package kernlab can be used
to coerce the kernelMatrix class to matrix objects representing a
kernel matrix. These matrices can then be used with the kernelMatrix
interfaces which most of the functions in kernlab support.
# S4 method for class 'matrix'
as.kernelMatrix(x, center = FALSE)## Create toy data
x <- rbind(matrix(rnorm(10),,2),matrix(rnorm(10,mean=3),,2))
y <- matrix(c(rep(1,5),rep(-1,5)))
### Use as.kernelMatrix to label the cov. matrix as a kernel matrix
### which is eq. to using a linear kernel
K <- as.kernelMatrix(crossprod(t(x)))
K
#> An object of class "kernelMatrix"
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 3.3842064 -0.17169336 0.44346471 0.67373079 1.3019484 -3.5879004
#> [2,] -0.1716934 6.00142372 0.07397544 -1.44496762 -2.6620380 -9.4576549
#> [3,] 0.4434647 0.07397544 0.05966449 0.06557371 0.1288150 -0.6253417
#> [4,] 0.6737308 -1.44496762 0.06557371 0.46625016 0.8703322 1.5550631
#> [5,] 1.3019484 -2.66203795 0.12881504 0.87033219 1.6254322 2.7955050
#> [6,] -3.5879004 -9.45765488 -0.62534167 1.55506310 2.7955050 19.3099326
#> [7,] -4.0764347 -13.07376699 -0.74797216 2.31493451 4.1847609 25.6845000
#> [8,] -6.1051616 -4.21291235 -0.87282546 -0.15071211 -0.3895672 13.7476155
#> [9,] -2.9033928 -9.07661247 -0.52895084 1.59345612 2.8787364 17.9154308
#> [10,] -6.0280524 -3.62890306 -0.85325643 -0.27376785 -0.6145842 12.7201541
#> [,7] [,8] [,9] [,10]
#> [1,] -4.0764347 -6.1051616 -2.9033928 -6.0280524
#> [2,] -13.0737670 -4.2129124 -9.0766125 -3.6289031
#> [3,] -0.7479722 -0.8728255 -0.5289508 -0.8532564
#> [4,] 2.3149345 -0.1507121 1.5934561 -0.2737679
#> [5,] 4.1847609 -0.3895672 2.8787364 -0.6145842
#> [6,] 25.6845000 13.7476155 17.9154308 12.7201541
#> [7,] 34.3416327 17.3766934 23.9385818 15.9809068
#> [8,] 17.3766934 14.4270131 12.1989750 13.8442077
#> [9,] 23.9385818 12.1989750 16.6882269 11.2278997
#> [10,] 15.9809068 13.8442077 11.2278997 13.3208380
svp2 <- ksvm(K, y, type="C-svc")
svp2
#> Support Vector Machine object of class "ksvm"
#>
#> SV type: C-svc (classification)
#> parameter : cost C = 1
#>
#> [1] " Kernel matrix used as input."
#>
#> Number of Support Vectors : 3
#>
#> Objective Function Value : -0.1661
#> Training error : 0