Skip to content

Commit

Permalink
more informative error for workflow_set that was not fitted
Browse files Browse the repository at this point in the history
  • Loading branch information
dramanica committed Oct 13, 2024
1 parent bfcb020 commit ba0d4d0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 5 additions & 1 deletion R/add_member.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ add_member <- function(x, member, ...) {
#' @rdname add_member
#' @export
add_member.default <- function(x, member, ...) {
stop("no method available for this object type")
stop("no method available for this object type: ", class(member))
}

#' @param id the name to be given to this workflow in the `wflow_id` column.
Expand Down Expand Up @@ -88,6 +88,10 @@ add_member.tune_results <- function(x, member, metric = NULL, id = NULL, ...) {
add_member.workflow_set <- function(x, member, metric = NULL, ...) {
for (i_wflow in member$wflow_id) {
this_res <- workflowsets::extract_workflow_set_result(member, id = i_wflow)
# if the result is an empty list, throw an error (how did we get to such a situation?)
if (length(this_res)==0) {
stop("no result found for workflow ", i_wflow, "; did you forget to fit the workflow?")
}
x <- x %>% add_member(this_res, metric = metric, id = i_wflow)
}
x
Expand Down
15 changes: 14 additions & 1 deletion R/sdm_spec_gam.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
#'
#' This function returns a [parsnip::model_spec] for a General Additive Model to
#' be used as a classifier of presences and absences in Species Distribution Model.
#'
#'
#' Note that, when using GAMs in a `workflow_set()`, it is necessary to
#' update the model with [gam_formula()] (see [`parsnip::model_formula`] for a
#' discussion of formulas with special terms in `tidymodels`):
#' \preformatted{
#' workflow_set(
#' preproc = list(default = my_recipe),
#' models = list(gam = sdm_spec_gam()),
#' cross = TRUE
#' ) \%>\% update_workflow_model("default_gam",
#' spec = sdm_spec_gam(),
#' formula = gam_formula(my_recipe))
#' }
#' @param ... parameters to be passed to [parsnip::gen_additive_mod()] to
#' customise the model. See the help of that function for details.
#' @param tune character defining the tuning strategy. As there are no hyperparameters
Expand All @@ -14,6 +26,7 @@
#' my_gam_spec <- sdm_spec_gam()
#' @family "sdm model specifications"
#' @export
#' @seealso [parsnip::gen_additive_mod()] [gam_formula()]

sdm_spec_gam <- function(..., tune = "none") {
tune <- rlang::arg_match(tune)
Expand Down
2 changes: 1 addition & 1 deletion data-raw/restructure/a_tidysdm_workflow_options.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ To build a `workflow_set` of different models, the `hyperparameters` we want to
For the most commonly used models, `tidysdm` automatically chooses the most important parameters:\
- Generalised Linear Model, using `sdm_spec_glm()`,

\- General Additive Model, using `sdm_spec_gam()`,
\- General Additive Model, using `sdm_spec_gam()` (note that because of the non-standard formula interface of `mgcv`, it is necessary to update the model with `gam_formula()`),

\- random forest specs with tuning, using `sdm_spec_rf()`,

Expand Down
16 changes: 16 additions & 0 deletions man/sdm_spec_gam.Rd

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

5 changes: 4 additions & 1 deletion vignettes/a0_tidysdm_overview.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,10 @@ tutorial](https://workflowsets.tidymodels.org/articles/tuning-and-comparing-mode
The latter three models have tunable hyperparameters. For the most
commonly used models, `tidysdm` automatically chooses the most important
parameters, but it is possible to fully customise model specifications
(e.g. see the help for `sdm_spec_rf`).
(e.g. see the help for `sdm_spec_rf`). Note that, if you used GAMs with `sdm_spec_gam()`,
it is necessary to update the model with `gam_formula()` due to the non-standard
formula notation of GAMs (see the help
of `sdm_spec_gam()` for an example of how to do this).

```{r workflow_set}
lacerta_models <-
Expand Down

0 comments on commit ba0d4d0

Please sign in to comment.