当我尝试使用“ svr”作为函数的第一个参数时,如何使用带有支持向量REGRESSION而不是分类的调整函数,它不起作用,并且找不到单个调整回归的示例。这就是我使用e1071软件包执行的代码:
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))))
是不是因为我的问题是回归问题?这些代码行要花很长时间才能正常执行吗?以及如何计算svr的R平方?
答案 0 :(得分:1)
在e1071::svm()
中,可以从响应变量中自动推断出问题类型,但是可以使用type
参数将其覆盖。 tune()
函数及其用于svm的包装的行为类似:
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,您可以直接将其计算为
ypred <- predict( tuneSVR2$best.model, mtcars[,c("drat","wt")] )
cor( ypred, mtcars$mpg )^2
# [1] 0.8325807