@@ -170,7 +170,7 @@ In addition to the data of the task, we also need the formula that describes wha
170
170
We use the function ` getTaskFormula() ` to extract this from the task.
171
171
172
172
``` {r, code=showFunctionDef(mlr:::trainLearner.classif.lda), eval=FALSE, tidy=TRUE}
173
- trainLearner.classif.lda = function(.learner, .task, .subset, .weights = NULL, ...) {
173
+ trainLearner.classif.lda = function(.learner, .task, .subset, .weights = NULL, ...) {
174
174
f = getTaskFormula(.task)
175
175
MASS::lda(f, data = getTaskData(.task, .subset), ...)
176
176
}
@@ -198,10 +198,11 @@ It is pretty much just a straight pass-through of the arguments to the `base::pr
198
198
``` {r, code=showFunctionDef(mlr:::predictLearner.classif.lda), eval=FALSE, tidy=TRUE, tidy.opts=list(indent=2, width.cutoff=100)}
199
199
predictLearner.classif.lda = function(.learner, .model, .newdata, predict.method = "plug-in", ...) {
200
200
p = predict(.model$learner.model, newdata = .newdata, method = predict.method, ...)
201
- if(.learner$predict.type == "response")
201
+ if (.learner$predict.type == "response") {
202
202
return(p$class)
203
- else
203
+ } else {
204
204
return(p$posterior)
205
+ }
205
206
}
206
207
```
207
208
@@ -243,7 +244,7 @@ makeRLearner.regr.earth = function() {
243
244
```
244
245
245
246
``` {r, code=showFunctionDef(mlr:::trainLearner.regr.earth), eval=FALSE, tidy=TRUE}
246
- trainLearner.regr.earth = function(.learner, .task, .subset, .weights = NULL, ...) {
247
+ trainLearner.regr.earth = function(.learner, .task, .subset, .weights = NULL, ...) {
247
248
f = getTaskFormula(.task)
248
249
earth::earth(f, data = getTaskData(.task, .subset), ...)
249
250
}
@@ -297,29 +298,30 @@ makeRLearner.surv.coxph = function() {
297
298
```
298
299
299
300
``` {r, code=showFunctionDef(mlr:::trainLearner.surv.coxph), eval=FALSE, tidy=TRUE, tidy.opts=list(indent=2, width.cutoff=100)}
300
- trainLearner.surv.coxph = function(.learner, .task, .subset, .weights = NULL, ...) {
301
+ trainLearner.surv.coxph = function(.learner, .task, .subset, .weights = NULL, ...) {
301
302
f = getTaskFormula(.task)
302
303
data = getTaskData(.task, subset = .subset)
303
304
if (is.null(.weights)) {
304
305
mod = survival::coxph(formula = f, data = data, ...)
305
- } else {
306
+ } else {
306
307
mod = survival::coxph(formula = f, data = data, weights = .weights, ...)
307
308
}
308
- if (.learner$predict.type == "prob")
309
+ if (.learner$predict.type == "prob") {
309
310
mod = attachTrainingInfo(mod, list(surv.range = range(getTaskTargets(.task)[, 1L])))
311
+ }
310
312
mod
311
313
}
312
314
```
313
315
314
316
``` {r, code=showFunctionDef(mlr:::predictLearner.surv.coxph), eval=FALSE, tidy=TRUE, tidy.opts=list(indent=2, width.cutoff=100)}
315
317
predictLearner.surv.coxph = function(.learner, .model, .newdata, ...) {
316
- if(.learner$predict.type == "response") {
318
+ if (.learner$predict.type == "response") {
317
319
predict(.model$learner.model, newdata = .newdata, type = "lp", ...)
318
320
} else if (.learner$predict.type == "prob") {
319
321
surv.range = getTrainingInfo(.model$learner.model)$surv.range
320
322
times = seq(from = surv.range[1L], to = surv.range[2L], length.out = 1000)
321
323
t(summary(survival::survfit(.model$learner.model, newdata = .newdata,
322
- se.fit = FALSE, conf.int = FALSE), times = times)$surv)
324
+ se.fit = FALSE, conf.int = FALSE), times = times)$surv)
323
325
} else {
324
326
stop("Unknown predict type")
325
327
}
@@ -355,7 +357,7 @@ makeRLearner.cluster.FarthestFirst = function() {
355
357
```
356
358
357
359
``` {r, code=showFunctionDef(mlr:::trainLearner.cluster.FarthestFirst), eval=FALSE, tidy=TRUE, tidy.opts=list(indent=2, width.cutoff=100)}
358
- trainLearner.cluster.FarthestFirst = function(.learner, .task, .subset, .weights = NULL, ...) {
360
+ trainLearner.cluster.FarthestFirst = function(.learner, .task, .subset, .weights = NULL, ...) {
359
361
ctrl = RWeka::Weka_control(...)
360
362
RWeka::FarthestFirst(getTaskData(.task, .subset), control = ctrl)
361
363
}
@@ -459,14 +461,15 @@ This method takes the Learner (`makeLearner()`) `.learner` and the WrappedModel
459
461
It must return the predictions in the same format as the ` predictLearner() ` function.
460
462
461
463
``` {r, code=showFunctionDef(mlr:::getOOBPredsLearner.classif.randomForest), eval=FALSE, tidy=TRUE, tidy.opts=list(indent=2, width.cutoff=100)}
464
+
462
465
```
463
466
464
467
# Registering your learner
465
468
466
469
If your interface code to a new learning algorithm exists only locally, i.e., it is not (yet) merged into ` mlr ` or does not live in an extra package with a proper namespace you might want to register the new S3 methods to make sure that these are found by, e.g., ` listLearners() ` .
467
470
You can do this as follows:
468
471
469
- ``` {r, eval=FALSE}
472
+ ``` r
470
473
registerS3method(" makeRLearner" , " <awesome_new_learner_class>" ,
471
474
makeRLearner. < awesome_new_learner_class > )
472
475
registerS3method(" trainLearner" , " <awesome_new_learner_class>" ,
@@ -477,14 +480,14 @@ registerS3method("predictLearner", "<awesome_new_learner_class>",
477
480
478
481
If you have written more methods, for example in order to extract feature importance values or out-of-bag predictions these also need to be registered in the same manner, for example:
479
482
480
- ``` {r, eval=FALSE}
483
+ ``` r
481
484
registerS3method(" getFeatureImportanceLearner" , " <awesome_new_learner_class>" ,
482
485
getFeatureImportanceLearner. < awesome_new_learner_class > )
483
486
```
484
487
485
488
For the new learner to work with parallelization, you may have to export the new methods explicitly:
486
489
487
- ``` {r, eval=FALSE}
490
+ ``` r
488
491
parallelExport(" trainLearner.<awesome_new_learner_class>" ,
489
492
" predictLearner.<awesome_new_learner_class>" )
490
493
```
0 commit comments