1 standardCV_core <- function(data, target, task = NULL, gmodel = NULL, params = NULL,
2 loss = NULL, CV = list(type = "MC", V = 10, test_size = 0.2, shuffle = TRUE)
5 task = match.arg(task, c("classification", "regression"))
6 if (is.character(gmodel))
7 gmodel <- match.arg(gmodel, c("knn", "ppr", "rf", "tree"))
8 if (is.numeric(params) || is.character(params))
9 params <- as.list(params)
11 if (is.numeric(target))
14 task = "classification"
18 loss <- function(y1, y2) {
19 if (task == "classification") {
23 if (!is.null(dim(y2)))
24 mean(rowSums(abs(y1 - y2)))
26 y2 <- as.character(y2)
29 for (idx in seq_along(names))
30 positions[[ names[idx] ]] <- idx
33 function(idx) sum(abs(y1[idx,] - positions[[ y2[idx] ]])),
45 if (CV$type == "vfold" && CV$shuffle)
46 shuffle_inds <- sample(n, n)
47 get_testIndices <- function(v, shuffle_inds) {
48 if (CV$type == "vfold") {
49 first_index = round((v-1) * n / CV$V) + 1
50 last_index = round(v * n / CV$V)
51 test_indices = first_index:last_index
52 if (!is.null(shuffle_inds))
53 test_indices <- shuffle_inds[test_indices]
56 test_indices = sample(n, round(n * CV$test_size))
59 list_testinds <- list()
60 for (v in seq_len(CV$V))
61 list_testinds[[v]] <- get_testIndices(v, shuffle_inds)
63 gmodel <- agghoo::Model$new(data, target, task, gmodel, params)
66 for (p in seq_len(gmodel$nmodels)) {
68 for (v in seq_len(CV$V)) {
69 testIdx <- list_testinds[[v]]
70 dataHO <- data[-testIdx,]
71 testX <- data[testIdx,]
72 targetHO <- target[-testIdx]
73 testY <- target[testIdx]
74 if (!is.matrix(dataHO) && !is.data.frame(dataHO))
75 dataHO <- as.matrix(dataHO)
76 if (!is.matrix(testX) && !is.data.frame(testX))
77 testX <- as.matrix(testX)
78 model_pred <- gmodel$get(dataHO, targetHO, p)
79 prediction <- model_pred(testX)
80 error <- error + loss(prediction, testY)
82 if (error <= best_error) {
83 newModel <- list(model=model_pred, param=gmodel$getParam(p))
84 if (error == best_error)
85 best_model[[length(best_model)+1]] <- newModel
87 best_model <- list(newModel)
92 best_model[[ sample(length(best_model), 1) ]]
95 standardCV_run <- function(
96 dataTrain, dataTest, targetTrain, targetTest, verbose, CV, floss, ...
98 s <- standardCV_core(dataTrain, targetTrain, ...)
100 print(paste( "Parameter:", s$param ))
102 err_s <- floss(ps, targetTest)
104 print(paste("error CV:", err_s))
105 invisible(c(errors, err_s))
108 agghoo_run <- function(
109 dataTrain, dataTest, targetTrain, targetTest, verbose, CV, floss, ...
111 a <- agghoo(dataTrain, targetTrain, ...)
115 print(unlist(a$getParams()))
117 pa <- a$predict(dataTest)
118 err <- floss(pa, targetTest)
120 print(paste("error agghoo:", err))
123 # ... arguments passed to agghoo or any other procedure
124 compareTo <- function(
125 data, target, rseed=-1, verbose=TRUE, floss=NULL,
126 CV = list(type = "MC",
135 test_indices <- sample( n, round(n / ifelse(n >= 500, 10, 5)) )
136 trainData <- as.matrix(data[-test_indices,])
137 trainTarget <- target[-test_indices]
138 testData <- as.matrix(data[test_indices,])
139 testTarget <- target[test_indices]
141 # Set error function to be used on model outputs (not in core method)
142 if (is.null(floss)) {
143 floss <- function(y1, y2) {
144 ifelse(task == "classification", mean(y1 != y2), mean(abs(y1 - y2)))
148 # Run (and compare) all methods:
149 runOne <- function(o) {
150 o(dataTrain, dataTest, targetTrain, targetTest, verbose, CV, floss, ...)
152 if (is.list(method_s))
153 errors <- sapply(method_s, runOne)
154 else if (is.function(method_s))
155 errors <- runOne(method_s)
161 # Run compareTo N times in parallel
162 compareMulti <- function(
163 data, target, N = 100, nc = NA,
164 CV = list(type = "MC",
171 nc <- parallel::detectCores()
172 compareOne <- function(n) {
174 compareTo(data, target, n, verbose=FALSE, CV, method_s, ...)
176 errors <- if (nc >= 2) {
178 parallel::mclapply(1:N, compareOne, mc.cores = nc)
180 lapply(1:N, compareOne)
183 Reduce('+', errors) / N