1 defaultLoss_classif <- function(y1, y2) {
3 # Standard case: "hard" classification
6 # "Soft" classification: predict() outputs a probability matrix
7 # In this case "target" could be in matrix form.
9 mean(rowSums(abs(y1 - y2)))
11 # Or not: y2 is a "factor".
12 y2 <- as.character(y2)
13 # NOTE: the user should provide target in matrix form because
14 # matching y2 with columns is rather inefficient!
17 for (idx in seq_along(names))
18 positions[[ names[idx] ]] <- idx
21 function(idx) sum(abs(y1[idx,] - positions[[ y2[idx] ]])),
27 defaultLoss_regress <- function(y1, y2) {
31 # TODO: allow strings like "MSE", "abs" etc
32 checkLoss <- function(loss, task) {
33 if (!is.null(loss) && !is.function(loss))
34 stop("loss: function(y1, y2) --> Real")
36 loss <- if (task == "classification") {
45 checkCV <- function(CV) {
47 CV <- list(type="MC", V=10, test_size=0.2, shuffle=TRUE)
50 stop("CV: list of type('MC'|'vfold'), V(integer, [test_size, shuffle]")
51 if (is.null(CV$type)) {
52 warning("CV$type not provided: set to MC")
56 warning("CV$V not provided: set to 10")
59 if (CV$type == "MC" && is.null(CV$test_size))
61 if (CV$type == "vfold" && is.null(CV$shuffle))
67 checkDaTa <- function(data, target) {
68 if (!is.data.frame(data) && !is.matrix(data))
69 stop("data: data.frame or matrix")
70 if (is.data.frame(target) || is.matrix(target)) {
71 if (!is.numeric(target))
72 stop("multi-columns target must be a probability matrix")
73 if (nrow(target) != nrow(data) || ncol(target) == 1)
74 stop("target probability matrix does not match data size")
76 else if (!is.numeric(target) && !is.factor(target) && !is.character(target))
77 stop("target: numeric, factor or character vector")
80 checkTask <- function(task, target) {
82 task <- match.arg(task, c("classification", "regression"))
83 task <- ifelse(is.numeric(target), "regression", "classification")
86 checkModPar <- function(gmodel, params) {
87 if (is.character(gmodel))
88 gmodel <- match.arg(gmodel, c("knn", "ppr", "rf", "tree"))
89 else if (!is.null(gmodel) && !is.function(gmodel))
90 stop("gmodel: function(dataHO, targetHO, param) --> function(X) --> y")
91 if (is.numeric(params) || is.character(params))
92 params <- as.list(params)
93 if (!is.list(params) && !is.null(params))
94 stop("params: numerical, character, or list (passed to model)")
95 if (is.function(gmodel) && !is.list(params))
96 stop("params must be provided when using a custom model")
97 if (is.list(params) && is.null(gmodel))
98 stop("model (or family) must be provided when using custom params")
99 list(gmodel=gmodel, params=params)