D-Lab's Introduction to Machine Learning with tidymodels
This repository contains the materials for D-Lab's Introduction to Machine Learning with tidymodels. Prior experience with the concepts in R Fundamentals and Data Wrangling and Manipulation in R is assumed.
Workshop Goals
In this workshop, we provide an introduction to machine learning algorithms by making use of the tidymodels
package. First, we discuss what machine learning is, what problems it works well for, and what problems it might work less well for. Then, we'll explore the tidymodels
framework to learn how to fit machine learning models in R. Finally, we will apply the tidymodels
framework to explore multiple machine learning algorithms in R.
By the end of the workshop, learners should feel prepared to explore machine learning approaches for their data problems.
Familiarity with R programming and data wrangling is assumed. If you are not familiar with the materials in Data Wrangling and Manipulation in R, we recommend attending that workshop first. In addition, this workshop focuses on how to implement machine learning approaches. Learners will likely benefit from previous exposure to statistics.
Installation Instructions
We will use RStudio to go through the workshop materials, which requires the installation of both the R language and the RStudio software. Complete the following steps:
-
Download R: Follow the links according to the operating system that you are running. Download the package, and install R onto your computer. You should install the most recent version (at least version 4.0).
-
Download RStudio: Install RStudio Desktop. This should be free. Do this after you have already installed R. The D-Lab strongly recommends an RStudio edition of 2022.02.0+443 "Prairie Trillium" or higher.
- Click the green "Code" button in the top right of the repository information.
- Click "Download Zip".
- Extract this file to a folder on your computer where you can easily access it (we recommend Desktop).
-
Optional: if you're familiar with
git
, you can instead clone this repository by opening a terminal and enteringgit clone [email protected]:dlab-berkeley/Machine-Learning-with-tidymodels.git
. -
Make sure the following packages are installed on your computer
install.packages(c("tidyverse", "tidymodels", "here","pROC","glmnet", "ranger", "rpart", "xgboost","rpart.plot", "doParallel", "palmerpenguins", "ISLR2", "klaR", "stacks"))
In addition you'll need to install the following package from github
remotes::install_github("tidymodels/discrim")
Run the Code
Now that you have all the required software and materials, you need to run the code:
-
Launch the RStudio software.
-
Use the file navigator to find the
Machine-Learning-with-tidymodels
folder you downloaded from Github. -
Open up the file corresponding to the part of the workshop you're attending.
-
If necessary run the initial line of code
renv::init()
. You may see an error and a message "The project library is out of sync with the lockfile." If so, run renv::restore(). -
Place your cursor on a given line and press "Command + Enter" (Mac) or "Control + Enter" (PC) to run an individual line of code.
-
The
solutions
folder contains the solutions to the challenge problems.
Is R not working on your laptop?
This workshop makes use of many packages within the R ecosystem. For that reason, we have found it to be easier to use R on your local machine.
If you do not have R installed and the materials loaded on your workshop by the time it starts, we strongly recommend using the UC Berkeley Datahub to run the materials for these lessons. You can access the DataHub by clicking this link. Some users may find that they have to click the link twice if the materials do not load initially.
The DataHub downloads this repository, along with any necessary packages, and allows you to run the materials in an RStudio instance on UC Berkeley's servers. No installation is needed from your end - you only need an internet browser and a CalNet ID to log in. By using the DataHub, you can save your work and come back to it at any time. When you want to return to your saved work, go straight to DataHub, sign in, and click on the Machine-Learning-with-tidymodels
folder.
Additional Resources
This workshop draws heavily on the following resources:
Tidy Modeling with R by Max Kuhn and Julia Silge.
An Introduction to Statistical Learning by James, Witten, Hastie, and Tibshirani
"Machine Learning Methods Economists Should Know About" by Athey and Imbens
The Elements of Statistical Learning by Hastie, Friedman, and Tibshirani
Deep Learning by Goodfellow, Bengio, and Courville
Other D-Lab R Workshops
Basic Competency
- R Data Wrangling
- R Graphics with ggplot2
- R Functional Programming
- Project Management in R
- Geospatial Fundamentals in R with sf
- Census Data in R
Intermediate/Advanced Competency
- Unsupervised Learning in R
- Introduction to Deep Learning in R
- Fairness and Bias in Machine Learning
- R Package Development
Contributors
Previous iterations of D-Lab's Machine Learning with R were created by: