## The following are extra R packages that need to be installed as any
## "normal" R package
library(e1071)         # where the svm is
library(DMwR)          # package containing a framework for CV exps
library(randomForest)  # randomForest
library(earth)         # MARS reimplementation

## This package is not available in the standard R package repository
## and thus needs to be installed through the package file we provide
## at the place were you got this script
library(uba)           # Ribeiro's utility-based evaluation framework


source('cvAuxs.R')              # auxiliary functions for the CV experiments
source('sampling4regression.R') # SmoteR and Undersampling code


##############################################################
# Load the used data sets
# ============================================================

load('allDataSets.Rdata')  # loads  DSs object containing all data sets


##############################################################
# The learning systems and their variants
# ============================================================

VARS <- list()

VARS$svm   <- list(cost=c(10,150,300),gamma=c(0.01,0.001))

VARS$rf    <- list(mtry=c(5,7),ntree=c(500,750,1500))

VARS$earth <- list(nk=c(10,17),degree=c(1,2),thresh=c(0.01,0.001))


CVsetts <- cvSettings(3,10,1234) # the CV experimental settings 3x10CV

TODO <- c('earth','svm','rf')
todoDSs <- DSs

# main loop
for(d in seq(along=todoDSs)) {
  for(td in TODO) {
    assign(td,
           try(
               experimentalComparison(
                                  DSs[d],         
                                  c(
                                    do.call('variants',
                                            c(list('allData',learner=td),VARS[[td]],
                                              varsRootName=paste('allData',td,sep='.'))),
                                    do.call('variants',
                                            c(list('underSampl',learner=td,
                                                   thr.rel=0.75,perc.under=c(200,300)),
                                              VARS[[td]],
                                              varsRootName=paste('underSampl',td,sep='.'))),
                                    do.call('variants',
                                            c(list('smote',learner=td,
                                                   thr.rel=0.75,perc.over=c(200,500),
                                                   k=5,perc.under=c(200,300)),
                                              VARS[[td]],
                                              varsRootName=paste('smote',td,sep='.')))
                                    ),
                                  CVsetts)
               )
           )

    # save the results
    if (class(get(td)) != 'try-error') save(list=td,file=paste(names(DSs)[d],td,'Rdata',sep='.'))
  }
}