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: expose "method" arg of quantile_reg #374

Merged
merged 2 commits into from
Sep 9, 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
20 changes: 12 additions & 8 deletions R/make_quantile_reg.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' "rq" is supported.
#' @param quantile_levels A scalar or vector of values in (0, 1) to determine which
#' quantiles to estimate (default is 0.5).
#' @param method A fitting method used by [quantreg::rq()]. See the
#' documentation for a list of options.
#'
#' @export
#'
Expand All @@ -22,7 +24,7 @@
#' rq_spec <- quantile_reg(quantile_levels = c(.2, .8)) %>% set_engine("rq")
#' ff <- rq_spec %>% fit(y ~ ., data = tib)
#' predict(ff, new_data = tib)
quantile_reg <- function(mode = "regression", engine = "rq", quantile_levels = 0.5) {
quantile_reg <- function(mode = "regression", engine = "rq", quantile_levels = 0.5, method = "br") {
# Check for correct mode
if (mode != "regression") {
cli_abort("`mode` must be 'regression'")
Expand All @@ -35,7 +37,7 @@ quantile_reg <- function(mode = "regression", engine = "rq", quantile_levels = 0
cli::cli_warn("Sorting `quantile_levels` to increasing order.")
quantile_levels <- sort(quantile_levels)
}
args <- list(quantile_levels = rlang::enquo(quantile_levels))
args <- list(quantile_levels = rlang::enquo(quantile_levels), method = rlang::enquo(method))

# Save some empty slots for future parts of the specification
parsnip::new_model_spec(
Expand All @@ -54,9 +56,6 @@ make_quantile_reg <- function() {
parsnip::set_new_model("quantile_reg")
}
parsnip::set_model_mode("quantile_reg", "regression")



parsnip::set_model_engine("quantile_reg", "regression", eng = "rq")
parsnip::set_dependency("quantile_reg", eng = "rq", pkg = "quantreg")

Expand All @@ -68,6 +67,14 @@ make_quantile_reg <- function() {
func = list(pkg = "quantreg", fun = "rq"),
has_submodel = FALSE
)
parsnip::set_model_arg(
model = "quantile_reg",
eng = "rq",
parsnip = "method",
original = "method",
func = list(pkg = "quantreg", fun = "rq"),
has_submodel = FALSE
)

parsnip::set_fit(
model = "quantile_reg",
Expand All @@ -78,7 +85,6 @@ make_quantile_reg <- function() {
protect = c("formula", "data", "weights"),
func = c(pkg = "quantreg", fun = "rq"),
defaults = list(
method = "br",
na.action = rlang::expr(stats::na.omit),
model = FALSE
)
Expand All @@ -101,7 +107,6 @@ make_quantile_reg <- function() {
object <- parsnip::extract_fit_engine(object)
type <- class(object)[1]


# can't make a method because object is second
out <- switch(type,
rq = dist_quantiles(unname(as.list(x)), object$quantile_levels), # one quantile
Expand All @@ -117,7 +122,6 @@ make_quantile_reg <- function() {
return(data.frame(.pred = out))
}


parsnip::set_pred(
model = "quantile_reg",
eng = "rq",
Expand Down
10 changes: 9 additions & 1 deletion man/quantile_reg.Rd

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

Loading