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

Setting objective in set_engine not respected for xgboost #403

Closed
lorenzwalthert opened this issue Dec 22, 2020 · 6 comments
Closed

Setting objective in set_engine not respected for xgboost #403

lorenzwalthert opened this issue Dec 22, 2020 · 6 comments

Comments

@lorenzwalthert
Copy link

lorenzwalthert commented Dec 22, 2020

I wanted to use one of the many objectives in xgboost described here.
So I went on and wrote this code to set the objective in set_engine(), as ... are passed to the engine as described in the docs for ... in help(set_engine, package = 'parsnip'):

Any optional arguments associated with the chosen computational engine. [...]

However, I think the objective is not respected, as it is always derived from mode, which can only be regression or classification.

library(parsnip)
library(magrittr)
# boost_tree
model <- parsnip::boost_tree(mtry = NULL, trees = 5, mode = 'regression', learn_rate = .1)
model <- parsnip::set_engine(model, "xgboost", objective = "reg:squaredlogerror")
set.seed(4)
data <- matrix(rnorm(1000), ncol = 4) %>%
  tibble::as_tibble() %>%
  dplyr::mutate(y = sample(1000/4))
fit <- model %>%
  parsnip::fit(y ~., data = data)

predict(fit, data)

This problem actually did come up in curso-r/treesnip#24, where I wanted to use the quantile objective for {lightgbm} and could not and the answer was: "We just copied that code from parsnip". In the interest of a consistent solution to this problem, I am creating an issue here. As described in curso-r/treesnip#24, the maintainers of {treesnip} are happy to let the user override the objective, but I thought maybe this topic merits a discussion here, as the same problem applies to {xgboost} and other engines too.

I think it should be possible to override the objective if it is consistent with the mode argument, because I it can in some instances be orthogonal to the mode (e.g. in my example, i want to use the squared log error) or specify the mode more precisely, e.g. survival:cox is a Cox regression, which is consistent with mode = 'regression' conceptually I think.

@topepo
Copy link
Member

topepo commented Jan 14, 2021

Fixed in #411

library(parsnip)
library(magrittr)
# boost_tree
model <- parsnip::boost_tree(mtry = NULL, trees = 5, mode = 'regression', learn_rate = .1)
model <- parsnip::set_engine(model, "xgboost", objective = "reg:squaredlogerror")
set.seed(4)
data <- matrix(rnorm(1000), ncol = 4) %>%
  as.data.frame() %>%
  dplyr::mutate(y = sample(1000/4))
fit <- model %>%
  parsnip::fit(y ~., data = data)

predict(fit, data)
#> # A tibble: 250 x 1
#>    .pred
#>    <dbl>
#>  1  1.20
#>  2  1.20
#>  3  1.20
#>  4  1.20
#>  5  1.20
#>  6  1.20
#>  7  1.20
#>  8  1.20
#>  9  1.20
#> 10  1.20
#> # … with 240 more rows
fit$fit$params$objective
#> [1] "reg:squaredlogerror"

Created on 2021-01-14 by the reprex package (v0.3.0)

@topepo topepo closed this as completed Jan 14, 2021
@lorenzwalthert
Copy link
Author

lorenzwalthert commented Jan 15, 2021

Thanks Max. Will head over to treesnip and try (or make them try) to implement the same there. 😄

@topepo
Copy link
Member

topepo commented Jan 15, 2021

I have on my list a few PRs to submit over there but have not had time. If you want I can include this (if you don't need it right away).

@lorenzwalthert
Copy link
Author

If you want to do it, I'd surely prefer that 😄. There is already an issue for it as mentioned above just to make sure: curso-r/treesnip#24. Thanks a lot.

@Athospd
Copy link

Athospd commented Jan 15, 2021

I went ahead and replicate the logic for catboost and lightgbm to spare @topepo’s time.
curso-r/treesnip@62fe946 and curso-r/treesnip@c15533a

@github-actions
Copy link

github-actions bot commented Mar 6, 2021

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants