R/model-rq.R

Defines functions tidypredict_normalized.rqs tidypredict_outcome_levels.rqs tidypredict_output_type.rqs split_rqs acceptable_formula.rqs parse_model.rqs tidypredict_fit.rqs acceptable_formula.rq parse_model.rq

# Predict ---------------------------------------

# Parse model --------------------------------------

#' @export
parse_model.rq <- function(model) parse_model_lm(model)

#' @export
acceptable_formula.rq <- function(model) {
  acceptable_lm(model)
}

# rqs (multiple quantiles) -------------------------

#' @export
tidypredict_fit.rqs <- function(model) {
  models <- split_rqs(model)
  set_names(
    map(models, tidypredict_fit),
    paste0("quantile_", format(model$tau))
  )
}

#' @export
parse_model.rqs <- function(model) {
  models <- split_rqs(model)
  set_names(
    map(models, parse_model),
    paste0("quantile_", format(model$tau))
  )
}

#' @export
acceptable_formula.rqs <- function(model) {
  acceptable_lm(model)
}

# Split an `rqs` object (multiple quantiles) into a list of single-quantile
# `rq` objects, one per column of the coefficient matrix.
split_rqs <- function(model) {
  coefs <- model$coefficients
  map(
    seq_along(model$tau),
    ~ {
      one <- model
      class(one) <- "rq"
      one$coefficients <- coefs[, .x]
      one$tau <- model$tau[.x]
      one
    }
  )
}

# Output metadata ---------------------------------

# `parse_model.rqs()` returns a bare list of parsed models rather than a single
# parsed model, so the default cannot route through it. Several `tau` give a
# named list of expressions that looks exactly like a multiclass probability
# list but holds unrelated quantile predictions.
#' @export
tidypredict_output_type.rqs <- function(x, ...) {
  rlang::check_dots_empty()
  "numeric"
}

#' @export
tidypredict_outcome_levels.rqs <- function(x, ...) {
  rlang::check_dots_empty()
  NULL
}

#' @export
tidypredict_normalized.rqs <- function(x, ...) {
  rlang::check_dots_empty()

  # The names are quantiles, not levels, and the values do not sum to anything.
  NA
}

Try the tidypredict package in your browser

Any scripts or data that you put into this service are public.

tidypredict documentation built on Oct. 18, 2026, 5:06 p.m.