Skip to content
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
7 changes: 7 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()`).
Expand Down
8 changes: 7 additions & 1 deletion R/registry.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
7 changes: 7 additions & 0 deletions man/fit.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/test-xgboost-objective.R
Original file line number Diff line number Diff line change
@@ -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")
})
Loading