3 standardCV <- function(data, target, task = NULL, gmodel = NULL, params = NULL,
4 loss = NULL, CV = list(type = "MC", V = 10, test_size = 0.2, shuffle = TRUE)
7 task = match.arg(task, c("classification", "regression"))
8 if (is.character(gmodel))
9 gmodel <- match.arg(gmodel, c("knn", "ppr", "rf", "tree"))
10 if (is.numeric(params) || is.character(params))
11 params <- as.list(params)
13 if (is.numeric(target))
16 task = "classification"
20 loss <- function(y1, y2) {
21 if (task == "classification") {
25 if (!is.null(dim(y2)))
26 mean(rowSums(abs(y1 - y2)))
28 y2 <- as.character(y2)
31 for (idx in seq_along(names))
32 positions[[ names[idx] ]] <- idx
35 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 compareToCV <- function(df, t_idx, task=NULL, rseed=-1, verbose=TRUE) {
99 task <- ifelse(is.numeric(df[,t_idx]), "regression", "classification")
101 test_indices <- sample( n, round(n / ifelse(n >= 500, 10, 5)) )
102 a <- agghoo(df[-test_indices,-t_idx], df[-test_indices,t_idx], task)
106 print(unlist(a$getParams()))
108 pa <- a$predict(df[test_indices,-t_idx])
109 err_a <- ifelse(task == "classification",
110 mean(pa != df[test_indices,t_idx]),
111 mean(abs(pa - df[test_indices,t_idx])))
113 print(paste("error agghoo:", err_a))
114 # Compare with standard cross-validation:
115 s <- standardCV(df[-test_indices,-t_idx], df[-test_indices,t_idx], task)
117 print(paste( "Parameter:", s$param ))
118 ps <- s$model(df[test_indices,-t_idx])
119 err_s <- ifelse(task == "classification",
120 mean(ps != df[test_indices,t_idx]),
121 mean(abs(ps - df[test_indices,t_idx])))
123 print(paste("error CV:", err_s))
128 compareMulti <- function(df, t_idx, task = NULL, N = 100, nc = NA) {
131 errors <- mclapply(1:N,
133 compareToCV(df, t_idx, task, n, verbose=FALSE) },
135 print("error agghoo vs. cross-validation:")
136 Reduce('+', errors) / N