Skip to content
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
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pkgdown/
tests/testthat/helper_data\.Rda$
cran-comments.md
^CRAN-RELEASE$
^vignettes/articles$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
notes/
inst/doc
data-raw/paper/paper.tex
vignettes/articles/wfsets-candidates_cache*
18 changes: 13 additions & 5 deletions R/add_candidates.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@
#' evaluated using the [blend_predictions()] function.
#'
#' @param data_stack A `data_stack` object.
#' @param candidates A model definition: either a `tune_results`
#' or `resample_results` object outputted from
#' [tune::tune_grid()], [tune::tune_bayes()], or [tune::fit_resamples()].
#' These results must have been fitted with the `control` settings
#' @param candidates A (set of) model definition(s) defining candidate model
#' stack members. Should inherit from `tune_results` or `workflow_set`.
#'
#' - `tune_results`: An object outputted from [tune::tune_grid()],
#' [tune::tune_bayes()], or [tune::fit_resamples()].
#' - `workflow_set`: An object outputted from [workflowsets::workflow_map()].
#' This approach allows for supplying multiple sets of candidate members
#' with only one call to `add_candidates`. See the "Stacking With Workflow Sets"
#' article on the [package website](https://stacks.tidymodels.org/) for example code!
#'
#' Regardless, these results must have been fitted with the `control` settings
#' `save_pred = TRUE, save_workflow = TRUE`—see the [control_stack_grid()],
#' [control_stack_bayes()], and [control_stack_resamples()]
#' documentation for helper functions.
#' @param name The label for the model definition---defaults to the name
#' of the `candidates` object.
#' of the `candidates` object. Ignored if `candidates` inherits from
#' `workflow_set`.
#' @inheritParams stacks
#'
#' @return A `data_stack` object--see [stacks()] for more details!
Expand Down
19 changes: 14 additions & 5 deletions man/add_candidates.Rd

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

42 changes: 42 additions & 0 deletions tests/testthat/test_add_candidates.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,45 @@ test_that("stacks can handle columns and levels named 'class'", {
"data_stack"
)
})

test_that("stacks can add candidates via workflow sets", {
skip_on_cran()

wf_set <-
workflowsets::workflow_set(
preproc = list(tree_frogs_reg_rec, tree_frogs_reg_rec, spline_rec),
models = list(lin_reg_spec, svm_spec, lin_reg_spec),
cross = FALSE
)

wf_set_trained <-
workflowsets::workflow_map(
wf_set,
fn = "tune_grid",
seed = 1,
resamples = reg_folds,
metrics = yardstick::metric_set(yardstick::rmse),
control = control_stack_grid()
)

set.seed(1)
wf_set_stack <-
stacks() %>%
add_candidates(wf_set_trained) %>%
blend_predictions() %>%
fit_members()

set.seed(1)
wf_stack <-
stacks() %>%
add_candidates(wf_set_trained$result[[1]], name = wf_set_trained$wflow_id[[1]]) %>%
add_candidates(wf_set_trained$result[[2]], name = wf_set_trained$wflow_id[[2]]) %>%
add_candidates(wf_set_trained$result[[3]], name = wf_set_trained$wflow_id[[3]]) %>%
blend_predictions() %>%
fit_members()

expect_equal(
predict(wf_set_stack, tree_frogs),
predict(wf_stack, tree_frogs)
)
})
Loading