1 #' @title R6 class representing a (generic) model.
4 #' "Model" class, containing a (generic) learning function, which from
5 #' data + target [+ params] returns a prediction function X --> y.
6 #' Parameters for cross-validation are either provided or estimated.
7 #' Model family can be chosen among "rf", "tree", "ppr" and "knn" for now.
10 Model <- R6::R6Class("Model",
12 #' @field nmodels Number of parameters (= number of [predictive] models)
14 #' @description Create a new generic model.
15 #' @param data Matrix or data.frame
16 #' @param target Vector of targets (generally numeric or factor)
17 #' @param task "regression" or "classification"
18 #' @param gmodel Generic model returning a predictive function; chosen
19 #' automatically given data and target nature if not provided.
20 #' @param params List of parameters for cross-validation (each defining a model)
21 initialize = function(data, target, task, gmodel = NA, params = NA) {
23 # (Generic) model not provided
24 all_numeric <- is.numeric(as.matrix(data))
26 # At least one non-numeric column: use random forests or trees
27 # TODO: 4 = arbitrary magic number...
28 gmodel = ifelse(ncol(data) >= 4, "rf", "tree")
31 gmodel = ifelse(task == "regression", "ppr", "knn")
34 # Here, gmodel is a string (= its family),
35 # because a custom model must be given with its parameters.
36 params <- as.list(private$getParams(gmodel, data, target))
37 private$params <- params
38 if (is.character(gmodel))
39 gmodel <- private$getGmodel(gmodel, task)
40 private$gmodel <- gmodel
41 self$nmodels <- length(private$params)
44 #' Returns the model at index "index", trained on dataHO/targetHO.
45 #' index is between 1 and self$nmodels.
46 #' @param dataHO Matrix or data.frame
47 #' @param targetHO Vector of targets (generally numeric or factor)
48 #' @param index Index of the model in 1...nmodels
49 get = function(dataHO, targetHO, index) {
50 private$gmodel(dataHO, targetHO, private$params[[index]])
54 # No need to expose model or parameters list
57 # Main function: given a family, return a generic model, which in turn
58 # will output a predictive model from data + target + params.
59 getGmodel = function(family, task) {
60 if (family == "tree") {
61 function(dataHO, targetHO, param) {
63 method <- ifelse(task == "classification", "class", "anova")
64 df <- data.frame(cbind(dataHO, target=targetHO))
65 model <- rpart(target ~ ., df, method=method, control=list(cp=param))
66 function(X) predict(model, X)
69 else if (family == "rf") {
70 function(dataHO, targetHO, param) {
72 if (task == "classification" && !is.factor(targetHO))
73 targetHO <- as.factor(targetHO)
74 model <- randomForest::randomForest(dataHO, targetHO, mtry=param)
75 function(X) predict(model, X)
78 else if (family == "ppr") {
79 function(dataHO, targetHO, param) {
80 model <- stats::ppr(dataHO, targetHO, nterms=param)
81 function(X) predict(model, X)
84 else if (family == "knn") {
85 function(dataHO, targetHO, param) {
87 function(X) class::knn(dataHO, X, cl=targetHO, k=param)
91 # Return a default list of parameters, given a gmodel family
92 getParams = function(family, data, target) {
93 if (family == "tree") {
94 # Run rpart once to obtain a CV grid for parameter cp
96 df <- data.frame(cbind(data, target=target))
106 r <- rpart(target ~ ., df, method="class", control=ctrl)
107 cps <- r$cptable[-1,1]
108 if (length(cps) <= 11)
110 step <- (length(cps) - 1) / 10
111 cps[unique(round(seq(1, length(cps), step)))]
113 else if (family == "rf") {
115 # Use caret package to obtain the CV grid of mtry values
117 caret::var_seq(p, classification = (task == "classificaton"),
120 else if (family == "ppr")
121 # This is nterms in ppr() function
123 else if (family == "knn") {
125 # Choose ~10 NN values
126 K <- length(unique(target))
130 step <- (2*sqrt_n - 1) / 10
131 grid <- unique(round(seq(1, 2*sqrt_n, step)))
133 # Common binary classification case: odd number of neighbors
135 if (grid[i] %% 2 == 0)
136 grid[i] <- grid[i] + 1 #arbitrary choice