The usemodels package is a helpful way of quickly creating code snippets to fit models using the tidymodels framework.

Given a simple formula and a data set, the use_* functions can create code that appropriate for the data (given the model).

For example, using the palmerpenguins data with a glmnet model:

> library(usemodels)
> library(palmerpenguins)
> data(penguins)
> use_glmnet(body_mass_g ~ ., data = penguins)
glmnet_recipe <- 
  recipe(formula = body_mass_g ~ ., data = penguins) %>% 
  step_novel(all_nominal(), -all_outcomes()) %>% 
  step_dummy(all_nominal(), -all_outcomes()) %>% 
  step_zv(all_predictors()) %>% 
  step_normalize(all_predictors(), -all_nominal()) 

glmnet_spec <- 
  linear_reg(penalty = tune(), mixture = tune()) %>% 
  set_mode("regression") %>% 
  set_engine("glmnet") 

glmnet_workflow <- 
  workflow() %>% 
  add_recipe(glmnet_recipe) %>% 
  add_model(glmnet_spec) 

glmnet_grid <- tidyr::crossing(penalty = 10^seq(-6, -1, length.out = 20), mixture = c(0.05, 
    0.2, 0.4, 0.6, 0.8, 1)) 

glmnet_tune <- 
  tune_grid(glmnet_workflow, resamples = stop("add your rsample object"), grid = glmnet_grid) 

The recipe steps that are used (if any) depend on the type of data as well as the model. In this case, the first two steps handle the fact that Species is a factor-encoded predictor (and glmnet requires all numeric predictors). The last two steps are added because, for this model, the predictors should be on the same scale to be properly regularized.

The package includes these templates:

> ls("package:usemodels", pattern = "use_")
[1] "use_cubist"  "use_earth"   "use_glmnet"  "use_kknn"    "use_ranger" 
[6] "use_xgboost"

Installation

You can install usemodels with:

devtools::install_github("tidymodels/usemodels")

Contributing

This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.