This repository contains the materials for D-Lab's R Machine Learning
with tidymodels
. 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. Learners will likely benefit from prior
exposure to basic statistics.
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.
Some topics covered include:
- Supervised machine learning - regression and classification
- Train-test splits
- The bias-variance trade-off
- Algorithms - linear regression, logistic regression, and random forest
- Cross-validation
- Hyperparameter tuning
- Regularization - ridge, lasso, and elastic net
By the end of the workshop, learners should feel prepared to explore machine learning approaches for their data problems.
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 git@github.com:dlab-berkeley/R-Machine-Learning.git
. -
Be sure to run the
install.R
script in the repository so that all necessary packages are installed.
This workshop makes use of many packages within the R ecosystem. For that reason, we recommend using 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 the following button:
Some users may 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
R-Machine-Learning
folder.
If you don't have a Berkeley CalNet ID, you can still run these lessons in the cloud, by clicking this button:
If you are loading Binder with this repository for the first time, it may take a few minutes to set up. Binder operates similarly to the D-Lab DataHub, but on a different set of servers. By using Binder, however, you cannot save your work.
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
R-Machine-Learning
folder you downloaded from Github. -
Open up the file corresponding to the part of the workshop you're attending.
-
If necessary, run
install.R
to make sure the requisite packages are installed. This should not be necessary on Binder. -
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.
This workshop draws heavily on the following resources:
-
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
- R Data Wrangling
- R Graphics with ggplot2
- R Functional Programming
- Project Management in R
- Geospatial Fundamentals in R with sf
- Census Data in R
- Unsupervised Learning in R
- Introduction to Deep Learning in R
- Fairness and Bias in Machine Learning
- R Package Development
Previous iterations of D-Lab's Machine Learning with R were created by: