Skip to content

Instantly share code, notes, and snippets.

@topepo
Created November 16, 2025 18:26
Show Gist options
  • Select an option

  • Save topepo/a95f9dfdd6d1241332bcf6a1748f1db2 to your computer and use it in GitHub Desktop.

Select an option

Save topepo/a95f9dfdd6d1241332bcf6a1748f1db2 to your computer and use it in GitHub Desktop.
prototype for creating grids that are space-filling designed crossed with a lage submodel grid
cbt_spec <-
boost_tree(
trees = tune(),
min_n = tune(),
tree_depth = tune(),
learn_rate = tune(),
sample_size = tune(),
stop_iter = tune()
) |>
set_engine("xgboost") %>%
set_mode("classification")
grid_space_filling_submodels <- function(x, size = 5, submodel_size = 50, ...) {
pset <- extract_parameter_set_dials(x)
submod_info <-
paste0(class(x)[1], "_args") |>
parsnip::get_from_env() |>
# check engine available
dplyr::filter(engine == x$engine & has_submodel) |>
dplyr::select(id = parsnip) |>
dplyr::mutate(component = class(x)[1], source = "model_spec")
pset_submod <- pset[pset$id %in% submod_info$id, ]
if (nrow(pset_submod) == 0) {
res <- grid_space_filling(pset, size = size, ...)
return(res)
} else if (nrow(pset_submod) > 1) {
cli::cli_abort(
"tidymodels only supports single submodel parameters. This object has
submodels for : {.arg pset_submod$name}"
)
}
non_sub_set <- dplyr::anti_join(
pset,
submod_info,
by = c("id", "component", "source")
)
non_sub_grid <- grid_space_filling(non_sub_set, size = size)
sub_grid <- grid_regular(pset_submod, levels = submodel_size)
tidyr::crossing(non_sub_grid, sub_grid)
}
linear_reg(
penalty = tune(),
mixture = tune()
) |>
set_engine("glmnet") %>%
grid_space_filling_submodels(size = 10, submodel_size = 20) |>
ggplot(aes(penalty, mixture)) +
geom_point() +
scale_x_log10()
boost_tree(
trees = tune(),
min_n = tune(),
learn_rate = tune()
) |>
set_engine("xgboost") %>%
set_mode("classification") |>
grid_space_filling_submodels(size = 15, submodel_size = 10) |>
ggplot(aes(learn_rate, min_n)) +
geom_point() +
facet_wrap(~trees) +
scale_x_log10()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment