0

How can I use tune function with support vector REGRESSION and not classification as when I tried to use "svr" as the first argument of the function it didn't work and I couldn't find a single example of tuning for regression. So this was the code I executed using e1071 package:

tuneSVR <- tune(svm, 
                train.x = train_[, -which(names(train) %in% c("Identifier", "Sales"))],
                train.y = train$Sales,
                data = train,
                ranges = list(epsilon = seq(0,1,0.1), cost = 2^(seq(0.5,8,.5))))

Is it wrong because my problem is a regression one? and is it normal for these lines of code to take long hours to be executed? and how can I calculate R Squared for svr?

user233531
  • 311
  • 1
  • 4
  • 17
  • Are you trying to get SVR working with a particular package, or are you asking about SVR parameter tuning in general? If the former, please make your example reproducible by including the relevant `library()` calls and an exemplar dataset. If the latter, take a look at `mlr` and `caret` packages. The corresponding tutorials show how to do parameter tuning for arbitrary predictors (including SVR). – Artem Sokolov Feb 26 '19 at 15:28
  • I am using e1071 package. – user233531 Feb 26 '19 at 15:29

1 Answers1

1

In e1071::svm(), the problem type is automatically inferred from the response variable, but can be overwritten using the type parameter. The tune() function and its wrapper for svm behave similarly:

library( e1071 )

# Option 1: using the wrapper
tuneSVR1 <- tune.svm( mtcars[,c("drat","wt")], mtcars$mpg )

# Option 2: using the base function
tuneSVR2 <- tune( svm, mpg~drat+wt, data=mtcars )

# In both cases, the methods correctly infer that the task is regression
tuneSVR2$best.model

# Call:
# best.tune(method = svm, train.x = mpg ~ drat + wt, data = mtcars)
# 
# Parameters:
#    SVM-Type:  eps-regression 
#  SVM-Kernel:  radial 
#        cost:  1 
#       gamma:  0.5 
#     epsilon:  0.1 
# 
# Number of Support Vectors:  28

Since R-squared between two vectors is just the square of their Pearson correlation, you can compute it directly as

ypred <- predict( tuneSVR2$best.model, mtcars[,c("drat","wt")] )
cor( ypred, mtcars$mpg )^2
# [1] 0.8325807
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
  • Great! I also had one more questions if you could answer: how can I calculate R Squared for svr (Because it didn't work the way I calculated using Metrics::rse(svr_tune_pred_v, valid$Sales))? – user233531 Feb 26 '19 at 15:47