### Install Package from PR Branch in GitHub Actions Source: https://github.com/tidymodels/extratests/blob/main/README.md This snippet demonstrates how to configure GitHub Actions to install a package directly from a pull request branch. It uses the `pak::pkg_install()` function and appends the PR number to the package repository path. This is crucial for ensuring that CI tests in `extratests` run against the specific changes in a target package's PR. ```yaml try(pak::pkg_install("tidymodels/parsnip#991")) ``` -------------------------------- ### Configure Bagged Tree for Regression with RPART Engine Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md This code configures a bagged decision tree model for regression using the 'rpart' engine. It demonstrates the initial setup and the message returned when the specific implementation is not immediately available, guiding the user to install and load the 'baguette' extension package. ```r bag_tree() %>% set_engine("rpart") %>% set_mode("regression") ``` -------------------------------- ### Show Best Static Metric with Evaluation Time (ANOVA Racing) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Demonstrates using `show_best` with a static metric and attempting to specify an `eval_time`. A warning is issued as `eval_time` is only applicable to dynamic survival metrics. ```r show_best(race_stc_res, metric = "concordance_survival", eval_time = 1) ``` -------------------------------- ### Display Recipe Object with NNMF Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/recipes-nnmf_sparse.md Prints a recipe object that has undergone non-negative matrix factorization. This shows the structure of the recipe, including the number of variables by role and the NNMF operation applied to all predictors. It's useful for inspecting the state of a recipe after preparation. ```r print(rec) ``` -------------------------------- ### Show Best Dynamic Metric with Explicit Metric (Bayesian Optimization) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Shows how to explicitly specify a dynamic metric like 'brier_survival' when using `show_best`. An error occurs if the requested metric is not found in the set of evaluated metrics. ```r show_best(bayes_dyn_res, metric = "brier_survival_integrated") ``` -------------------------------- ### Show Best Static Metric with Explicit Metric (ANOVA Racing) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Shows how to explicitly specify a static metric like 'concordance_survival' when using `show_best`. An error occurs if the requested metric is not found in the set of evaluated metrics. ```r show_best(race_stc_res, metric = "brier_survival_integrated") ``` -------------------------------- ### Show Best Dynamic Metric with Specific Evaluation Time (Bayesian Optimization) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Demonstrates specifying an exact evaluation time for a dynamic metric when using `show_best` with Bayesian optimization. This can lead to an error if the specified time is not available in the results. ```r show_best(bayes_dyn_res, metric = "brier_survival", eval_time = 1) ``` -------------------------------- ### Get Labs for Dynamic Metric Plot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object associated with dynamic metric race tuning. This helps in understanding the plot's components when using dynamic survival metrics. ```r ggplot2::get_labs(dyn_race_plot) ``` ```r ggplot2::get_labs(dyn_autoplot) ``` -------------------------------- ### Get Labs for Integrated Metric Plot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object related to integrated metric race tuning. This is useful for inspecting the plot's aesthetic mappings and axis titles. ```r ggplot2::get_labs(int_race_plot) ``` ```r ggplot2::get_labs(int_autoplot) ``` -------------------------------- ### Prepare Recipe with NNMF Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/recipes-nnmf_sparse.md Prepares a recipe object using the 'prep' function with the 'iris' dataset as training data. The 'verbose = TRUE' argument provides detailed output during the preparation process, including information about the NNMF operation and memory usage. This is a standard step before baking a recipe for new data. ```r rec <- prep(rec, training = iris, verbose = TRUE) ``` -------------------------------- ### Set Engine and Mode for Decision Tree (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md Demonstrates setting both the engine and mode for a decision tree model specification in R. This example attempts to use the 'partykit' engine with a 'regression' mode, which may result in a message if an implementation is not found. ```r decision_tree() %>% set_engine("partykit") %>% set_mode("regression") ``` -------------------------------- ### Tune Sim Annealing with Interactive Logger Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/tune-catalog-finetune.md This code demonstrates the `tune_sim_anneal` function, employing simulated annealing for model tuning. It includes a control function designed to raise warnings and errors, illustrating the interactive logger's capability to capture and report these issues during iterative tuning. The example uses kknn regression and initializes with a previous tuning result. ```r res_sa <- tune_sim_anneal(parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()), Sale_Price ~ ., rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5), initial = res_anova, iter = 15, control = control_sim_anneal( verbose_iter = FALSE, extract = function(x) { raise_warning() raise_error() })) ``` -------------------------------- ### Get Labs for Static Metric Plot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object associated with static metric race tuning. This function helps in understanding the components of the plot, such as axis labels and group identifiers. ```r ggplot2::get_labs(stc_race_plot) ``` ```r ggplot2::get_labs(stc_autoplot) ``` -------------------------------- ### Show Best Static Metric with Censored Data (ANOVA Racing) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Presents the results for a static metric using ANOVA racing with censored data. It defaults to 'concordance_survival' if no metric is specified. The output shows the best performance for the given cost complexity. ```r show_best(race_stc_res) ``` -------------------------------- ### Show Best Dynamic Metric with Censored Data (Bayesian Optimization) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Illustrates using `show_best` with censored data and a dynamic metric, evaluated using Bayesian optimization. It defaults to 'brier_survival' if no metric is specified. The output includes evaluation time and other performance metrics. ```r show_best(bayes_dyn_res) ``` -------------------------------- ### Configure Bagged Tree for Regression with RPART (Duplicate) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md This R code configures a bagged decision tree model for regression using the 'rpart' engine. It is a duplicate example showing the message when the implementation is missing and the 'baguette' package is suggested. ```r bag_tree() %>% set_mode("regression") %>% set_engine("rpart") ``` -------------------------------- ### Show Best Results for Static Metric (Censored Data) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Displays the best model results using a static metric for censored data. When no metric is specified, 'concordance_survival' is used by default. The `eval_time` argument is ignored for static metrics. ```r show_best(race_stc_res) ``` ```r show_best(race_stc_res, metric = "concordance_survival", eval_time = 1) ``` -------------------------------- ### Tune Model with Integrated Metric (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Tunes a survival model using an integrated metric (brier_survival_integrated) and specifies an evaluation time. It demonstrates how to use `show_best` to retrieve the optimal 'cost_complexity' parameter. A warning is issued if `eval_time` is not applicable to the chosen metric. ```r num_final_wl <- show_best(wl_integrated_res, metric = "brier_survival_integrated", eval_time = 5) %>% pluck("cost_complexity") %>% unique() ``` -------------------------------- ### Set Engine for Decision Tree Model Specification (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md Illustrates how to specify a different computational engine for a decision tree model specification using `set_engine()` in R. This example switches the engine to 'partykit' while keeping the mode as 'unknown'. ```r decision_tree() %>% set_engine("partykit") ``` -------------------------------- ### Show Best Models by Integrated Brier Survival (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Displays the best performing model candidates using the 'brier_survival_integrated' metric. It selects results and removes '.estimator' and '.config' columns. A warning is issued because 'brier_survival' was used during tuning, and using a different metric ('brier_survival_integrated') for ranking might lead to different conclusions. ```r show_best(wl_mixed_res, metric = "brier_survival_integrated") %>% select( -.estimator, -.config) ``` -------------------------------- ### Show Best Results for Dynamic Metric (Censored Data) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Demonstrates how to display the best model results using a dynamic metric for censored data. When no metric is specified, 'brier_survival' is used by default. This snippet also shows an error when requesting an invalid metric. ```r show_best(race_dyn_res) ``` ```r mutate(show_best(race_dyn_res, metric = "concordance_survival"), mean = round(mean, 2), std_err = round(std_err, 2)) ``` ```r show_best(race_dyn_res, metric = "brier_survival", eval_time = 1) ``` ```r show_best(race_dyn_res, metric = "brier_survival_integrated") ``` -------------------------------- ### Show Best Integrated Metric with Censored Data (Grid Search) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-show-best.md Displays the best model results using an integrated metric with grid search. When no specific metric is provided, it defaults to 'brier_survival_integrated'. The output shows model performance across different cost complexities. ```r show_best(grid_int_res) ``` -------------------------------- ### Analyze Survival Model Tuning Output Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md This section displays the output of a survival model tuning process, showing a tibble with cost complexity, performance metrics (brier_survival_integrated), evaluation time, mean performance, sample size, and standard error. The output is generated from a tidymodels workflow. ```r Output # A tibble: 5 x 6 cost_complexity .metric .eval_time mean n std_err 1 7.94e-11 brier_survival_integrated NA 0.285 30 0.00426 2 8.41e-11 brier_survival_integrated NA 0.285 30 0.00426 3 8.91e-11 brier_survival_integrated NA 0.285 30 0.00426 4 9.44e-11 brier_survival_integrated NA 0.285 30 0.00426 5 1 e-10 brier_survival_integrated NA 0.285 30 0.00426 # race tuning (W/L) - unneeded eval_time ``` -------------------------------- ### Tune Model with Dynamic Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Tunes a survival model using dynamic metrics and specifies multiple evaluation times. The `tune_race_win_loss` function is used with custom control parameters. A warning is issued if multiple evaluation times are provided, indicating that only the first will be used. ```r set.seed(2193) wl_dyn_res <- mod_spec %>% tune_race_win_loss(event_time ~ X1 + X2, resamples = sim_rs, grid = grid, metrics = dyn_mtrc, eval_time = time_points, control = rctrl) ``` -------------------------------- ### Get Plot Labels for Mixed Multi-Autoplot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object generated by a multi-autoplot function for a mixed model. `ggplot2::get_labs` is used to examine the aesthetic mappings, including those for alpha, size, x-axis, and y-axis. It indicates how different parameters are represented visually in the plot. ```r ggplot2::get_labs(mix_multi_autoplot) ``` -------------------------------- ### Get Plot Labels for Mixed Autoplot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object generated by an autoplot function for a mixed model. `ggplot2::get_labs` inspects the aesthetics mapped to the plot, showing labels for parameters like alpha, size, x-axis, and y-axis. It displays the current mapping, such as 'Cost-Complexity Parameter' for the x-axis. ```r ggplot2::get_labs(mix_autoplot) ``` -------------------------------- ### Get Plot Labels for Mixed Alternative Autoplot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object generated by an alternative autoplot function for a mixed model. This function, `ggplot2::get_labs`, helps understand the plot's aesthetic mappings, showing labels for parameters like alpha, size, x, and y-axis. It can reveal different metrics being plotted, such as 'concordance_survival'. ```r ggplot2::get_labs(mix_alt_autoplot) ``` -------------------------------- ### Show Best Models by Brier Survival at Specific Eval Times (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Displays the best performing model candidates based on the 'brier_survival' metric at evaluation times 1 and 3. The output is a tibble with '.estimator' and '.config' columns removed. If multiple evaluation times are specified, a warning may indicate that only the first available time is used for ranking. ```r show_best(wl_mixed_res, metric = "brier_survival", eval_time = c(1, 3)) %>% select(-.estimator, -.config) ``` -------------------------------- ### Set Mode for Decision Tree Model Specification (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md Shows how to set the operational mode for a decision tree model specification using `set_mode()` in R. The example sets the mode to 'censored regression', retaining the default 'rpart' engine. ```r decision_tree() %>% set_mode("censored regression") ``` -------------------------------- ### Attempt to Show Best Models with Unused Metric (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Attempts to display the best performing model candidates using a metric named 'unused_metric'. This operation is expected to fail with an error message indicating that the specified metric is not available in the results. The function will list the available metrics for selection. ```r res <- show_best(wl_mixed_res, metric = "unused_metric", eval_time = c(1, 3)) %>% select(-.estimator, -.config) ``` -------------------------------- ### Multi-predict for time predictions with censored regression Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/glmnet-logistic.md This example demonstrates using multi_predict() for time predictions. It highlights a condition where the object must be a censored regression model for event time predictions. Attempting to use multi_predict() with a non-censored regression model for time predictions will result in an error. ```R logistic_reg(penalty = 0.01) %>% set_engine("glmnet") %>% fit(Class ~ log(funded_amnt) + int_rate + term, data = lending_club) %>% multi_predict(lending_club, type = "time") ``` -------------------------------- ### Fitting glmnet Engine Without Penalty Values Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/glmnet-multinom.md Illustrates fitting a `multinom_reg` model with the `glmnet` engine when no specific penalty value is provided. The `glmnet` engine requires a single numeric value for `penalty` or a `tune()` specification. This example highlights the need to specify or tune the penalty. ```R multinom_reg() %>% set_engine("glmnet") %>% fit(class ~ ., data = hpc_data) ``` -------------------------------- ### Tune Linear Regression Model with glmnet Engine Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md This code snippet demonstrates tuning a linear regression model using the `glmnet` engine within the tidymodels framework. It employs `tune_race_win_loss` for race-based resampling and specifies `rmse` as the performance metric. Note the warning generated because `eval_time` is not applicable to this model type. ```r tune_res <- linear_reg(penalty = tune(), engine = "glmnet") %>% tune_race_win_loss(mpg ~ ., resamples = vfold_cv(mtcars, 5), metrics = metric_set( rmse), eval_time = 10) Condition Warning in `tune_race_win_loss()`: `eval_time` is only used for models with mode "censored regression". ``` -------------------------------- ### Show Best Models by Brier Survival at Eval Time 1 (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the top performing model candidates using the 'brier_survival' metric specifically at evaluation time 1. The results are presented as a tibble, with the '.estimator' and '.config' columns removed. This allows for a focused view of model performance at a particular time point. ```r show_best(wl_mixed_res, metric = "brier_survival", eval_time = 1) %>% select( -.estimator, -.config) ``` -------------------------------- ### Predicting Censoring Probabilities with Invalid Arguments Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-survival-censoring-model.md This example demonstrates predicting censoring probabilities using a censoring model, highlighting the error that occurs when an invalid argument is passed to the prediction function. The `predict()` function expects specific arguments, and extraneous ones will raise an error. ```r predict(mod_fit$censor_probs, time = pred_times, invalid_arg = TRUE) ``` -------------------------------- ### Tune Proportional Hazards Model with glmnet Engine Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md This code snippet shows how to tune a proportional hazards survival model using the `glmnet` engine with tidymodels. It uses `tune_race_win_loss` and the `concordance_survival` metric. A warning is issued regarding `eval_time` as it's only relevant for specific dynamic or integrated survival metrics. ```r tune_res <- proportional_hazards(penalty = tune(), engine = "glmnet") %>% tune_race_win_loss(surv ~ ., resamples = vfold_cv(lung_surv, 5), metrics = metric_set( concordance_survival), eval_time = 10) Condition Warning in `tune_race_win_loss()`: `eval_time` is only used for dynamic or integrated survival metrics. ``` -------------------------------- ### Tune Survival Model with Mixed Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Tunes a survival model using the `tune_race_win_loss` function with a mixture of metric types. It requires a model specification (`mod_spec`), a formula, resampled data (`sim_rs`), a grid of tuning parameters (`grid_ties`), a mix of metrics (`mix_mtrc`), evaluation times (`time_points`), and control parameters (`rctrl`). The function may issue warnings about available evaluation times. ```r set.seed(2193) wl_mixed_res <- mod_spec %>% tune_race_win_loss(event_time ~ X1 + X2, resamples = sim_rs, grid = grid_ties, metrics = mix_mtrc, eval_time = time_points, control = rctrl) ``` -------------------------------- ### Stacking Data Preparation with Bayesian Tuning Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/stacks-tuning.md Prepares data for stacking models by adding candidate workflows optimized via Bayesian tuning. This function expects a workflow set (`wf_set_bayes`) as input and may issue warnings if certain workflows fail evaluation. ```r data_st_bayes <- stacks() %>% add_candidates(wf_set_bayes) %>% suppressMessages() ``` -------------------------------- ### Get Plot Labels for Mixed Race Plot (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_win_loss.md Retrieves the labels for a ggplot object representing a mixed race plot. This function, `ggplot2::get_labs`, is used to inspect the aesthetics mapped to different plot elements such as x, y, and group. The output shows the current labels for these aesthetics. ```r ggplot2::get_labs(mix_race_plot) ``` -------------------------------- ### Recipe Varying Parameters for Hyperparameter Tuning in R Source: https://context7.com/tidymodels/extratests/llms.txt Illustrates how to define recipes with `varying()` parameters for hyperparameter tuning and how to extract these varying arguments using `varying_args()`. This functionality is useful for integrating preprocessing steps with model tuning workflows. ```r library(testthat) library(tidymodels) withr::local_options(lifecycle_verbosity = "quiet") # Define recipe with varying parameters rec_1 <- recipe(mpg ~ ., mtcars) %>% step_center(all_predictors()) %>% step_impute_knn(all_predictors(), neighbors = varying()) %>% step_pca(all_predictors(), num_comp = varying()) # Extract varying arguments (full = TRUE returns all parameters) rec_res_1 <- varying_args(rec_1) # Example of expected output structure and assertions # expect_s3_class(rec_res_1, "tbl_df") # expect_true("varying" %in% names(rec_res_1)) # Extract only varying parameters (full = FALSE) rec_varying_only <- varying_args(rec_1, full = FALSE) # expect_identical(rec_varying_only$name, c("neighbors", "num_comp")) # Use with model spec x_spec <- rand_forest(min_n = varying()) %>% set_engine("ranger", sample.fraction = varying()) spec_varying <- varying_args(x_spec, full = FALSE) # expect_identical(spec_varying$name, c("min_n", "sample.fraction")) ``` -------------------------------- ### Get Plot Labels for Dynamic Autoplot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Extracts labels from a ggplot object representing a dynamic autoplot. This aids in understanding the plot's structure and the meaning of its aesthetic elements. The function operates on a ggplot object. ```r ggplot2::get_labs(dyn_autoplot) ``` -------------------------------- ### Fit XGBoost with Sparse Matrix (High Sparsity) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/sparsity.md This snippet demonstrates fitting a model using the 'xgboost' engine when the input data is a high sparsity matrix. It is expected to produce an error indicating that 'x' is a sparse matrix, as demonstrated in several test cases. ```R fit(wf_spec, ames) ``` -------------------------------- ### Tuning Bayesian Dynamic Survival Models (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-autoplot.md This code snippet demonstrates tuning a Bayesian dynamic survival model using the `tune_bayes` function. It specifies the model formula, resamples, number of iterations, metrics, evaluation times, control parameters, and initial grid. A warning indicates that only the first evaluation time will be used. ```r set.seed(2193) bayes_dynamic_res <- mod_spec %>% tune_bayes(event_time ~ X1 + X2, resamples = sim_rs, iter = 2, metrics = dyn_mtrc, eval_time = time_points, control = bctrl, initial = grid_dynamic_res) ``` -------------------------------- ### Requesting class predictions with non-classification glmnet model Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/glmnet-linear.md This example shows an error when `multi_predict()` is used to obtain class predictions from a model object that is not a classification model. The error message explicitly states that for class predictions, the model object should be a classification model. ```R linear_reg(penalty = 0.01) %>% set_engine("glmnet") %>% fit(mpg ~ ., data = mtcars) %>% multi_predict(mtcars, type = "class") ``` -------------------------------- ### XGBoost Case Weights with xgboost::xgb.train Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-case-weights.md Demonstrates the usage of case weights in xgboost models via the xgb.train function. It shows the parameters passed to the function, including the case weights. ```r print(attr(wt_fit$fit, "call")) ``` -------------------------------- ### Get Plot Labels for Integrated Autoplot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Retrieves the labeling information from an autoplot associated with integrated metrics. This function helps in deciphering the plot's components, including axis labels and aesthetic mappings. It takes a ggplot object. ```r ggplot2::get_labs(int_autoplot) ``` -------------------------------- ### Demonstrate Error: Varying Non-Varying Args in Recipe Steps Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/recipes-varying.md This code snippet demonstrates the error that occurs when attempting to use `varying()` on a non-varying argument within a tidymodels recipe. The error arises because certain arguments, such as 'skip' for 'step_type' steps, are explicitly not allowed to vary. ```r varying_args(rec_bad_varying) # Condition # Error in `map()`: # i In index: 1. # Caused by error in `map2()`: # i In index: 5. # i With name: skip. # Caused by error in `.f()`: # ! The argument skip for a recipe step of type "step_type" is not allowed to vary. ``` -------------------------------- ### Select Model by Percent Loss for Dynamic Survival Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md This snippet demonstrates selecting the best model based on percent loss for dynamic survival metrics using `select_by_pct_loss`. It requires the grid results, the metric, the tuning parameter (penalty), and the evaluation time. ```r select_by_pct_loss(grid_dynamic_res, metric = "brier_survival", penalty, eval_time = 10) # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` -------------------------------- ### Get Plot Labels for Static Race Plot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Retrieves and displays the labels for a static race plot generated by ggplot2. This function is useful for understanding the aesthetic mappings of the plot, such as axes and grouping variables. It takes a ggplot object as input. ```r ggplot2::get_labs(stc_race_plot) ``` -------------------------------- ### Select Best Model for Dynamic Survival Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md This snippet demonstrates selecting the best model from a grid search for survival models with dynamic metrics using `select_best`. It shows default behavior when metrics and evaluation times are not specified, and how to specify them. Errors may occur if the specified metric is not available or if the evaluation time is invalid. ```r select_best(grid_dynamic_res) # Warning in `select_best()`: # No value of `metric` was given; "brier_survival" will be used. # Warning in `select_best()`: # 4 evaluation times are available; the first will be used (i.e. `eval_time = 10`). # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_dynamic_res, metric = "brier_survival", eval_time = 10) # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_dynamic_res, metric = "brier_survival", eval_time = c(5, 10)) # Warning in `select_best()`: # 2 evaluation times are available; the first will be used (i.e. `eval_time = 5`). # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_dynamic_res, metric = "brier_survival_integrated", eval_time = 10) # Error in `select_best()`: # ! "brier_survival_integrated" was not in the metric set. Please choose from: "brier_survival". ``` ```r select_best(grid_dynamic_res, metric = "brier_survival", eval_time = 0) # Error in `select_best()`: # ! Evaluation time 0 is not in the results. ``` -------------------------------- ### Get Plot Labels for Dynamic Race Plot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Fetches and displays the labels for a dynamic race plot. This function is used to inspect the titles and labels associated with plot aesthetics like axes and grouping, generated by ggplot2. It requires a ggplot object. ```r ggplot2::get_labs(dyn_race_plot) ``` -------------------------------- ### Case Weight Helpers for Classification and Regression in R Source: https://context7.com/tidymodels/extratests/llms.txt Provides helper functions for testing importance weights and frequency weights in classification and regression models. It demonstrates setting up datasets with weights and comparing model fits with and without weights or using subsets. ```r # Load helper functions source("tests/testthat/helper-case-weights.R") library(parsnip) library(modeldata) # Generate binary classification data with importance weights two_class_setup <- make_two_class_wts() wts <- two_class_setup$wts two_class_subset <- two_class_setup$subset # Use with model fitting spec <- logistic_reg() %>% set_engine("glm") # Fit with importance weights (should match subset fit) # fit_weighted <- fit( # spec, # Class ~ A + B, # data = two_class_dat, # case_weights = wts # ) # fit_subset <- fit(spec, Class ~ A + B, data = two_class_subset) # Predictions should be equivalent # pred_weighted <- predict(fit_weighted, two_class_subset, type = "prob") # pred_subset <- predict(fit_subset, two_class_subset, type = "prob") # Generate regression data with frequency weights # ames_setup <- make_ames_wts() # wts_freq <- ames_setup$wts # ames_subset <- ames_setup$subset # Frequency weights for count-based weighting spec_lm <- linear_reg() %>% set_engine("lm") # fit_freq <- fit( # spec_lm, # Sale_Price ~ Longitude + Latitude, # data = ames_setup$full, # case_weights = wts_freq # ) ``` -------------------------------- ### Create Decision Tree Model Specification (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md Demonstrates how to create a basic decision tree model specification using the `decision_tree()` function in R. This function initializes a decision tree model with a default engine and an unknown mode. ```r decision_tree() ``` -------------------------------- ### Get Plot Labels for Integrated Race Plot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Extracts and presents the labels from a ggplot object representing an integrated race plot. This aids in understanding how variables are mapped to plot aesthetics, such as axes, and grouping. The function expects a ggplot object as input. ```r ggplot2::get_labs(int_race_plot) ``` -------------------------------- ### Catalog Summary Output for Tuning Results Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/tune-catalog-finetune.md This snippet shows the output of `catalog_summary_test`, which is used to summarize tuning results from tidymodels. It displays a count or identifier for specific tuning iterations or results, presented in a simple key-value format. ```r catalog_summary_test ``` -------------------------------- ### Fit Resamples with Static Survival Metrics Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-workflow-set.md This code snippet demonstrates fitting resampled survival models using a predefined workflow set (`wflow_set`), static metrics (`stc_mtrc`), and specified resamples (`sim_rs`). It also includes control parameters (`ctrl`) and a seed for reproducibility. Note that `eval_time` is provided but will generate warnings as it's not used for static metrics. ```r wflow_set_fit_stc_eval <- workflow_map(wflow_set, "fit_resamples", seed = 2193, resamples = sim_rs, metrics = stc_mtrc, eval_time = time_points, control = ctrl) ``` -------------------------------- ### Bayesian Tuning Survival Models with Mixed Metrics in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-bayes.md This R code demonstrates Bayesian tuning for survival models with a mixture of metric types. It uses `tune_bayes` with parameters including resampling, iterations, mixed metrics, evaluation times, initial grid, and control settings. The `show_best` function is used to display the top-performing models based on specified metrics and evaluation times, highlighting potential issues with incorrect evaluation time specifications. ```r set.seed(2193) bayes_mixed_res <- mod_spec %>% tune_bayes(event_time ~ X1 + X2, resamples = sim_rs, iter = 2, metrics = mix_mtrc, eval_time = time_points, initial = init_grid_mixed_res, control = bctrl) ``` ```r expect_snapshot_plot(print(autoplot(bayes_mixed_res)), "mix-bayes-0-times") ``` ```r show_best(bayes_mixed_res, metric = "brier_survival") ``` ```r show_best(bayes_mixed_res, metric = "brier_survival", eval_time = 1) ``` ```r show_best(bayes_mixed_res, metric = "brier_survival", eval_time = c(1.1)) ``` ```r show_best(bayes_mixed_res, metric = "brier_survival", eval_time = c(1, 3)) ``` ```r show_best(bayes_mixed_res, metric = "brier_survival_integrated") ``` -------------------------------- ### Display Best Survival Model Results (Mixed Metrics) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-sa.md Displays the best performing survival models based on specified metrics and evaluation times from a simulated annealing tuning process with mixed metrics. It can show the best models for a specific metric across all evaluation times, or for a specific evaluation time. Errors can occur if the specified evaluation time is not found in the results. ```r show_best(sa_mixed_res, metric = "brier_survival") ``` ```r show_best(sa_mixed_res, metric = "brier_survival", eval_time = 1) ``` ```r show_best(sa_mixed_res, metric = "brier_survival", eval_time = c(1.1)) ``` ```r show_best(sa_mixed_res, metric = "brier_survival", eval_time = c(1, 3)) ``` ```r show_best(sa_mixed_res, metric = "brier_survival_integrated") ``` -------------------------------- ### Select Best Model for Mixed Survival Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md This snippet illustrates selecting the best model from a grid search with mixed metric types using `select_best`. It covers default behavior and specifying metrics and evaluation times, highlighting differences for non-dynamic metrics. Errors related to invalid evaluation times can occur. ```r select_best(grid_mixed_res) # Warning in `select_best()`: # No value of `metric` was given; "brier_survival" will be used. # Warning in `select_best()`: # 4 evaluation times are available; the first will be used (i.e. `eval_time = 10`). # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_mixed_res, metric = "brier_survival", eval_time = 10) # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_mixed_res, metric = "brier_survival_integrated", eval_time = 0) # Warning in `select_best()`: # `eval_time` is only used for dynamic survival metrics. # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` ```r select_best(grid_mixed_res, metric = "brier_survival", eval_time = 0) # Error in `select_best()`: # ! Evaluation time 0 is not in the results. ``` -------------------------------- ### Get Plot Labels for Static Autoplot in R Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune_race_anova.md Retrieves and displays the labels for a static autoplot, likely generated from a model object. This helps in interpreting the plot's components, including the x and y axes, and any aesthetic mappings like color or size. It operates on a ggplot object. ```r ggplot2::get_labs(stc_autoplot) ``` -------------------------------- ### Augment Survival Model with eval_time Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/workflows-survival-augment.md Demonstrates how to use the `augment()` function with the `eval_time` argument for survival models. This argument is crucial for calculating survival probabilities and is required when default values are not present. It takes new data as input. ```r augment(wflow_fit, new_data = sim_dat, eval_time = c(1, 5, 10)) ``` -------------------------------- ### Fit Resamples for Survival Models (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-fit-resamples.md This code snippet demonstrates how to fit resamples for survival models using `fit_resamples` from the tidymodels package. It specifies the model formula, resamples, metrics, control settings, and evaluation time points. A warning may occur if `eval_time` is used with static survival metrics. ```r set.seed(2193) rs_static_res <- mod_spec %>% fit_resamples(event_time ~ X1 + X2, resamples = sim_rs, metrics = stc_mtrc, control = rsctrl, eval_time = time_points) ``` -------------------------------- ### Select Model by Percent Loss for Mixed Survival Metrics (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md This snippet shows how to select the best model based on percent loss for mixed survival metrics using `select_by_pct_loss`. It requires the grid results, the metric, the tuning parameter (penalty), and the evaluation time. ```r select_by_pct_loss(grid_mixed_res, metric = "brier_survival", penalty, eval_time = 10) # A tibble: 1 x 2 penalty .config 1 0.01 pre0_mod01_post0 ``` -------------------------------- ### Tune Sim Anneal with Eval Time Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-eval-time-attribute.md Illustrates using tune_sim_anneal with the eval_time argument. This function tunes a model using simulated annealing. It requires a model specification, data, an initial grid, the number of iterations, metrics, evaluation time points, and optional control parameters. A warning may occur if multiple evaluation times are provided. ```R set.seed(2193) sa_res <- mod_spec %>% tune_sim_anneal(event_time ~ X1 + X2, sim_rs, initial = grid_res, iter = 2, metrics = srv_mtrc, eval_time = time_points, control = control_sim_anneal( verbose_iter = FALSE)) ``` -------------------------------- ### Tune Bayesian Survival Model with Mixed Metrics Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-autoplot.md Tunes a Bayesian survival model using a grid of hyperparameter values and mixed metrics. The tuning process involves specifying the model formula, resamples, iterations, metrics, evaluation times, initial grid, and control parameters. A warning is issued if the number of evaluation times provided does not match expectations. ```r set.seed(2193) bayes_mixed_res <- mod_spec %>% tune_bayes(event_time ~ X1 + X2, resamples = sim_rs, iter = 2, metrics = mix_mtrc, eval_time = time_points, initial = grid_mixed_res, control = bctrl) ``` -------------------------------- ### Visualize Parameters of Bayesian Survival Model Tuning Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-autoplot.md Generates a plot of the parameters from a tuned Bayesian survival model. The `autoplot` function is used with `type = 'parameters'`, and a specific evaluation time is provided. A warning is issued indicating that `eval_time` is not used when plotting parameters. ```r mix_mult_param <- autoplot(bayes_mixed_res, type = "parameters", eval_time = c( 10, 15)) ``` -------------------------------- ### Predicting with multiple penalty values using multi_predict() Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/glmnet-logistic.md This example shows how to predict using multiple penalty values with a fitted logistic regression model. It utilizes the multi_predict() function to handle multiple predictions per row of data when a range of penalties is provided. The glmnet engine requires a single penalty value for direct `predict()` calls, necessitating multi_predict() for multiple values. ```R logistic_reg(penalty = 0.01) %>% set_engine("glmnet") %>% fit(Class ~ log(funded_amnt) + int_rate + term, data = lending_club) %>% predict(lending_club, penalty = 0:1) ``` -------------------------------- ### Configure Bagged Tree for Censored Regression with RPART Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md This R code successfully configures a bagged decision tree model for censored regression using the 'rpart' engine, assuming the necessary extensions are available. ```r bag_tree() %>% set_mode("censored regression") %>% set_engine("rpart") ``` -------------------------------- ### Tune Survival Model with Simulated Annealing (Dynamic Metrics) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-sa.md Tunes a survival model specification using simulated annealing with dynamic metrics. It requires a model specification, resamples, iteration count, parameter information, dynamic metrics, evaluation times, control parameters, and an initial grid. This function may issue warnings about the number of available evaluation times. ```r set.seed(2193) sa_dynamic_res <- mod_spec %>% tune_sim_anneal(event_time ~ X1 + X2, resamples = sim_rs, iter = 2, param_info = mod_param, metrics = dyn_mtrc, eval_time = time_points, control = sctrl, initial = init_grid_dynamic_res) ``` -------------------------------- ### Select model by percent loss with static survival metric Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md Selects the best model based on the percent loss criterion using the 'concordance_survival' metric and the 'penalty' variable from static results. This is another method for conservative model selection. ```R select_by_pct_loss(grid_static_res, metric = "concordance_survival", penalty) ``` -------------------------------- ### Extracting Labels for Static Survival Model Plots (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-autoplot.md This snippet demonstrates how to extract plot labels for survival models evaluated with static metrics. It shows the labels for plots visualizing marginal effects, performance over iterations, and parameter values. These functions are useful for understanding the axes and legends of generated plots. ```r ggplot2::get_labs(stc_marginal) ggplot2::get_labs(stc_perf) ggplot2::get_labs(stc_param) ``` -------------------------------- ### Configure Bagged Tree with C5.0 Engine (Unknown Mode) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md This R snippet configures a bagged tree model using the 'C5.0' engine without specifying a mode. The output shows the default model specification for an unknown mode. ```r bag_tree() %>% set_engine("C5.0") ``` -------------------------------- ### Select model by percent loss with integrated survival metric Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md Selects the best model using the percent loss criterion with the 'brier_survival_integrated' metric and the 'penalty' variable from integrated results. ```R select_by_pct_loss(grid_integrated_res, metric = "brier_survival_integrated", penalty) ``` -------------------------------- ### Plot Dynamic Grid Tuning Results (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-grid.md Generates a plot visualizing the results of a dynamic grid search for survival model tuning. This function is part of the tidymodels ecosystem and likely relies on internal data structures created during the tuning process. It outputs a plot object. ```r expect_snapshot_plot(print(autoplot(grid_dynamic_res)), "dyn-grid") ``` -------------------------------- ### GRF Quantile Regression Random Forest Model Specification Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/grf.md This snippet demonstrates how to configure a random forest model for quantile regression using the 'grf' engine. It shows the translation process and the generated fit template, including the specification of quantile levels. A message indicates the active quantile levels used. ```r translate(set_mode(set_engine(rand_forest(mtry = 100, trees = 1, min_n = 1000), "grf"), "quantile regression", quantile_levels = (1:3) / 4)) ``` -------------------------------- ### Tune Race ANOVA with Interactive Logger Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/tune-catalog-finetune.md This code snippet demonstrates the use of `tune_race_anova` from the tidymodels package. It integrates a custom control function that raises warnings and errors, showcasing how the interactive logger captures these messages during the tuning process. The function uses kknn regression and vfold_cv for cross-validation. ```r res_anova <- tune_race_anova(parsnip::nearest_neighbor("regression", "kknn", neighbors = tune()), Sale_Price ~ ., rsample::vfold_cv(modeldata::ames[, c(72, 40:45)], 5), control = control_race(extract = function(x) { raise_warning() raise_error() })) ``` -------------------------------- ### Configure Bagged Tree with RPART Engine (Unknown Mode) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md This R code configures a bagged tree model using the 'rpart' engine without specifying the mode. Parsnip informs the user about the required 'censored' and 'baguette' extension packages. ```r bag_tree() %>% set_engine("rpart") ``` -------------------------------- ### Plot Survival Model Tuning Results (Dynamic Metrics) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-sa.md Generates a plot visualizing the results of survival model tuning using simulated annealing with dynamic metrics. The `autoplot` function is used for this purpose. It might produce warnings if specific parameters are not used. ```r expect_snapshot_plot(print(autoplot(sa_dynamic_res)), "dyn-sa") ``` ```r expect_snapshot_plot(print(autoplot(sa_dynamic_res, eval_time = 1, type = "parameters")), "dyn-sa-param") ``` -------------------------------- ### Select best model with static survival metric (concordance_survival) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-select.md Selects the best model from a static cross-validation results grid using the 'concordance_survival' metric. If no metric is specified, it defaults to 'concordance_survival'. Demonstrates that 'eval_time' is not applicable for static metrics. ```R select_best(grid_static_res) select_best(grid_static_res, metric = "concordance_survival") select_best(grid_static_res, metric = "concordance_survival", eval_time = 0) ``` -------------------------------- ### Tune Race Win-Loss with Eval Time Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-eval-time-attribute.md Demonstrates tune_race_win_loss with the eval_time argument. This function tunes a model using the race-based win-loss method. It requires a model specification, data, a grid, metrics, and evaluation time points. A warning may occur if multiple evaluation times are provided. ```R set.seed(2193) wl_res <- mod_spec %>% tune_race_win_loss(event_time ~ X1 + X2, sim_rs, grid = grid, metrics = srv_mtrc, eval_time = time_points) ``` -------------------------------- ### Tune Bayes with Eval Time Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/survival-tune-eval-time-attribute.md Demonstrates using tune_bayes with the eval_time argument. This function tunes a model using Bayesian optimization. It requires a model specification, data, an initial grid, the number of iterations, metrics, and evaluation time points. A warning may occur if multiple evaluation times are provided. ```R set.seed(2193) bayes_res <- mod_spec %>% tune_bayes(event_time ~ X1 + X2, sim_rs, initial = grid_res, iter = 2, metrics = srv_mtrc, eval_time = time_points) ``` -------------------------------- ### Create Bagged Tree Model Specification (R) Source: https://github.com/tidymodels/extratests/blob/main/tests/testthat/_snaps/parsnip-extension-messaging.md Displays a pre-defined 'bt' object representing a Bagged Decision Tree Model Specification in R. This specification is for regression and uses the 'rpart' engine with specific main arguments. ```r bt ```