# One class SVM with caret in R using cross validation [closed]

I am using one class SVM to train and predict anomalies. I would like to train the model using cross validation in an easy way as I have done with a multiclass SVM with caret in R.

Now, I train the model doing:

svm.model<-svm(training,y=NULL,
type='one-classification',
nu=0.01,
gamma=0.002,
scale=TRUE,


However, I would like to use caret, apply cross validation and do something like:

train_control <- trainControl(method="repeatedcv", number=10, repeats=3)

svm.model <- train(classe~., data=training, method="svmRadial", trControl=train_control)


But instead of training a multiclass SVM I would like to use one class SVM.

Is there a way to do that in R with caret?

Simple option is not to use caret and just use the tune function from E1071.

svm_model <- tune(svm(training,y=NULL, type='one-classification', nu=0.01, gamma=0.002, scale=TRUE, kernel="radial", tunecontrol = tune.control(nrepeat = 3))


The default setting from tune is 10 fold CV. Using tune.control you can adjust this to repeat this as many times as you want. see ?tune.control for more options

If you want to use caret you will have to build your own model, because at the moment there is no one-classification model. But if you follow the steps on the caret page for using your own model you could adjust the first example.

Also look at these examples(example1, example2)

• Thanks for the answer @phiver. However, the tune command that you have written doesn't work. Looking at the documentation of the function I do not see a way to specify a type='one-classification' in the tune function nor in the tune.svm function. How would you do that? – user3791422 Aug 16 '15 at 15:24
• This link datalearner.wordpress.com/2013/10/24/… might help. Tuning should work with type specification. Just make sure you have enough samples – phiver Aug 16 '15 at 19:09
• I have 632 observations of 53 variables for training. With all the information that you have given me I execute: tune.svm(x = training,y=rep(TRUE,length(training[,1])), tunecontrol = tune.control(nrepeat = 3),scale=TRUE,kernel="radial",type="one-classification",nu=0.01). It seems to work and I get the value of gamma. However, I am not sure of having to do y=rep(TRUE,length(training[,1])), since this is a one-classification method, so it shouldn't be necessary. – user3791422 Aug 17 '15 at 7:37