我在R中使用kernlab软件包来构建SVM,以对某些数据进行分类。
SVM运行良好,因为它提供了不错的准确性的“预测”,但是我的输入变量列表比我想要的要大,而且我不确定不同变量的相对重要性。
我想实现一个遗传算法,以选择产生最佳训练/最适合的SVM的输入变量子集。
在尝试此GA实施时(可能是一个简短的psuedo示例),我想选择使用哪个R包时需要一些帮助。
我已经查看了大部分R GA / P软件包(RGP,genalg,subselect,GALGO),但是我在概念上很难解决如何将ksvm函数作为健身函数的一部分传递并输入我的变量数组作为人口池...?
在正确的方向上得到的任何帮助,想法或推动都将不胜感激。
谢谢
解决此问题的代码在稍后的EDIT中添加
# Prediction function to be used for backtesting
pred1pd = function(t) {
print(t)
##add section to select the best variable set from those available using GA
# evaluation function - selects the best indicators based on miminsied training error
mi.evaluate <- function(string=c()) {
tmp <- data[(t-lookback):t,-1]
x <- string
tmp <- tmp[,x==1]
tmp <- cbind(data[(t-lookback):t,1],tmp)
colnames(tmp)[1] <- "targets"
trainedmodel = ksvm(targets ~ ., data = tmp, type = ktype, kernel="rbfdot", kpar=list(sigma=0.1), C = C, prob.model = FALSE, cross = crossvalid)
result <- error(trainedmodel)
print(result)
}
## monitor tge GA process
monitor <- function(obj) {
minEval = min(obj$evaluations);
plot(obj, type="hist");
}
## pass out the GA results; size is set to be the number of potential indicators
gaResults <- rbga.bin(size=39, mutationChance=0.10, zeroToOneRatio=10, evalFunc=mi.evaluate, verbose=TRUE, monitorFunc=monitor, popSize=50, iters=3, elitism=10)
## now need to pull out the best chromosome and rebuild the data frame based on these results so that we can train the model
bestChro <- gaResults$population[1,]
newData <- data[,-1]
newData <- newData[,bestChro==1]
newData <- cbind(data[,1],newData)
colnames(newData)[1] <- "targets"
print(colnames(newData))
# Train model using new data set
model = trainSVM(newData[(t-lookback):t, ], ktype, C, crossvalid)
# Prediction
pred = as.numeric(as.vector(predict(model, newData[t+1, -1], type="response")))
# Print for user inspection
print(pred)
}