### Install reprex and sessioninfo packages in R Source: https://github.com/tidymodels/parsnip/blob/main/issue_template.md This code snippet installs the 'reprex' and 'sessioninfo' R packages, which are recommended for creating reproducible examples and gathering session information when reporting issues. Ensure you have a stable internet connection as it downloads packages from CRAN. ```r install.packages(c("reprex", "sessioninfo"), repos = "http://cran.r-project.org") ``` -------------------------------- ### Set engine for parsnip models Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/args_and_modes.md Illustrates how to set the computational engine for a parsnip model. It shows examples of valid and invalid engine specifications, including cases where the engine does not support the model's mode. ```r set_engine(set_mode(decision_tree(), "regression"), "C5.0") ``` ```r decision_tree(mode = "regression", engine = "C5.0") ``` ```r set_engine(set_mode(decision_tree(engine = NULL), "regression"), "C5.0") ``` ```r set_engine(set_mode(decision_tree(), "regression"), "C5.0") ``` ```r linear_reg(engine = "boop") ``` ```r set_engine(linear_reg()) ``` ```r set_engine(proportional_hazards()) ``` ```r set_engine(bag_tree, "rpart") ``` -------------------------------- ### Identify Tunable Parameters for linear_reg with 'quantreg' Engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md This example demonstrates calling `tunable()` on a `linear_reg()` model specification with the 'quantreg' engine set. The output indicates that there are no tunable parameters identified for this specific engine and model combination. ```r tunable(set_engine(linear_reg(), "quantreg")) ``` -------------------------------- ### Set arguments for parsnip models Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/args_and_modes.md Demonstrates how to set arguments for parsnip models. This function requires at least one named argument to be passed. ```r set_args(rand_forest()) ``` -------------------------------- ### Identify Tunables for Logistic Regression with Brulee Engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md This example shows how to use 'tunable' to find hyperparameters for 'logistic_reg()' when the 'brulee' engine is specified. It lists nine tunable parameters, including 'epochs', 'penalty', 'mixture', and others related to the engine's configuration. ```r tunable(set_engine(logistic_reg(), "brulee")) ``` -------------------------------- ### Load Parsnip Engine Implementations Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/fit_interfaces.md Addresses errors that occur when parsnip cannot find an implementation for a specified model or engine. This requires installing and loading the relevant parsnip extension packages. ```r ! parsnip could not locate an implementation for `cubist_rules` model specifications. i The parsnip extension package rules implements support for this specification. i Please install (if needed) and load to continue. ``` ```r ! parsnip could not locate an implementation for `poisson_reg` model specifications. i The parsnip extension packages multilevelmod, poissonreg, and agua implement support for this specification. i Please install (if needed) and load to continue. ``` ```r ! parsnip could not locate an implementation for `cubist_rules` model specifications using the `Cubist` engine. i The parsnip extension package rules implements support for this specification. i Please install (if needed) and load to continue. ``` -------------------------------- ### Identify Tunables for Logistic Regression with Keras Engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md This example demonstrates how to use 'tunable' to discover hyperparameters for 'logistic_reg()' when the 'keras' engine is selected. It indicates that only 'penalty' is identified as a tunable parameter for this combination. ```r tunable(set_engine(logistic_reg(), "keras")) ``` -------------------------------- ### Generate a reproducible example (reprex) in R Source: https://github.com/tidymodels/parsnip/blob/main/issue_template.md This command uses the 'reprex' package to generate a minimal reproducible example of your R code. It automatically includes session information, which is crucial for debugging and understanding the context of the issue. Run this in your R console after copying your problematic code. ```r reprex::reprex(si = TRUE) ``` -------------------------------- ### Execute Null Model Fit with Error (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/nullmodel.md Shows an example of attempting to fit a null model that results in an error due to a 'term' object not being found, likely indicating an issue with the provided formula or data. ```r res <- fit(set_engine(null_model(mode = "regression"), "parsnip"), hpc_bad_form, data = hpc) ``` -------------------------------- ### Set mode for parsnip models Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/args_and_modes.md Shows how to set the operational mode for a parsnip model. It highlights valid modes and demonstrates errors when an invalid mode is provided. ```r set_mode(rand_forest()) ``` ```r set_mode(rand_forest(), 2) ``` ```r set_mode(rand_forest(), "haberdashery") ``` ```r set_mode(linear_reg(), "classification") ``` ```r set_mode(set_engine(decision_tree(), "C5.0"), "regression") ``` ```r set_mode(set_engine(decision_tree(engine = NULL), "C5.0"), "regression") ``` ```r set_mode(proportional_hazards(), "regression") ``` ```r set_mode(linear_reg()) ``` ```r set_mode(bag_tree, "classification") ``` ```r set_mode(bag_tree, "classification") ``` -------------------------------- ### Print Null Model Specification (Classification) (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/nullmodel.md Demonstrates how to print a null model specification for classification. The output shows the model mode and the default computational engine. ```r print(null_model(mode = "classification")) ``` -------------------------------- ### Handle non-existent engine in show_engines Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Demonstrates the error message when `show_engines` is called with a model function name that does not exist or has no registered engines. ```R show_engines("linear_re") ``` -------------------------------- ### Identify Tunable Parameters for linear_reg with 'brulee' Engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md This example shows how to use `tunable()` with a `linear_reg()` model specification that has the 'brulee' engine set. It lists various tunable parameters such as 'epochs', 'penalty', 'mixture', 'learn_rate', 'momentum', 'batch_size', 'stop_iter', and 'rate_schedule', indicating these are available for hyperparameter tuning. ```r tunable(set_engine(linear_reg(), "brulee")) ``` -------------------------------- ### Fit linear regression model with lm engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Example of fitting a linear regression model using the 'lm' engine. It demonstrates predicting with new data and handling potential rank-deficient fits. ```R preds <- predict(fit(linear_reg(), y ~ ., data = data), new_data = data2) ``` -------------------------------- ### Update linear_reg model with tune() Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Demonstrates how to update a linear regression model specification using tune() for mixture and nlambda parameters. This allows for hyperparameter tuning. ```R update(set_engine(linear_reg(mixture = 0), "glmnet", nlambda = 10), mixture = tune(), nlambda = tune()) ``` -------------------------------- ### Handle NULL engine for translate Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Demonstrates error handling when the engine argument is set to NULL for the translate function. An engine must be specified for translation. ```R translate(linear_reg(), engine = NULL) ``` -------------------------------- ### Translate Arguments for LiblineaR Engine (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the LiblineaR engine using `set_engine`. Demonstrates how `translate_args` handles missing arguments and tuning parameters like 'type'. ```r translate_args(set_engine(mixture_v, "LiblineaR")) ``` -------------------------------- ### Translate Null Model with Unsupported Engine (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/nullmodel.md Illustrates an error that occurs when specifying an unsupported engine for a null model. The error message suggests checking available engines using `show_engines()`. ```r translate(set_engine(null_model(), "wat?")) ``` -------------------------------- ### Handle unsupported engine for translate Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Shows error handling when an unsupported engine is specified for the translate function. The engine 'wat?' is not recognized by the linear_reg model. ```R translate(linear_reg(), engine = "wat?") ``` -------------------------------- ### Translate Arguments for Spark Engine (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the Spark engine using `set_engine`. Illustrates argument handling for Spark, including missing arguments and tuning parameters like 'elastic_net_param'. ```r translate_args(set_engine(mixture_v, "spark")) ``` -------------------------------- ### Translate Arguments for earth Engine with prune_method (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the earth engine when specifying the pruning method (`pmethod`). Shows how `translate_args` handles tuning parameters for pruning strategies. ```r translate_args(set_engine(prune_method_v, "earth")) ``` -------------------------------- ### Translate Arguments for trees with C5.0 Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for a tree-based model using the 'C5.0' engine. Shows how 'trials' are handled as a tunable parameter. This function is part of the parsnip package for argument translation. ```R translate_args(set_engine(trees, "C5.0")) ``` -------------------------------- ### Translate Arguments for earth Engine with prod_degree (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the earth engine when specifying the product degree (`degree`). Demonstrates argument translation for specific earth model configurations. ```r translate_args(set_engine(prod_degree, "earth")) ``` -------------------------------- ### Translate Arguments for basic_reg with rpart Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for a basic regression model using the 'rpart' engine. Illustrates how 'model' can be set as a tunable parameter. This function is part of the parsnip package. ```R translate_args(set_engine(basic_reg, "rpart", model = TRUE)) ``` -------------------------------- ### Translate Arguments for cost_complexity with rpart Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for a cost complexity parameter in a model using the 'rpart' engine. Demonstrates how 'cp' is handled as a tunable parameter. This function is part of the parsnip package. ```R translate_args(set_engine(cost_complexity, "rpart")) ``` -------------------------------- ### Translate Null Model with Missing Engine (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/nullmodel.md Demonstrates an error when attempting to translate a null model specification without specifying an engine. The error message indicates the allowed mode/engine combinations. ```r translate(set_engine(null_model(mode = "regression"))) ``` -------------------------------- ### Informative Messages for Unknown Model Implementations (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/misc.md Details how parsnip provides informative messages when a model specification (like bag_tree) is used with an unknown engine or mode. It guides the user on installing and loading necessary extension packages (e.g., 'baguette', 'censored'). ```R set_mode(set_engine(bag_tree(), "rpart"), "regression") ``` ```R set_mode(bag_tree(), "censored regression") ``` ```R bag_tree() ``` ```R set_engine(bag_tree(), "rpart") ``` ```R bt ``` -------------------------------- ### Set Spark Engine Arguments for Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Shows how to set arguments for the 'spark' engine in parsnip, which integrates with Apache Spark. Demonstrates configuration for `min_info_gain`, `feature_subset_strategy`, `num_trees`, and `min_instances_per_node`. `min_info_gain` is the minimum impurity decrease for split, `feature_subset_strategy` selects features, `num_trees` is the number of trees, and `min_instances_per_node` is the minimum number of instances per leaf node. ```r translate_args(set_engine(basic, "spark", min_info_gain = 2)) ``` ```r translate_args(set_engine(mtry, "spark")) ``` ```r translate_args(set_engine(trees, "spark")) ``` ```r translate_args(set_engine(min_n, "spark")) ``` -------------------------------- ### Handling Invalid Prediction Types in Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/registration.md This example demonstrates an error resulting from an invalid prediction type ('eggs') being specified for the `set_pred()` function. Parsnip has a predefined set of supported prediction types, and any deviation will lead to an error. The error message helpfully lists the acceptable prediction types, guiding the user to correct their input. ```r set_pred(model = "sponge", eng = "gum", mode = "classification", type = "eggs", value = class_vals) ``` -------------------------------- ### Install parsnip Package (R) Source: https://github.com/tidymodels/parsnip/blob/main/README.md Demonstrates how to install the parsnip R package. It can be installed as part of the tidymodels meta-package, directly from CRAN, or from its GitHub repository using the 'pak' package manager. ```R install.packages("tidymodels") # Alternatively, install just parsnip: install.packages("parsnip") # Or the development version from GitHub: # install.packages("pak") pak::pak("tidymodels/parsnip") ``` -------------------------------- ### Install MixOmics Package (R) Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/pls_mixOmics.md Provides R code to install the 'mixOmics' package, which is required for the 'pls' model engine in parsnip. This package is available via the Bioconductor repository. ```r if (!require("remotes", quietly = TRUE)) { install.packages("remotes") } remotes::install_bioc("mixOmics") ``` -------------------------------- ### Translate Basic Arguments in Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Shows the translation of basic arguments for a model in parsnip using `translate_args`. When called with no specific engine or parameters, it returns an empty list, indicating no arguments to translate. ```r translate_args(basic) ``` -------------------------------- ### Print C5.0 Rules Model Specification (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/model_basics.md Shows how to print the specification for a C5.0 rules model. It notes that the `rules` package is required and specifies the computational engine as `C5.0`. ```r print(C5_rules()) ``` -------------------------------- ### Set Flexsurv Engine Arguments for Survival Regression in Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Demonstrates setting arguments for the 'flexsurv' engine in parsnip for survival regression models. Covers basic arguments and specific parameters like `cl` (confidence level) and `dist` (distribution). The `dist` parameter allows specifying survival distributions such as 'lnorm' (log-normal). ```r translate_args(set_engine(basic, "flexsurv")) ``` ```r translate_args(set_engine(basic, "flexsurv", cl = 0.99)) ``` ```r translate_args(set_engine(normal, "flexsurv")) ``` ```r translate_args(set_engine(dist_v, "flexsurv")) ``` -------------------------------- ### Translate Arguments for decision_tree with rpart Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the decision_tree model using the 'rpart' engine. This example shows how formula, data, and weights are handled as missing arguments. This function is part of the parsnip package. ```R translate_args(set_engine(basic_class, "rpart")) ``` -------------------------------- ### Configure Model Engines and Arguments (R) Source: https://context7.com/tidymodels/parsnip/llms.txt Demonstrates setting computational engines for parsnip models and passing engine-specific arguments for fine-tuned control. It also shows how to switch between different engines for the same model type. ```r # Set engine with additional arguments rf_spec <- rand_forest(trees = 1000) %>% set_engine("ranger", importance = "permutation", num.threads = 4, verbose = FALSE) %>% set_mode("regression") # View the model specification print(rf_spec) # Output shows main arguments and engine-specific arguments # Switch engines easily rf_randomforest <- rand_forest(trees = 1000) %>% set_engine("randomForest", importance = TRUE) %>% set_mode("regression") # Decision tree with engine-specific parameters dt_spec <- decision_tree(tree_depth = 10, min_n = 20) %>% set_engine("rpart", parms = list(prior = c(0.65, 0.35)), control = rpart::rpart.control(cp = 0.01)) %>% set_mode("classification") # Boosted tree with xgboost boost_spec <- boost_tree(trees = 100, tree_depth = 6, learn_rate = 0.3) %>% set_engine("xgboost", objective = "reg:squarederror", nthread = 2) %>% set_mode("regression") ``` -------------------------------- ### Translate kknn Arguments with Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Demonstrates the translation of arguments for the 'kknn' engine using parsnip's translate_args function. It shows how different input settings for parameters like 'ks', 'kernel', 'scale', and 'distance' are processed and represented. ```R translate_args(set_engine(basic, "kknn")) ``` ```R translate_args(set_engine(neighbors, "kknn")) ``` ```R translate_args(set_engine(neighbors, "kknn", scale = FALSE)) ``` ```R translate_args(set_engine(weight_func, "kknn")) ``` ```R translate_args(set_engine(dist_power, "kknn")) ``` -------------------------------- ### Set randomForest Engine Arguments for Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Demonstrates setting arguments for the 'randomForest' engine in parsnip. Shows how to translate arguments for `norm.votes` and `mtry` parameters. The `norm.votes` argument controls whether normalized vote counts are used, and `mtry` specifies the number of variables randomly sampled as candidates at each split. ```r translate_args(set_engine(basic, "randomForest", norm.votes = FALSE)) ``` ```r translate_args(set_engine(mtry, "randomForest")) ``` ```r translate_args(set_engine(trees, "randomForest")) ``` -------------------------------- ### Handle Classification Outcome Error with `glm` and Caught Control Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md Similar to the previous example, this shows an error during the `fit` process with a classification model when the outcome is not a factor. This error occurs even when using a specific control object (`caught_ctrl`). ```r glm_form_catch <- fit(lc_basic, funded_amnt ~ term, data = lending_club, control = caught_ctrl) ``` -------------------------------- ### Test Control Object Coercion in Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/condense_control.md Shows the `control_test` function being used with a control object 'ctrl'. This example also results in an error, specifically a coercion error where a control object cannot be converted to itself, implying issues with argument availability. ```r control_test(ctrl) ``` -------------------------------- ### Print Naive Bayes Model Specification (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/model_basics.md Illustrates printing the specification for a Naive Bayes model. This example shows a message indicating that parsnip could not locate an implementation, suggesting the need for an extension package. ```r print(naive_Bayes()) ``` -------------------------------- ### Translate Proportional Hazards Arguments with Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Shows the translation of arguments for a proportional hazards model using parsnip's translate_args. This snippet focuses on the basic argument translation and includes an example of an error condition related to the 'penalty' argument for the 'glmnet' engine. ```R translate_args(basic) ``` ```R translate_args(basic_incomplete) ``` -------------------------------- ### Translate Arguments for basic_reg with xgboost Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Demonstrates argument translation for a basic regression model with the 'xgboost' engine. Illustrates setting parameters like 'print_every_n' and shows default values for 'nthread' and 'verbose'. This function is part of the parsnip package. ```R translate_args(set_engine(basic_reg, "xgboost", print_every_n = 10L)) ``` -------------------------------- ### Condense Control with Parsnip Control Object Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/condense_control.md Demonstrates the use of `condense_control` with a `control_parsnip` object and another control object 'ctrl'. This snippet illustrates a scenario that leads to an error, indicating missing arguments within the control objects. ```r condense_control(control_parsnip(), ctrl) ``` -------------------------------- ### Get prediction types for poisson_reg/zeroinfl (R) Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/poisson_reg_zeroinfl.md Retrieves the available prediction types for the 'poisson_reg' model with the 'zeroinfl' engine. ```r parsnip:::get_from_env("poisson_reg_predict") | dplyr::filter(engine == "zeroinfl") | dplyr::select(mode, type) ``` -------------------------------- ### Translate Parsnip LightGBM Regression to Bonsai Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/boost_tree_lightgbm.md This code snippet demonstrates how to translate a parsnip 'boost_tree' model specification with the 'lightgbm' engine and 'regression' mode into its equivalent 'bonsai' package call. It shows the default tuning parameters and the template for the underlying bonsai function. ```r boost_tree( mtry = integer(), trees = integer(), tree_depth = integer(), learn_rate = numeric(), min_n = integer(), loss_reduction = numeric() ) |> set_engine("lightgbm") | set_mode("regression") | translate() ``` -------------------------------- ### Predict with new_data instead of newdata Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Shows the correct usage of `new_data` for predictions, highlighting that `newdata` is deprecated and will result in an error. ```R predict(res_xy, newdata = hpc[1:3, num_pred]) ``` -------------------------------- ### Fit Survival Model with Parsnip (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/surv_reg_flexsurv.md This snippet shows how to fit a basic survival model using the `fit_xy` function from the parsnip package. It expects a formula interface for survival models, and an error occurs if this is not provided. The input consists of survival data and predictor variables. ```R res <- fit_xy(surv_basic, x = lung[, "age", drop = FALSE], y = lung$time, control = ctrl) # Condition # Error in `fit_xy()`: # ! Survival models must use the formula interface. ``` -------------------------------- ### Translate spark arguments for logistic_reg (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the 'spark' engine. X, formula, and weights are missing arguments, and the family is set to binomial. ```r translate_args(set_engine(basic, "spark")) ``` -------------------------------- ### Print Boosted Tree Model Specification (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/model_basics.md Demonstrates printing the specification for a boosted tree model. The default computational engine is `xgboost`. ```r print(boost_tree()) ``` -------------------------------- ### R Regression Workflow with Random Forest Source: https://context7.com/tidymodels/parsnip/llms.txt An end-to-end example in R demonstrating data preparation (splitting, feature engineering), specifying a random forest model for regression, fitting the model, generating predictions on test data, and displaying results. ```r library(parsnip) library(dplyr) # Prepare data set.seed(123) mtcars_split <- mtcars %>% mutate( id = row_number(), high_mpg = factor(ifelse(mpg > median(mpg), "high", "low")) ) train_data <- mtcars_split %>% filter(id <= 24) test_data <- mtcars_split %>% filter(id > 24) # Regression workflow reg_spec <- rand_forest(mtry = 3, trees = 1000) %>% set_engine("ranger", importance = "impurity") %>% set_mode("regression") reg_fit <- reg_spec %>% fit(mpg ~ hp + wt + cyl + disp, data = train_data) reg_predictions <- predict(reg_fit, new_data = test_data) reg_results <- test_data %>% select(mpg) %>% bind_cols(reg_predictions) print(reg_results) ``` -------------------------------- ### Translate Parsnip Naive Bayes to 'naivebayes' Engine (R) Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/naive_Bayes_naivebayes.md Demonstrates how to translate a parsnip Naive Bayes model specification, including its tuning parameters, into the syntax of the original 'naivebayes' package. This is useful for understanding the underlying implementation and ensuring correct translation. ```r library(discrim) naive_Bayes(smoothness = numeric(0), Laplace = numeric(0)) |> set_engine("naivebayes") |> translate() ``` -------------------------------- ### Get Random Forest Prediction Types Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/rand_forest_partykit.md Retrieves the available prediction types for the random forest engine when using the 'partykit' engine, filtered by mode. ```r parsnip:::get_from_env("rand_forest_predict") |> dplyr::filter(engine == "partykit") |> dplyr::select(mode, type) ``` -------------------------------- ### Translate penalty arguments for glmnet engine (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for a penalty model with the 'glmnet' engine. X, y, weights, and family are set, with family as binomial. ```r translate_args(set_engine(penalty, "glmnet")) ``` -------------------------------- ### Translate LiblineaR arguments for logistic_reg (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the 'LiblineaR' engine. It indicates that x, y, and weights are missing arguments by default, and verbose is set to FALSE. ```r translate_args(set_engine(basic, "LiblineaR")) ``` -------------------------------- ### Set XGBoost Engine with Params List (R) - Not Recommended Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/boost_tree_xgboost.md Illustrates the less recommended way of passing engine-specific arguments to the 'xgboost' engine by nesting them within a `params` list in `set_engine()`. Parsnip will warn and re-route these arguments, but direct passing is preferred. ```r boost_tree() | set_engine("xgboost", params = list(eval_metric = "mae")) ``` -------------------------------- ### Identify Tunables for Logistic Regression (Default Engine) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md This snippet demonstrates how to use the 'tunable' function with a basic 'logistic_reg()' model specification without specifying an engine. It shows that no tunables are identified by default when no specific engine is set. ```r tunable(logistic_reg()) ``` -------------------------------- ### Handle Deprecated Function `surv_reg` Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/surv_reg.md This example shows a deprecation warning for the `surv_reg()` function, indicating that users should migrate to the newer `survival_reg()` function for future use. ```r surv_reg() ``` -------------------------------- ### Handle Unsupported Engine for Model Specification Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/surv_reg.md Shows an error that occurs when an unsupported engine is specified for a model. The error message guides the user to available engines. ```r translate(set_engine(surv_reg(), "wat")) ``` -------------------------------- ### Handle invalid mode for linear_reg Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Illustrates the error handling for providing an invalid mode to the linear_reg function. The function expects 'regression' or 'classification' but an incorrect value is provided. ```R linear_reg(mode = "classification") ``` -------------------------------- ### Show Available Engines for Model Types Source: https://context7.com/tidymodels/parsnip/llms.txt Provides R code examples for using the show_engines() function to list all available computational engines for specific parsnip model types like 'linear_reg', 'rand_forest', and 'boost_tree'. ```r # Show all available engines for a model type show_engines("linear_reg") # Output: Table showing engines like lm, glm, glmnet, keras, stan, etc. show_engines("rand_forest") # Output: Table showing ranger, randomForest, spark, etc. show_engines("boost_tree") # Output: Table showing xgboost, C5.0, lightgbm, etc. ``` -------------------------------- ### Translate Arguments for earth Engine with keepxy = FALSE (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the earth engine when `keepxy` is explicitly set to `FALSE`. Demonstrates how `translate_args` captures overridden arguments. ```r translate_args(set_engine(basic, "earth", keepxy = FALSE)) ``` -------------------------------- ### Fit C5.0 Model with Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/boost_tree_C5.0.md This snippet demonstrates fitting a C5.0 model using the parsnip package. It specifies the formula, data, and the 'C5.0' engine. An example condition shows an error related to the `engine` argument during data creation. ```r res <- fit(lc_basic, funded_amnt ~ term, data = lending_club, engine = "C5.0", control = ctrl) # Error in `.convert_form_to_xy_fit()`: # ! The argument `engine` cannot be used to create the data. # Possible arguments are subset or weights. ``` -------------------------------- ### Translate penalty arguments for LiblineaR engine (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for a penalty model with the 'LiblineaR' engine. X and y are missing arguments, verbose is FALSE, and cost is set to 1. ```r translate_args(set_engine(penalty, "LiblineaR")) ``` -------------------------------- ### Print GAM Model Specification (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/model_basics.md Illustrates printing the specification for a Generalized Additive Model (GAM). The default computational engine is `mgcv`. ```r print(gen_additive_mod()) ``` -------------------------------- ### Translate stan arguments for logistic_reg (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the 'stan' engine. Formula, data, and weights are missing arguments, family is set to binomial, and refresh is set to 0. ```r translate_args(set_engine(basic, "stan")) ``` -------------------------------- ### Translate Arguments for boost_tree with C5.0 Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the boost_tree model using the 'C5.0' engine. Shows how missing arguments are represented and how specific parameters like 'rules' can be set. This function is part of the parsnip package. ```R translate_args(set_engine(basic_class, "C5.0")) ``` ```R translate_args(set_engine(basic_class, "C5.0", rules = TRUE)) ``` -------------------------------- ### Update Linear Regression with Mixture and nlambda Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/update.md Illustrates updating a linear regression model specification with both 'mixture' and 'nlambda' arguments. This example shows how to set main and engine-specific arguments simultaneously. ```r update(expr3, mixture = 1, nlambda = 10) update(expr3, mixture = 1, nlambda = 10, fresh = TRUE) ``` -------------------------------- ### Handle incorrect outcome type for glm model (fit) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/linear_reg.md Illustrates error handling when `fit` is used for a generalized linear model (glm) with a formula, but the outcome variable is a factor. ```R res <- fit(hpc_glm, hpc_bad_form, data = hpc, control = ctrl) ``` ```R lm_form_catch <- fit(hpc_glm, hpc_bad_form, data = hpc, control = caught_ctrl) ``` -------------------------------- ### Create and Use Parsnip Control Parameters Source: https://context7.com/tidymodels/parsnip/llms.txt Illustrates how to create a control object using control_parsnip() to manage fitting parameters like verbosity and error catching. It then shows how to apply this control object when fitting a random forest model. ```r # Create control object for fitting ctrl <- control_parsnip( verbosity = 1, # 0 = silent, 1 = some messages, 2 = all messages catch = FALSE # Should errors be caught or thrown? ) # Fit with control parameters rf_spec <- rand_forest(trees = 500) %>% set_engine("ranger") %>% set_mode("regression") rf_fit <- rf_spec %>% fit(mpg ~ ., data = mtcars, control = ctrl) # Silent fitting ctrl_silent <- control_parsnip(verbosity = 0) fit_silent <- rf_spec %>% fit(mpg ~ ., data = mtcars, control = ctrl_silent) # Catch errors for robust fitting ctrl_catch <- control_parsnip(catch = TRUE, verbosity = 0) # This won't throw an error if fitting fails fit_safe <- rf_spec %>% fit(mpg ~ bad_variable, data = mtcars, control = ctrl_catch) # Check if fit failed if (inherits(fit_safe$fit, "try-error")) { print("Model fitting failed") } ``` -------------------------------- ### Handle Classification Outcome Error with `fit_xy` and `glm` Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md Demonstrates an error when using `fit_xy` for a classification model where the outcome variable is not a factor. The `check_outcome` function correctly identifies and flags this issue. ```r glm_xy_catch <- fit_xy(lc_basic, control = caught_ctrl, x = lending_club[, num_pred], y = lending_club$total_bal_il) ``` -------------------------------- ### Validate Data Input Type for tester Function Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/fit_interfaces.md Verifies that the 'data' argument for the 'tester' function is a data.frame, dgCMatrix, or tbl_spark. Providing a double matrix will lead to an error. ```r tester(NULL, f, data = as.matrix(hpc[, 1:4])) # Condition: # Error in `tester()`: # ! `data` should be a , not a double matrix. ``` -------------------------------- ### Translate Parsnip LightGBM Classification to Bonsai Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/boost_tree_lightgbm.md This code snippet illustrates the translation of a parsnip 'boost_tree' model specification for 'lightgbm' engine and 'classification' mode to its 'bonsai' package equivalent. It displays the default parameters and the template for the underlying bonsai function. ```r boost_tree( mtry = integer(), trees = integer(), tree_depth = integer(), learn_rate = numeric(), min_n = integer(), loss_reduction = numeric() ) |> set_engine("lightgbm") | set_mode("classification") | translate() ``` -------------------------------- ### Get prediction types for quantreg engine Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/linear_reg_quantreg.md Retrieves the supported prediction types for the 'quantreg' engine within parsnip. This helps in understanding the output formats available for predictions made by this engine. ```r parsnip:::get_from_env("linear_reg_predict") |> dplyr::filter(engine == "quantreg") |> dplyr::select(mode, type) ``` -------------------------------- ### Translate Arguments for Basic earth Engine (parsnip) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the basic earth engine using `set_engine`. Shows how `translate_args` handles standard arguments and default settings like `keepxy = TRUE`. ```r translate_args(set_engine(basic, "earth")) ``` -------------------------------- ### Get prediction types for gen_additive_mod Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/gen_additive_mod_mgcv.md Retrieves and displays the available prediction types for the gen_additive_mod function, categorized by mode (regression and classification). This helps in understanding the possible outputs of the model's predictions. ```r parsnip:::get_from_env("gen_additive_mod_predict") |> dplyr::select(mode, type) ``` -------------------------------- ### Print Quadratic Discriminant Model Specification (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/model_basics.md Demonstrates printing the specification for a quadratic discriminant model. It notes that the `discrim` package is required and the computational engine is `MASS`. ```r print(discrim_quad()) ``` -------------------------------- ### Get prediction types for glmer engine in parsnip Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/linear_reg_glmer.md Retrieves the supported prediction types for the 'glmer' engine within the parsnip package. This function helps understand what kinds of predictions can be generated. ```r parsnip:::get_from_env("linear_reg_predict") |> dplyr::filter(engine == "glmer") |> dplyr::select(mode, type) ``` -------------------------------- ### Translate parsnip svm_poly to kernlab for Regression Source: https://github.com/tidymodels/parsnip/blob/main/man/rmd/svm_poly_kernlab.md Demonstrates how to translate the svm_poly() parsnip model specification to the original 'kernlab' package for regression tasks. This involves setting the engine and mode, and then using the translate() function to see the underlying R code. It outlines the main arguments and the specific function call for kernlab. ```r svm_poly( cost = double(1), degree = integer(1), scale_factor = double(1), margin = double(1) ) |> set_engine("kernlab") |> set_mode("regression") |> translate() ``` -------------------------------- ### Set Ranger Engine Arguments for Parsnip Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Illustrates setting arguments for the 'ranger' engine in parsnip, a fast implementation of random forests. Covers parameters such as `mtry`, `num.threads`, `verbose`, `num.trees`, `importance`, `min.node.size`, and `probability`. `mtry` is the number of variables randomly sampled at each split, `num.threads` controls parallel processing, `verbose` for output logging, `num.trees` for the number of trees, `importance` for variable importance calculation, `min.node.size` for minimum nodes, and `probability` for probability estimation. ```r translate_args(set_engine(mtry, "ranger")) ``` ```r translate_args(set_engine(trees, "ranger")) ``` ```r translate_args(set_engine(trees, "ranger", importance = "impurity")) ``` ```r translate_args(set_engine(min_n, "ranger")) ``` -------------------------------- ### Identify Tunables for Logistic Regression with Glmnet Engine Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/logistic_reg.md This snippet illustrates the use of 'tunable' with 'logistic_reg()' and the 'glmnet' engine. It identifies two tunable parameters: 'penalty' and 'mixture', which are common tuning parameters for penalized regression models. ```r tunable(set_engine(logistic_reg(), "glmnet")) ``` -------------------------------- ### Translate glm arguments for logistic_reg (R) Source: https://github.com/tidymodels/parsnip/blob/main/tests/testthat/_snaps/translate.md Translates arguments for the 'glm' engine of logistic regression. It shows that formula, data, and weights are missing arguments by default, while the family is set to binomial. ```r translate_args(set_engine(basic, "glm")) ``` -------------------------------- ### Specify Boosted Trees with C5.0 Engine Source: https://context7.com/tidymodels/parsnip/llms.txt Demonstrates how to create a boosted tree model specification using parsnip and set the C5.0 engine with specific trial parameters for classification. ```r c5_spec <- boost_tree(trees = 30) %>% set_engine("C5.0", trials = 10) %>% set_mode("classification") ```