Search code examples
rclassificationmulticoresvmnnet

Running a list of functions with different params with a single command


I am looking to test outcome of different regression/classification algorithms (i.e. svm, nnet, rpart, randomForest, naiveBayes, etc.) on the same data, to see which works better. But I need to have my code as short and clean as possible. To test all algorithms, I want to run them using a single mclapply() call of package multicore:

invisible(lapply(c("party","nnet","caret","klaR","randomForest","e1071","rpart",
                   "multicore"), require, character.only = T))
algorithms <- c(knn3, NaiveBayes, nnet, ctree, randomForest, svm, naiveBayes, rpart)
data(iris)
model <- mclapply(algorithms, function(alg) alg(Species ~ ., iris))

The problem is that some of the algorithms need extra parameters, i.e. nnet() needs parameter size to be set. For sure this can be fixed through several if,else commands, but is there any simpler solution?


Solution

  • One thing you could do is replace those in algorithms that require additional arguments with partial functions, e.g.

    algorithms <- c(knn3, ctree, function(...) nnet(..., size=2))