From b26724284a0a6aacdbacda37abd64c868599ab62 Mon Sep 17 00:00:00 2001 From: ielbadisy Date: Sun, 15 Nov 2026 05:14:29 +0000 Subject: [PATCH] Accept a regression objective in the xgboost learner (Poisson, gamma, Tweedie) --- NEWS.md | 4 ++++ R/fit.R | 7 +++++++ R/registry.R | 8 +++++++- man/fit.Rd | 7 +++++++ tests/testthat/test-xgboost-objective.R | 23 +++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-xgboost-objective.R diff --git a/NEWS.md b/NEWS.md index c4bc545..f137707 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # funcml 0.9.11 +* The `xgboost` learner accepts a regression `objective` in `spec` + (default `"reg:squarederror"`), so count and skewed outcomes can be fitted + with `"count:poisson"`, `"reg:gamma"` or `"reg:tweedie"` (plus + `tweedie_variance_power`). Predictions are on the mean scale. * New `bootvalidate()` for bootstrap internal validation of any learner. It returns the apparent performance, the Harrell optimism-corrected estimate (as `rms::validate()`), the leave-one-out bootstrap (OOB) diff --git a/R/fit.R b/R/fit.R index 52cf39a..270b079 100644 --- a/R/fit.R +++ b/R/fit.R @@ -29,6 +29,13 @@ #' (default 0.1) is the width of the insensitive tube in support vector #' regression and is ignored for classification. #' +#' For regression, `xgboost` accepts `spec$objective`, any xgboost regression +#' objective (default `"reg:squarederror"`), for example `"count:poisson"` +#' for counts, `"reg:gamma"` or `"reg:tweedie"` (with +#' `spec$tweedie_variance_power`, default 1.5 in xgboost). Predictions are +#' on the response (mean) scale. Classification objectives are set +#' automatically. +#' #' @param formula Model formula. #' @param data Data frame. #' @param model Learner id (see `learners()`). diff --git a/R/registry.R b/R/registry.R index 0c1aa81..3c8d3b9 100644 --- a/R/registry.R +++ b/R/registry.R @@ -1153,8 +1153,14 @@ build_registry <- function() { eta = spec$eta, subsample = spec$subsample, colsample_bytree = spec$colsample_bytree, - objective = if (task == "regression") "reg:squarederror" else if (length(levels(y)) > 2) "multi:softprob" else "binary:logistic" + objective = if (task == "regression") spec$objective %||% "reg:squarederror" else if (length(levels(y)) > 2) "multi:softprob" else "binary:logistic" ) + if (task == "regression" && !is.null(spec$tweedie_variance_power)) { + params$tweedie_variance_power <- spec$tweedie_variance_power + } + if (task == "regression" && params$objective %in% c("count:poisson", "reg:gamma", "reg:tweedie") && any(y < 0)) { + stop(sprintf("xgboost objective '%s' needs a non-negative outcome.", params$objective), call. = FALSE) + } if (task == "classification" && length(levels(y)) > 2) { params$num_class <- length(levels(y)) } diff --git a/man/fit.Rd b/man/fit.Rd index 2138ffe..e99c69f 100644 --- a/man/fit.Rd +++ b/man/fit.Rd @@ -69,6 +69,13 @@ accepted by \code{\link[kknn:kknn]{kknn::kknn()}} (default \code{"optimal"}; for \code{e1071_svm} takes \code{spec = list(cost, gamma, kernel, epsilon)}; \code{epsilon} (default 0.1) is the width of the insensitive tube in support vector regression and is ignored for classification. + +For regression, \code{xgboost} accepts \code{spec$objective}, any xgboost regression +objective (default \code{"reg:squarederror"}), for example \code{"count:poisson"} +for counts, \code{"reg:gamma"} or \code{"reg:tweedie"} (with +\code{spec$tweedie_variance_power}, default 1.5 in xgboost). Predictions are +on the response (mean) scale. Classification objectives are set +automatically. } \examples{ fit_obj <- fit(mpg ~ wt + hp, data = mtcars, model = "glm") diff --git a/tests/testthat/test-xgboost-objective.R b/tests/testthat/test-xgboost-objective.R new file mode 100644 index 0000000..120981a --- /dev/null +++ b/tests/testthat/test-xgboost-objective.R @@ -0,0 +1,23 @@ +test_that("xgboost regression objective is passed through (Poisson)", { + skip_if_not_installed("xgboost") + set.seed(4) + n <- 300 + d <- data.frame(x1 = rnorm(n), x2 = rnorm(n)) + d$y <- rpois(n, exp(0.5 + 0.6 * d$x1)) + spec <- list(objective = "count:poisson", nrounds = 50, max_depth = 2, eta = 0.1) + f <- fit(y ~ x1 + x2, d, model = "xgboost", spec = spec) + expect_identical(xgboost::xgb.config(enginemodel(f))$learner$objective$name, "count:poisson") + X <- as.matrix(d[, c("x1", "x2")]) + ref <- xgboost::xgb.train( + params = list(max_depth = 2, eta = 0.1, subsample = 1, colsample_bytree = 1, + objective = "count:poisson"), + data = xgboost::xgb.DMatrix(X, label = d$y), nrounds = 50, verbose = 0 + ) + p <- predict(f, d) + expect_equal(as.numeric(p), as.numeric(predict(ref, xgboost::xgb.DMatrix(X))), tolerance = 1e-6) + expect_true(all(p > 0)) + # mean-scale predictions track the true mean better than a constant + expect_lt(mean((p - exp(0.5 + 0.6 * d$x1))^2), stats::var(exp(0.5 + 0.6 * d$x1))) + d$y[1] <- -1 + expect_error(fit(y ~ x1 + x2, d, model = "xgboost", spec = spec), "non-negative") +})