Refactoring: separate standard CV from agghoo + some bug fixes
[agghoo.git] / R / R6_Model.R
CommitLineData
c5946158
BA
1#' @title R6 class representing a (generic) model.
2#'
3#' @description
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.
8#'
d9a139b5
BA
9#' @importFrom FNN knn.reg
10#' @importFrom class knn
11#' @importFrom stats ppr
12#' @importFrom randomForest randomForest
13#' @importFrom rpart rpart
14#' @importFrom caret var_seq
15#'
c5946158
BA
16#' @export
17Model <- R6::R6Class("Model",
18 public = list(
19 #' @field nmodels Number of parameters (= number of [predictive] models)
20 nmodels = NA,
21 #' @description Create a new generic model.
22 #' @param data Matrix or data.frame
23 #' @param target Vector of targets (generally numeric or factor)
24 #' @param task "regression" or "classification"
25 #' @param gmodel Generic model returning a predictive function; chosen
26 #' automatically given data and target nature if not provided.
27 #' @param params List of parameters for cross-validation (each defining a model)
d9a139b5
BA
28 initialize = function(data, target, task, gmodel = NULL, params = NULL) {
29 if (is.null(gmodel)) {
c5946158
BA
30 # (Generic) model not provided
31 all_numeric <- is.numeric(as.matrix(data))
32 if (!all_numeric)
33 # At least one non-numeric column: use random forests or trees
34 # TODO: 4 = arbitrary magic number...
35 gmodel = ifelse(ncol(data) >= 4, "rf", "tree")
36 else
37 # Numerical data
38 gmodel = ifelse(task == "regression", "ppr", "knn")
39 }
d9a139b5 40 if (is.null(params))
c5946158
BA
41 # Here, gmodel is a string (= its family),
42 # because a custom model must be given with its parameters.
43 params <- as.list(private$getParams(gmodel, data, target))
44 private$params <- params
45 if (is.character(gmodel))
46 gmodel <- private$getGmodel(gmodel, task)
47 private$gmodel <- gmodel
48 self$nmodels <- length(private$params)
49 },
50 #' @description
51 #' Returns the model at index "index", trained on dataHO/targetHO.
c5946158
BA
52 #' @param dataHO Matrix or data.frame
53 #' @param targetHO Vector of targets (generally numeric or factor)
54 #' @param index Index of the model in 1...nmodels
55 get = function(dataHO, targetHO, index) {
56 private$gmodel(dataHO, targetHO, private$params[[index]])
504afaad
BA
57 },
58 #' @description
59 #' Returns the parameter at index "index".
60 #' @param index Index of the model in 1...nmodels
61 getParam = function(index) {
62 private$params[[index]]
c5946158
BA
63 }
64 ),
65 private = list(
66 # No need to expose model or parameters list
d9a139b5
BA
67 gmodel = NULL,
68 params = NULL,
c5946158
BA
69 # Main function: given a family, return a generic model, which in turn
70 # will output a predictive model from data + target + params.
71 getGmodel = function(family, task) {
72 if (family == "tree") {
73 function(dataHO, targetHO, param) {
74 require(rpart)
75 method <- ifelse(task == "classification", "class", "anova")
76 df <- data.frame(cbind(dataHO, target=targetHO))
d9a139b5 77 model <- rpart::rpart(target ~ ., df, method=method, control=list(cp=param))
c5946158
BA
78 function(X) predict(model, X)
79 }
80 }
81 else if (family == "rf") {
82 function(dataHO, targetHO, param) {
83 require(randomForest)
84 if (task == "classification" && !is.factor(targetHO))
85 targetHO <- as.factor(targetHO)
86 model <- randomForest::randomForest(dataHO, targetHO, mtry=param)
87 function(X) predict(model, X)
88 }
89 }
90 else if (family == "ppr") {
91 function(dataHO, targetHO, param) {
92 model <- stats::ppr(dataHO, targetHO, nterms=param)
93 function(X) predict(model, X)
94 }
95 }
96 else if (family == "knn") {
d9a139b5
BA
97 if (task == "classification") {
98 function(dataHO, targetHO, param) {
99 require(class)
100 function(X) class::knn(dataHO, X, cl=targetHO, k=param)
101 }
102 }
103 else {
104 function(dataHO, targetHO, param) {
105 require(FNN)
106 function(X) FNN::knn.reg(dataHO, X, y=targetHO, k=param)$pred
107 }
c5946158
BA
108 }
109 }
110 },
111 # Return a default list of parameters, given a gmodel family
112 getParams = function(family, data, target) {
113 if (family == "tree") {
114 # Run rpart once to obtain a CV grid for parameter cp
115 require(rpart)
116 df <- data.frame(cbind(data, target=target))
117 ctrl <- list(
118 minsplit = 2,
119 minbucket = 1,
120 maxcompete = 0,
121 maxsurrogate = 0,
122 usesurrogate = 0,
123 xval = 0,
124 surrogatestyle = 0,
125 maxdepth = 30)
126 r <- rpart(target ~ ., df, method="class", control=ctrl)
127 cps <- r$cptable[-1,1]
128 if (length(cps) <= 11)
129 return (cps)
130 step <- (length(cps) - 1) / 10
131 cps[unique(round(seq(1, length(cps), step)))]
132 }
133 else if (family == "rf") {
134 p <- ncol(data)
135 # Use caret package to obtain the CV grid of mtry values
136 require(caret)
137 caret::var_seq(p, classification = (task == "classificaton"),
138 len = min(10, p-1))
139 }
140 else if (family == "ppr")
141 # This is nterms in ppr() function
142 1:10
143 else if (family == "knn") {
144 n <- nrow(data)
145 # Choose ~10 NN values
146 K <- length(unique(target))
147 if (n <= 10)
148 return (1:(n-1))
149 sqrt_n <- sqrt(n)
150 step <- (2*sqrt_n - 1) / 10
151 grid <- unique(round(seq(1, 2*sqrt_n, step)))
152 if (K == 2) {
153 # Common binary classification case: odd number of neighbors
154 for (i in 2:11) {
155 if (grid[i] %% 2 == 0)
156 grid[i] <- grid[i] + 1 #arbitrary choice
157 }
158 }
159 grid
160 }
161 }
162 )
163)