# Tuning parameters of SVM in tune function

Cross Validated Asked by siegfried on September 5, 2020

I would like to fit a radial SVM onto the data. The tuning parameters cost and gamma are chosen by CV with the tune function. However the summary of the optimal model does not show the optimal gamma value.

The summary where gamma is not shown:

> svmult= svmcv$best.model > svmult Call: best.tune(method = svm, train.x = type ~ ., data = wheat[tr, ], ranges = list(cost = seq(0.001, 1, length.out = 50)), kernel = "radial", gamma = c(0.1, 1, 2, 3, 4, 5)) Parameters: SVM-Type: C-classification SVM-Kernel: radial cost: 0.2864286 Number of Support Vectors: 171  Meanwhile when I try to plot the data and the SVM hyperplanes, I received this error: Error in plot.svm(svmult, wheat) : missing formula.. Please help me with the CV and plotting. Thanks in advance! The data: > head(wheat) class density hardness size weight moisture type 1 hrw 1.313662 73.32533 2.19946 26.3070 6.97526 Healthy 2 srw 1.233610 31.06556 1.84809 28.4400 12.51565 Healthy 3 srw 1.203221 -23.43620 3.15953 40.0530 12.36660 Sprout 4 hrw 1.279238 70.97478 3.09405 25.0035 7.53971 Healthy 5 srw 1.341604 60.25526 2.14827 30.0990 12.22773 Healthy 6 hrw 1.384803 53.07190 2.42810 26.5440 7.22400 Healthy > dim(wheat) [1] 275 7  rm(list=ls()) wheat= read.csv('Wheat.csv') wheat= na.omit(wheat) head(wheat) wheat$class= factor(wheat$class) attach(wheat) library(e1071) x= model.matrix(type~., data= wheat)[,-1] y= wheat$type
tr= sample(1:nrow(wheat), floor(nrow(wheat)*2/3))
trainX= x[tr,]
testX= x[-tr,]
trainY= y[tr]
testY= y[-tr]
svmcv= tune(svm, type~., data = wheat[tr,], kernel= 'radial', ranges = list(cost=seq(0.001, 1, length.out = 50)), gamma= c(0.1,1,2,3,4,5))
summary(svmcv)
svmult= svmcv\$best.model
plot(svmult, wheat)


I believe gamma should be put inside the ranges list. The same goes for kernel if you want to tune for different kernels.

As for the plotting problem, you are missing a formula for the plot, and since it is a 2D-plot, you probably need to specify a slice for the other variables so that it is visualizable in 2D. Try something like this:

plot(svmModel, data=wheat, type~Variable2ChosenAsFree,
slice=list(
Variable3ChosenAsConstant=3,
Variable4ChosenAsConstant=8
)


Where you need to provide the correct variable names instead of my made up variable names.

Answered by dekuShrub on September 5, 2020

## Related Questions

### Compare the results of two canonical correlation analyses (CCA)

1  Asked on February 18, 2021 by forlooper

### Why doesn’t the optimizer just look for stationary points of the loss function?

1  Asked on February 18, 2021 by borut-flis

### Statistical analysis for comparing expertise levels between 3 groups

0  Asked on February 17, 2021 by kaaren0111

### Find the prior distribution for the natural parameter of an exponential family

1  Asked on February 17, 2021 by xxtensionxx

### Using decision tree for unsupervised discretization?

1  Asked on February 17, 2021 by aflatoun

### How to conduct a multilevel model/regression for panel data in Python?

1  Asked on February 17, 2021 by exlo

### How to choose resample size when drawing without replacement?

1  Asked on February 17, 2021

### Two-way repeated measures ANOVA vs mixed ANOVA

1  Asked on February 16, 2021 by xe-m

### What is the adjusted R-squared formula in lm in R and how should it be interpreted?

2  Asked on February 16, 2021 by user1272262

### Linear model selection – Subset, Forward

0  Asked on February 16, 2021 by davud-mursalov

### What model to use for analyzing age frequency data? issues with linear model in R

1  Asked on February 16, 2021 by johnny5ish

### Dimensionality reduction of a large covariance matrix

0  Asked on February 15, 2021 by wilmer-e-henao

### How to estimate variance of classifier on test set?

2  Asked on February 15, 2021 by pterojacktyl

### Churn prediction for customers with limited data

0  Asked on February 14, 2021 by ahmet-turul-bayrak

### multiple linear regression vs polynomial regression models

0  Asked on February 14, 2021 by gracetam

### Multiple Regression study design – questionnaires

1  Asked on February 14, 2021

### Which package works for mediation analysis in R when variables are categorical?

0  Asked on February 14, 2021 by fouzia-farooq

### Compare two samples with many zeros

4  Asked on February 14, 2021

### Non-statistically significant effect of the instrument in the reduced form of the 2SLS

1  Asked on February 14, 2021 by fuca26

### What are the different types of averages?

4  Asked on February 14, 2021 by gpuguy

Get help from others!