I'm using the kernlab package in R to build an SVM for classifying some data.
The SVM is working nicely in that it provides 'predictions' of a decent accuracy, however my list of input variables is larger than I would like and I am unsure as to the relative importance of the different variables.
I'd like to implement a genetic-algorithm to select the sub-set of input variables that produces the best-trained/fittest SVM.
I'd like some help with choosing which R package to use when attempting this GA implementation (and possibly a brief psuedo-example).
I've looked a most of the R GA/P packages out there (RGP, genalg, subselect, GALGO), but I'm struggling conceptually to see how I would pass in my ksvm function as part of the fitness function and input my variable array as the population pool...?
Any help, thoughts, or nudges in the right direction gratefully received.
Thanks
code that solves this added below in a later 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)
}