Reputation: 3607
I'm new to R, and trying to fit a model using kernlab
with some data that I just loaded in. However, when I try and load it in I get the error message in the subject line. I assume this means the data type of X
and y
are not compatible.
Here's some sample code:
data = read.delim("my-sample-file.txt")
model = ksvm(data[, 1:10], data[, 11])
When I call data[, 11]
I just the raw values in the column returned to me, and I notice the typeof
function returns the value integer
, which I found strange. I am not using any additional packages, just trying to get something basic to work.
Thank you.
Upvotes: 3
Views: 1105
Reputation: 263421
Reading the help page for ksvm
shows that the Usage sections says that using x
and y
as the input parameters requires a matrix for x
, so this should be more successful (assuming that the data object has all numeric columns. You really should be looking at your data carefully before reaching for analysis tools.):
model = ksvm( x = data.matrix(data[, 1:10]), y=data[, 11]) )
Note that you can get exactly the same error with the iris data.frame:
ksvm(x=iris[-5], y=iris$Species)
Error in .local(x, ...) : x and y don't match.
Whereas converting to matrix results in success:
ksvm(x=data.matrix(iris[-5]), y=iris$Species)
Support Vector Machine object of class "ksvm"
SV type: C-svc (classification)
parameter : cost C = 1
Gaussian Radial Basis kernel function.
Hyperparameter : sigma = 0.484488222038106
Number of Support Vectors : 57
Objective Function Value : -3.7021 -3.8304 -21.7405
Training error : 0.026667
Morals of the story: Pay attention to the 'Usage' section to give guidance on the different forms that generic functions may take. And always assume that the authors of the help page are excruciatingly correct in their description of the arguments in the 'Arguments' sections. If they say matrix, don't assume they mean “sort of like a matrix.” (But if you mutter under your breath that this seems like something that should have been anticipated and a more informative error message emitted, I would not disagree.)
Upvotes: 4