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 all 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
Config/testthat/edition: 3
Encoding: UTF-8
NeedsCompilation: no
Expand Down
23 changes: 19 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", "quantiles"),
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 == "quantiles") {
pv$quantreg = TRUE # nolint
}

invoke(ranger::ranger,
dependent.variable.name = task$target_names,
data = task$data(),
Expand All @@ -123,12 +126,24 @@ 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)
prediction = invoke(predict, self$model,
data = newdata,
type = self$predict_type,
quantiles = private$.quantiles,
.args = pv)

if (self$predict_type == "quantiles") {
quantiles = prediction$predictions
attr(quantiles, "probs") = private$.quantiles
attr(quantiles, "response") = private$.quantile_response
return(list(quantiles = quantiles))
}

list(response = prediction$predictions, se = prediction$se)
},

.hotstart = function(task) {
model = self$model
model = self$models
model$num.trees = self$param_set$values$num.trees
model
}
Expand Down
4 changes: 2 additions & 2 deletions inst/paramtest/test_paramtest_classif.ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ test_that("predict classif.ranger", {
learner = lrn("classif.ranger")
fun = ranger:::predict.ranger
exclude = c(
"quantiles", # required type not supported in mlr3
"what", # required type (quantiles) not supported in mlr3
"quantiles", # not supported by classification
"what", # not supported by classification
"predict.all", # not supported in mlr3
"formula", # handled via mlr3
"object", # handled via mlr3
Expand Down
7 changes: 4 additions & 3 deletions 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 All @@ -33,8 +34,8 @@ test_that("predict regr.ranger", {
learner = lrn("regr.ranger")
fun = ranger:::predict.ranger
exclude = c(
"quantiles", # required type not supported in mlr3
"what", # required type (quantiles) not supported in mlr3
"quantiles", # handled via mlr3
"what", # not supported in mlr3
"predict.all", # not supported in mlr3
"formula", # handled via mlr3
"object", # handled via mlr3
Expand Down
3 changes: 1 addition & 2 deletions man/mlr_learners_regr.ranger.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions tests/testthat/test_regr_ranger.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ 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 = "quantiles",
quantiles = c(0.1, 0.5, 0.9),
quantile_response = 0.5)
task = tsk("mtcars")

learner$train(task)
pred = learner$predict(task)

expect_matrix(pred$quantiles, ncol = 3L)
expect_true(!any(apply(pred$quantiles, 1L, is.unsorted)))
expect_equal(pred$response, pred$quantiles[, 2L])

tab = as.data.table(pred)
expect_names(names(tab), identical.to = c("row_ids", "truth", "q0.1", "q0.5", "q0.9", "response"))
expect_equal(tab$response, tab$q0.5)
})