我一直没有尝试使用rstudio中的粒子群优化算法来建立目标函数,以找到支持向量回归模型的最佳参数。
我正在使用R版本3.6.0。 SVR模型具有2个预测变量(x1和X2)和一个响应变量(y)。首先使用插入符号包装的3倍交叉验证对它进行了训练。然后,已获取参数sigma的值。然后使用pso软件包尝试优化其他三个参数(C,nu和epsilon),但是遇到一个错误,提示 eval(替代(expr),数据,enclos = parent.frame())中的错误: 数字“ envir”参数,长度不为一
library(kernlab)
library(caret)
library(pso)
#data
TFCtrain <- data.frame(x1 = trainSVR[,5], x2= trainSVR[,6], y = trainSVR[,2])
TFCtrain
x1 x2 y
1 12.26775 880.0185 2.91
2 12.71001 825.5407 2.99
3 13.16302 847.2958 3.01
4 13.62273 810.8330 3.06
5 14.08361 590.3001 3.12
6 14.54082 756.5011 3.29
7 14.99525 809.5387 3.41
8 15.44599 758.9119 3.59
9 15.88455 793.9811 3.58
10 16.30023 759.2922 4.34
11 16.68656 642.2547 4.33
12 17.04015 656.8346 4.30
13 17.36652 710.9612 4.33
14 17.67936 865.7896 4.11
15 17.99774 919.8068 5.11
16 18.33630 931.7543 5.04
17 18.69944 951.9479 5.40
18 19.08594 1065.8964 5.46
19 19.49799 1242.4310 5.56
20 19.93637 1217.7492 5.28
21 20.40133 1219.7491 5.71
## cross validation
set.seed(1234)
ControlParameters <- trainControl(method = "cv",
number = 3
)
grid_search <- train(y~x1+x2,
data = TFCtrain,
method = "svmRadial", tunelength=8, metric="RMSE",
trControl = ControlParameters,
preProcess = c('center', 'scale')
grid_search
getTrainPerf(grid_search)
## Define objective function
sphere <- function(TFCtrain, par){with(TFCtrain,ksvm(y~x1+x2,data=TFCtrain,kernel="rbfdot",
kpar=list(sigma=0.5317279),C= par[1], nu= par[2], epsilon = par[3] , cross=3))}
## calculate the optimum solution using Particle Swarm Optimization Algorithm
pso_res <- psoptim(par = c(0.5,0.1,0.05), fn = sphere,
lower = 0, upper = 1, control = list(fnscale = 1, maxit = 60, s = 40, w = 1.4,
c.p = 1.5, c.g = 1.5))
pso_res
运行此命令后,出现以下错误: eval(替代(expr),数据,enclos = parent.frame())中的错误: 数字“ envir”参数,长度不为一
我的目标函数肯定有问题,但仍然无法解决。我想为SVR模型找到参数C,nu和epsilon的最佳值。 任何帮助将不胜感激。非常感谢