How to perform 10 fold cross validation with LibSVM in R?

Sigvard picture Sigvard · Nov 12, 2012 · Viewed 8.4k times · Source

I know that in MatLab this is really easy ('-v 10').

But I need to do it in R. I did find one comment about adding cross = 10 as parameter would do it. But this is not confirmed in the help file so I am sceptical about it.

svm(Outcome ~. , data= source, cost = 100, gamma =1, cross=10)

Any examples of a successful SVM script for R would also be appreciated as I am still running into some dead ends?

Edit: I forgot to mention outside of the tags that I use the libsvm package for this.

Answer

hlfernandez picture hlfernandez · Nov 13, 2012

I am also trying to perform a 10 fold cross validation. I think that using tune is not the right way in order to perform it, since this function is used to optimize the parameters, but not to train and test the model.

I have the following code to perform a Leave-One-Out cross validation. Suppose that dataset is a data.frame with your data stored. In each LOO step, the observed vs. predicted matrix is added, so that at the end, result contains the global observed vs. predicted matrix.

#LOOValidation
for (i in 1:length(dataset)){
    fit = svm(classes ~ ., data=dataset[-i,], type='C-classification', kernel='linear')
    pred = predict(fit, dataset[i,])
    result <- result + table(true=dataset[i,]$classes, pred=pred);
}
classAgreement(result)

So in order to perform a 10-fold cross validation, I guess we should manually partition the dataset, and use the folds to train and test the model.

for (i in 1:10)
    train <- getFoldTrainSet(dataset, i)
    test <- getFoldTestSet(dataset,i)
    fit = svm(classes ~ ., train, type='C-classification', kernel='linear')
    pred = predict(fit, test)
    results <- c(results,table(true=test$classes, pred=pred));

}
# compute mean accuracies and kappas ussing results, which store the result of each fold

I hope this help you.