Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add quantile regression to ranger #308

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ Suggests:
rmarkdown,
testthat (>= 3.0.0),
xgboost (>= 1.6.0)
Remotes:
mlr-org/mlr3@predict_type_quantile
Config/testthat/edition: 3
Encoding: UTF-8
NeedsCompilation: no
Expand Down
26 changes: 22 additions & 4 deletions R/LearnerRegrRanger.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ LearnerRegrRanger = R6Class("LearnerRegrRanger",
num.threads = p_int(1L, default = 1L, tags = c("train", "predict", "threads")),
num.trees = p_int(1L, default = 500L, tags = c("train", "predict", "hotstart")),
oob.error = p_lgl(default = TRUE, tags = "train"),
quantreg = p_lgl(default = FALSE, tags = "train"),
regularization.factor = p_uty(default = 1, tags = "train"),
regularization.usedepth = p_lgl(default = FALSE, tags = "train"),
replace = p_lgl(default = TRUE, tags = "train"),
Expand All @@ -64,7 +63,7 @@ LearnerRegrRanger = R6Class("LearnerRegrRanger",
super$initialize(
id = "regr.ranger",
param_set = ps,
predict_types = c("response", "se"),
predict_types = c("response", "se", "quantile"),
feature_types = c("logical", "integer", "numeric", "character", "factor", "ordered"),
properties = c("weights", "importance", "oob_error", "hotstart_backward"),
packages = c("mlr3learners", "ranger"),
Expand Down Expand Up @@ -111,6 +110,10 @@ LearnerRegrRanger = R6Class("LearnerRegrRanger",
pv$keep.inbag = TRUE # nolint
}

if (self$predict_type == "quantile") {
pv$quantreg = TRUE # nolint
}

invoke(ranger::ranger,
dependent.variable.name = task$target_names,
data = task$data(),
Expand All @@ -123,8 +126,23 @@ LearnerRegrRanger = R6Class("LearnerRegrRanger",
pv = self$param_set$get_values(tags = "predict")
newdata = ordered_features(task, self)

prediction = invoke(predict, self$model, data = newdata, type = self$predict_type, .args = pv)
list(response = prediction$predictions, se = prediction$se)
type = switch(
be-marc marked this conversation as resolved.
Show resolved Hide resolved
self$predict_type,
response = "response",
se = "se",
quantile = "quantiles"
)

prediction = invoke(predict, self$model, data = newdata, type = type, quantiles = private$.quantile, .args = pv)

if (type == "quantiles") {
response = prediction$predictions[, which(private$.quantile == private$.quantile_response)]
quantile = prediction$predictions
attr(quantile, "probs") = private$.quantile
be-marc marked this conversation as resolved.
Show resolved Hide resolved
list(response = response, quantile = quantile)
} else {
list(response = prediction$predictions, se = prediction$se)
}
},

.hotstart = function(task) {
Expand Down
3 changes: 2 additions & 1 deletion inst/paramtest/test_paramtest_regr.ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ test_that("regr.ranger", {
"status.variable.name", # handled via mlr3
"classification", # handled via mlr3
"mtry.ratio", # custom hyperpar
"time.interest" # survival only
"time.interest", # survival only
"quantreg" # handled by predict_type
)

ParamTest = run_paramtest(learner, fun, exclude, tag = "train")
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/test_regr_ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ test_that("default_values", {
values = default_values(learner, search_space, task)
expect_names(names(values), permutation.of = c("replace", "sample.fraction", "num.trees", "mtry.ratio"))
})

test_that("quantile prediction", {
learner = mlr3::lrn("regr.ranger", num.trees = 100, predict_type = "quantile")
learner$quantiles = c(0.1, 0.5, 0.9)
learner$quantile_response = 0.5
task = tsk("mtcars")

learner$train(task)
pred = learner$predict(task)
expect_matrix(pred$quantile)
tab = as.data.table(pred)
expect_names(names(tab), must.include = c("q0.1", "q0.5", "q0.9", "response"))
be-marc marked this conversation as resolved.
Show resolved Hide resolved
expect_equal(tab$response, tab$q0.5)
})
Loading