## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5
)

## -----------------------------------------------------------------------------
load_selectboost_quantile <- function() {
  if (requireNamespace("SelectBoost.quantile", quietly = TRUE)) {
    library(SelectBoost.quantile)
    return(invisible(TRUE))
  }

  if (!requireNamespace("pkgload", quietly = TRUE)) {
    stop(
      "SelectBoost.quantile is not installed and pkgload is unavailable.",
      call. = FALSE
    )
  }

  roots <- c(".", "..")
  roots <- roots[file.exists(file.path(roots, "DESCRIPTION"))]
  if (!length(roots)) {
    stop("Could not locate the package root for SelectBoost.quantile.", call. = FALSE)
  }

  pkgload::load_all(roots[[1]], export_all = FALSE, helpers = FALSE, quiet = TRUE)
  invisible(TRUE)
}

load_selectboost_quantile()

sim <- simulate_quantile_data(
  n = 100,
  p = 20,
  active = 1:4,
  rho = 0.7,
  correlation = "toeplitz",
  tau = 0.5,
  seed = 1
)

## -----------------------------------------------------------------------------
fit <- selectboost_quantile(
  sim$x,
  sim$y,
  tau = 0.5,
  B = 6,
  step_num = 0.5,
  screen = "auto",
  tune_lambda = "cv",
  lambda_rule = "one_se",
  lambda_inflation = 1.25,
  subsamples = 4,
  sample_fraction = 0.5,
  complementary_pairs = TRUE,
  max_group_size = 10,
  seed = 1,
  verbose = FALSE
)

print(fit)

## -----------------------------------------------------------------------------
smry <- summary(fit)
smry

support_selectboost_quantile(fit)
coef(fit, threshold = 0.55)

## -----------------------------------------------------------------------------
support_selectboost_quantile(
  fit,
  threshold = 0.55,
  selection_metric = "frequency"
)

## -----------------------------------------------------------------------------
plot(fit)

## -----------------------------------------------------------------------------
dat <- data.frame(y = sim$y, sim$x)

fit_formula <- selectboost_quantile(
  y ~ .,
  data = dat,
  tau = c(0.25, 0.5, 0.75),
  B = 4,
  step_num = 0.5,
  tune_lambda = "bic",
  seed = 2,
  verbose = FALSE
)

print(fit_formula)
summary(fit_formula, tau = 0.5)

## -----------------------------------------------------------------------------
predict(
  fit_formula,
  newdata = dat[1:3, -1, drop = FALSE],
  tau = 0.5
)

## -----------------------------------------------------------------------------
tuned <- tune_lambda_quantile(
  sim$x,
  sim$y,
  tau = 0.5,
  method = "cv",
  rule = "one_se",
  lambda_inflation = 1.25,
  nlambda = 6,
  folds = 3,
  repeats = 2,
  seed = 3,
  verbose = FALSE
)

print(tuned)
summary(tuned)

