install.packages("pak")
# check that it is installed then use it to install tidymodels
if (require(pak)) {
pak::pak("tidymodels")
}
1 Introduction
tidymodels is a framework for creating statistical and machine learning models in R. The framework consists of a set of tightly coupled R packages that are designed in the same way. The project began in late 2016.
The main tidymodels resources are:
tidymodels.org
: a website with reference materials, articles, and searchable lists of functions.Tidy Modeling with R (
tmwr.org
): a long-form book.workshops.tidymodels.org
: the current (and past) training materials.
We’ll reference these and other resources as needed.
1.1 Installation
tidymodels is built in R so you’ll need to install that. We used R version 4.4.1 (2024-06-14) for these notes. To install R, you can go to CRAN1 to download it for your operating system. If you are comfortable at the command line, the rig application is an excellent way to install and manage R versions.
You probably want to use an integrated development environment (IDE); it will make your life much better. We use the RStudio IDE, which can be downloaded here. Other applications are Visual Studio and emacs.
To use tidymodels, you need to install multiple packages. The core packages are bundled into a “verse” package called tidymodels. When you install that, you get the primary packages as well as some tidyverse packages such as dplyr and ggplot2.
To install it, you can use
install.packages("tidymodels")
We suggest using the pak package for installation. To do this, first install that and then use it for further installations:
1.2 Loading tidymodels
Once you do that, load tidymodels:
library(tidymodels)
#> ── Attaching packages ─────────────────────────────────────────── tidymodels 1.2.0 ──
#> ✔ broom 1.0.6 ✔ recipes 1.1.0
#> ✔ dials 1.3.0 ✔ rsample 1.2.1
#> ✔ dplyr 1.1.4 ✔ tibble 3.2.1
#> ✔ ggplot2 3.5.1 ✔ tidyr 1.3.1
#> ✔ infer 1.0.7 ✔ tune 1.2.1
#> ✔ modeldata 1.4.0 ✔ workflows 1.1.4
#> ✔ parsnip 1.2.1 ✔ workflowsets 1.1.0
#> ✔ purrr 1.0.2 ✔ yardstick 1.3.1
#> ── Conflicts ────────────────────────────────────────────── tidymodels_conflicts() ──
#> ✖ purrr::discard() masks scales::discard()
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
#> ✖ recipes::step() masks stats::step()
#> • Search for functions across packages at https://www.tidymodels.org/find/
The default output shows the packages that are automatically attached. There are a lot of functions in tidy models, but by loading this meta-package, you don’t have to remember which functions come from which packages.
Note the lines at the bottom that messages like :
This means that two packages, dplyr and stats, have functions with the same name (filter()
)2. If you were to type filter
at an R prompt, the function that you get corresponds to the one in the most recently loaded package. That’s not ideal.
tidymodels_prefer()
. When you use this, it prioritizes functions from the tidy models and tidyverse groups so that you get those 3 If you want to see the specific conflicts and how we resolve them, see this output:
tidymodels_prefer(quiet = FALSE)
#> [conflicted] Will prefer agua::refit over any other package.
#> [conflicted] Will prefer dials::Laplace over any other package.
#> [conflicted] Will prefer dials::max_rules over any other package.
#> [conflicted] Will prefer dials::neighbors over any other package.
#> [conflicted] Will prefer dials::prune over any other package.
#> [conflicted] Will prefer dials::smoothness over any other package.
#> [conflicted] Will prefer dplyr::collapse over any other package.
#> [conflicted] Will prefer dplyr::combine over any other package.
#> [conflicted] Will prefer dplyr::filter over any other package.
#> [conflicted] Will prefer dplyr::rename over any other package.
#> [conflicted] Will prefer dplyr::select over any other package.
#> [conflicted] Will prefer dplyr::slice over any other package.
#> [conflicted] Will prefer ggplot2::`%+%` over any other package.
#> [conflicted] Will prefer ggplot2::margin over any other package.
#> [conflicted] Will prefer parsnip::bart over any other package.
#> [conflicted] Will prefer parsnip::fit over any other package.
#> [conflicted] Will prefer parsnip::mars over any other package.
#> [conflicted] Will prefer parsnip::pls over any other package.
#> [conflicted] Will prefer purrr::cross over any other package.
#> [conflicted] Will prefer purrr::invoke over any other package.
#> [conflicted] Will prefer purrr::map over any other package.
#> [conflicted] Will prefer recipes::discretize over any other package.
#> [conflicted] Will prefer recipes::step over any other package.
#> [conflicted] Will prefer rsample::populate over any other package.
#> [conflicted] Will prefer scales::rescale over any other package.
#> [conflicted] Will prefer themis::step_downsample over any other package.
#> [conflicted] Will prefer themis::step_upsample over any other package.
#> [conflicted] Will prefer tidyr::expand over any other package.
#> [conflicted] Will prefer tidyr::extract over any other package.
#> [conflicted] Will prefer tidyr::pack over any other package.
#> [conflicted] Will prefer tidyr::unpack over any other package.
#> [conflicted] Will prefer tune::parameters over any other package.
#> [conflicted] Will prefer tune::tune over any other package.
#> [conflicted] Will prefer yardstick::get_weights over any other package.
#> [conflicted] Will prefer yardstick::precision over any other package.
#> [conflicted] Will prefer yardstick::recall over any other package.
#> [conflicted] Will prefer yardstick::spec over any other package.
#> [conflicted] Will prefer recipes::update over Matrix::update.
#> ── Conflicts ───────────────────────────────────────────────── tidymodels_prefer() ──
If you want to know more about why tidymodels exists, we’ve written a bit about this in the tidymodels book. The second chapter describes how tidyverse principles can be used for modeling.
1.3 Package Versions and Reproducability
We will do our best to use versions of our packages corresponding to the CRAN versions. We can’t always do that, and, for many packages, a version number ending with a value in the 9000 range (e.g., version “1.1.4.9001”) means that it was a development version of the package and was most likely installed from a GitHub repository.
At the end of each session, we’ll show which packages were loaded and used:
sessioninfo::session_info()
#> ─ Session info ────────────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.4.1 (2024-06-14)
#> os macOS Sonoma 14.6.1
#> system aarch64, darwin20
#> ui X11
#> language (EN)
#> collate en_US.UTF-8
#> ctype en_US.UTF-8
#> tz America/New_York
#> date 2024-09-23
#> pandoc 3.2 @ /Applications/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ────────────────────────────────────────────────────────────────────────
#> ! package * version date (UTC) lib source
#> P backports 1.5.0 2024-05-23 [?] CRAN (R 4.4.0)
#> P BiocManager 1.30.25 2024-08-28 [?] CRAN (R 4.4.1)
#> P broom * 1.0.6 2024-05-17 [?] CRAN (R 4.4.0)
#> P cachem 1.1.0 2024-05-16 [?] CRAN (R 4.4.0)
#> P class 7.3-22 2023-05-03 [?] CRAN (R 4.4.1)
#> P cli 3.6.3 2024-06-21 [?] CRAN (R 4.4.0)
#> P codetools 0.2-20 2024-03-31 [?] CRAN (R 4.4.1)
#> P colorspace 2.1-1 2024-07-26 [?] RSPM
#> P conflicted 1.2.0 2023-02-01 [?] CRAN (R 4.4.0)
#> P data.table 1.16.0 2024-08-27 [?] RSPM
#> P dials * 1.3.0 2024-07-30 [?] RSPM
#> P DiceDesign 1.10 2023-12-07 [?] CRAN (R 4.4.0)
#> P digest 0.6.37 2024-08-19 [?] RSPM
#> P dplyr * 1.1.4 2023-11-17 [?] CRAN (R 4.4.0)
#> P evaluate 1.0.0 2024-09-17 [?] CRAN (R 4.4.1)
#> P fansi 1.0.6 2023-12-08 [?] CRAN (R 4.4.0)
#> P fastmap 1.2.0 2024-05-15 [?] CRAN (R 4.4.0)
#> P foreach 1.5.2 2022-02-02 [?] CRAN (R 4.4.0)
#> P furrr 0.3.1 2022-08-15 [?] CRAN (R 4.4.0)
#> P future 1.34.0 2024-07-29 [?] RSPM
#> P future.apply 1.11.2 2024-03-28 [?] CRAN (R 4.4.0)
#> P generics 0.1.3 2022-07-05 [?] CRAN (R 4.4.0)
#> P ggplot2 * 3.5.1 2024-04-23 [?] CRAN (R 4.4.0)
#> P globals 0.16.3 2024-03-08 [?] CRAN (R 4.4.0)
#> P glue 1.7.0 2024-01-09 [?] CRAN (R 4.4.0)
#> P gower 1.0.1 2022-12-22 [?] CRAN (R 4.4.0)
#> P GPfit 1.0-8 2019-02-08 [?] CRAN (R 4.4.0)
#> P gtable 0.3.5 2024-04-22 [?] CRAN (R 4.4.0)
#> P hardhat 1.4.0 2024-06-02 [?] CRAN (R 4.4.0)
#> P htmltools 0.5.8.1 2024-04-04 [?] CRAN (R 4.4.0)
#> P htmlwidgets 1.6.4 2023-12-06 [?] CRAN (R 4.4.0)
#> P infer * 1.0.7 2024-03-25 [?] CRAN (R 4.4.0)
#> P ipred 0.9-15 2024-07-18 [?] RSPM
#> P iterators 1.0.14 2022-02-05 [?] CRAN (R 4.4.0)
#> P jsonlite 1.8.9 2024-09-20 [?] CRAN (R 4.4.1)
#> P knitr 1.48 2024-07-07 [?] CRAN (R 4.4.0)
#> P lattice 0.22-6 2024-03-20 [?] CRAN (R 4.4.1)
#> P lava 1.8.0 2024-03-05 [?] CRAN (R 4.4.0)
#> P lhs 1.2.0 2024-06-30 [?] CRAN (R 4.4.0)
#> P lifecycle 1.0.4 2023-11-07 [?] CRAN (R 4.4.0)
#> P listenv 0.9.1 2024-01-29 [?] CRAN (R 4.4.0)
#> P lubridate 1.9.3 2023-09-27 [?] CRAN (R 4.4.0)
#> P magrittr 2.0.3 2022-03-30 [?] CRAN (R 4.4.0)
#> P MASS 7.3-61 2024-06-13 [?] CRAN (R 4.4.0)
#> P Matrix 1.7-0 2024-04-26 [?] CRAN (R 4.4.1)
#> P memoise 2.0.1 2021-11-26 [?] CRAN (R 4.4.0)
#> P modeldata * 1.4.0 2024-06-19 [?] CRAN (R 4.4.0)
#> P munsell 0.5.1 2024-04-01 [?] CRAN (R 4.4.0)
#> P nnet 7.3-19 2023-05-03 [?] CRAN (R 4.4.1)
#> P parallelly 1.38.0 2024-07-27 [?] RSPM
#> P parsnip * 1.2.1 2024-03-22 [?] CRAN (R 4.4.0)
#> P pillar 1.9.0 2023-03-22 [?] CRAN (R 4.4.0)
#> P pkgconfig 2.0.3 2019-09-22 [?] CRAN (R 4.4.0)
#> P prodlim 2024.06.25 2024-06-24 [?] CRAN (R 4.4.0)
#> P purrr * 1.0.2 2023-08-10 [?] CRAN (R 4.4.0)
#> P R6 2.5.1 2021-08-19 [?] CRAN (R 4.4.0)
#> P Rcpp 1.0.13 2024-07-17 [?] RSPM
#> P recipes * 1.1.0 2024-07-04 [?] RSPM
#> P renv 1.0.8 2024-09-20 [?] CRAN (R 4.4.1)
#> P rlang 1.1.4 2024-06-04 [?] CRAN (R 4.4.0)
#> P rmarkdown 2.28 2024-08-17 [?] CRAN (R 4.4.0)
#> P rpart 4.1.23 2023-12-05 [?] CRAN (R 4.4.0)
#> P rsample * 1.2.1 2024-03-25 [?] CRAN (R 4.4.0)
#> P rstudioapi 0.16.0 2024-03-24 [?] CRAN (R 4.4.0)
#> P scales * 1.3.0 2023-11-28 [?] CRAN (R 4.4.0)
#> P sessioninfo 1.2.2 2021-12-06 [?] CRAN (R 4.4.0)
#> P survival 3.7-0 2024-06-05 [?] CRAN (R 4.4.0)
#> P tibble * 3.2.1 2023-03-20 [?] CRAN (R 4.4.0)
#> P tidymodels * 1.2.0 2024-03-25 [?] CRAN (R 4.4.0)
#> P tidyr * 1.3.1 2024-01-24 [?] CRAN (R 4.4.0)
#> P tidyselect 1.2.1 2024-03-11 [?] CRAN (R 4.4.0)
#> P timechange 0.3.0 2024-01-18 [?] CRAN (R 4.4.0)
#> P timeDate 4041.110 2024-09-22 [?] CRAN (R 4.4.1)
#> P tune * 1.2.1 2024-04-18 [?] CRAN (R 4.4.0)
#> P utf8 1.2.4 2023-10-22 [?] CRAN (R 4.4.0)
#> P vctrs 0.6.5 2023-12-01 [?] CRAN (R 4.4.0)
#> P withr 3.0.1 2024-07-31 [?] RSPM
#> P workflows * 1.1.4 2024-02-19 [?] CRAN (R 4.4.0)
#> P workflowsets * 1.1.0 2024-03-21 [?] CRAN (R 4.4.0)
#> P xfun 0.47 2024-08-17 [?] CRAN (R 4.4.0)
#> P yaml 2.3.10 2024-07-26 [?] CRAN (R 4.4.0)
#> P yardstick * 1.3.1 2024-03-21 [?] CRAN (R 4.4.0)
#>
#> [1] /Users/max/content/computing-tidymodels/renv/library/macos/R-4.4/aarch64-apple-darwin20
#> [2] /Users/max/Library/Caches/org.R-project.R/R/renv/sandbox/macos/R-4.4/aarch64-apple-darwin20/f7156815
#>
#> P ── Loaded and on-disk path mismatch.
#>
#> ───────────────────────────────────────────────────────────────────────────────────
The Comprehensive R Archive Network↩︎
The syntax
foo::bar()
means that the functionbar()
is inside of the package foo When used together, this is often referred to as “calling the function by its namespace.”. You can do this in your code, and developers often do. However, it’s fairly ugly. ↩︎Unfortunately, this is not a guarantee but it does work most of the time.↩︎