R Machine Learning Mini-Course

From Developer to Machine Learning Practitioner in 14 Days

In this mini-course you will discover how you can get started, build accurate models and confidently complete predictive modeling machine learning projects using R in 14 days.

This is a big and important post. You might want to bookmark it.

Who Is This Mini-Course For?

Before we get started, let’s make sure you are in the right place. The list below provides some general guidelines as to who this course was designed for.

Don’t panic if you don’t match these points exactly, you might just need to brush up in one area or another to keep up.

  • Developers that know how to write a little code. This means that it is not a big deal for you to pick up a new programming language like R once you know the basic syntax. It does not mean your a wizard coder, just that you can follow a basic C-like language with little effort.
  • Developers that know a little machine learning. This means you know about the basics of machine learning like cross validation, some algorithms and bias-variance trade-off. It does not mean that you are a machine learning PhD, just that you know the landmarks or know where to look them up.

This mini-course is neither a textbook on R or a textbook on machine learning.

It will take you from a developer that knows a little machine learning to a developer who can get results using R, the most powerful and most popular platform for machine learning.

Mini-Course Overview (what to expect)

This mini-course is broken down into 14 lessons that I call “days”.

You could complete one lesson per day (recommended) or complete all of the lessons in one day (hard core!). It really depends on the time you have available and your level of enthusiasm.

Below are 14 lessons that will get you started and productive with machine learning in R:

  • Day 1: Download and Install R.
  • Day 2: Get Around In R with Basic Syntax.
  • Day 3: Load Data and Standard Machine Learning Datasets.
  • Day 4: Understand Data with Descriptive Statistics.
  • Day 5: Understand Data with Visualization.
  • Day 6: Prepare For Modeling by Pre-Processing Data.
  • Day 7: Algorithm Evaluation With Resampling Methods.
  • Day 8: Algorithm Evaluation Metrics.
  • Day 9: Spot-Check Algorithms.
  • Day 10: Model Comparison and Selection.
  • Day 11: Improve Accuracy with Algorithm Tuning.
  • Day 12: Improve Accuracy with Ensemble Predictions.
  • Day 13: Finalize And Save Your Model.
  • Day 14: Hello World End-to-End Project.

Each lesson could take you 60 seconds or up to 30 minutes. Take your time and complete the lessons at your own pace. Ask questions and even post results in the comments below.

The lessons expect you to go off and find out how to do things. I will give you hints, but part of the point of each lesson is to force you to learn where to go to look for help on and about the R platform (hint, I have all of the answers directly on this blog, use the search).

I do provide more help in the early lessons because I want you to build up some confidence and inertia. Hang in there, don’t give up!

Get Started with Machine Learning in R, Right Now

Machine Learning Mastery With R Mini Course Table of Contents

R is the most popular platform among professional data scientists for applied machine learning.

Download your mini-course in Machine Learning with R.

Start Your FREE Mini-Course >> 

FREE 14-Day Mini-Course in
Machine Learning with R

Download your PDF containing all 14 lessons.

Get your daily lesson via email with tips and tricks.

Day 1: Download and Install R

You cannot get started with machine learning in R until you have access to the platform.

Todays lesson is easy, you must download and install the R platform on your computer.

  1. Visit the R homepage and download R for your operating system (Linux, OS X or Windows).
  2. Install R on your computer.
  3. Start R for the first time from command line by typing “R”.

If you need help installing R, checkout the post:

Day 2: Get Around In R with Basic Syntax

You need to be able to read and write basic R scripts.

As a developer you can pick-up new programming languages pretty quickly. R is case sensitive, uses hash (#) for comments and uses the arrow operator (<-) for assignments instead of the single equals (=).

Todays task is to practice the basic syntax of the R programming language in the R interactive environment.

  1. Practice assignment in the language using the arrow operator (<-).
  2. Practice using basic data structures like vectors, lists and data frames.
  3. Practice using flow control structures like If-Then-Else and loops.
  4. Practice calling functions, installing and loading packages.

For example, below is an example of creating a list of numbers and calculating the mean.

If you need help with basic R syntax, see the post:

Day 3: Load Data and Standard Machine Learning Datasets

Machine learning algorithms need data. You can load your own data from CSV files but when you are getting started with machine learning in R you should practice on standard machine learning datasets.

Your task for todays lesson are to get comfortable loading data into R and to find and load standard machine learning datasets.

The datasets package that comes with R has many standard datasets including the famous iris flowers dataset. The mlbench package also contains man standard machine learning datasets.

  1. Practice loading CSV files into R using the read.csv() function.
  2. Practice loading standard machine learning datasets from the datasets and mlbench packages.

Help: You can get help about a function by typing ?FunctionName or by calling the help() function and passing the function name that you need help with as an argument.

To get you started, the below snippet will install and load the mlbench package, list all of the datasets it offers and attach the PimaIndiansDiabetes dataset to your environment for you to play with.

Well done for making it this far! Hang in there.

Any questions so far? Ask in the comments.

Day 4: Understand Data with Descriptive Statistics

Once you have loaded your data into R you need to be able to understand it.

The better you can understand your data, the better and more accurate the models that you can build. The first step to understanding your data is to use descriptive statistics.

Today your lesson is to learn how to use descriptive statistics to understand your data.

  1. Understand your data using the head() function to look at the first few rows.
  2. Review the dimensions of your data with the dim() function.
  3. Review the distribution of your data with the summary() function.
  4. Calculate pair-wise correlation between your variables using the cor() function.

The below example loads the iris dataset and summarizes the distribution of each attribute.

Try it out!

Day 5: Understand Data with Visualization

Continuing on from yesterdays lesson, you must spend time to better understand your data.

A second way to improve your understanding of your data is by using data visualization techniques (e.g. plotting).

Today, your lesson is to learn how to use plotting in R to understand attributes alone and their interactions.

  1. Use the hist() function to create a histogram of each attribute.
  2. Use the boxplot() function to create box and whisker plots of each attribute.
  3. Use the pairs() function to create pair-wise scatterplots of all attributes.

For example the snippet below will load the iris dataset and create a scatterplot matrix of the dataset.

Day 6: Prepare For Modeling by Pre-Processing Data

Your raw data may not be setup to be in the best shape for modeling.

Sometimes you need to pre-process your data in order to best present the inherent structure of the problem in your data to the modeling algorithms. In today’s lesson, you will use the pre-processing capabilities provided by the caret package.

The caret package provides the preprocess() function that takes a method argument to indicate the type of pre-processing to perform. Once the pre-processing parameters have been prepared from a dataset, the same pre-processing step can be applied to each dataset that you may have.

Remember, you can install and load the caret package as follows:

  1. Standardize numerical data (e.g. mean of 0 and standard deviation of 1) using the scale and center options.
  2. Normalize numerical data (e.g. to a range of 0-1) using the range option.
  3. Explore more advanced power transforms like the Box-Cox power transform with the BoxCox option.

For example, the snippet below loads the iris dataset, calculates the parameters needed to normalize the data, then creates a normalized copy of the data.

Day 7: Algorithm Evaluation With Resampling Methods

The dataset used to train a machine learning algorithm is called a training dataset. The dataset used to train an algorithm cannot be used to give you reliable estimates of the accuracy of the model on new data. This is a big problem because the whole idea of creating the model is to make predictions on new data.

You can use statistical methods called resampling methods to split your training dataset up into subsets, some are used to train the model and others are held back and used to estimate the accuracy of the model on unseen data.

Your goal with todays lesson is to practice using the different resampling methods available in the caret package. Look up the help on the createDataPartition(), trainControl() and train() functions in R.

  1. Split a dataset into training and test sets.
  2. Estimate the accuracy of an algorithm using k-fold cross validation.
  3. Estimate the accuracy of an algorithm using repeated k-fold cross validation.

The snippet below uses the caret package to estimate the accuracy of the Naive Bayes algorithm on the iris dataset using 10-fold cross validation.

Need more help on this step?

Take a look at the post on resampling methods:

Did you realize that this is the half-way point? Well done!

Day 8: Algorithm Evaluation Metrics

There are many different metrics that you can use to evaluate the skill of a machine learning algorithm on a dataset.

You can specify the metric used for your test harness in caret in the train() function and defaults can be used for regression and classification problems. Your goal with todays lesson is to practice using the different algorithm performance metrics available in the caret package.

  1. Practice using the Accuracy and Kappa metrics on a classification problem (e.g. iris dataset).
  2. Practice using RMSE and RSquared metrics on a regression problem (e.g. longley dataset).
  3. Practice using the ROC metrics on a binary classification problem (e.g. PimaIndiansDiabetes dataset from the mlbench package).

The snippet below demonstrates calculating the LogLoss metric on the iris dataset.

Day 9: Spot-Check Algorithms

You cannot possibly know which algorithm will perform best on your data before hand.

You have to discover it using a process of trial and error. I call this spot-checking algorithms. The caret package provides an interface to many machine learning algorithms and tools to compare the estimated accuracy of those algorithms.

In this lesson you must practice spot checking different machine learning algorithms.

  1. Spot check linear algorithms on a dataset (e.g. linear regression, logistic regression and linear discriminate analysis).
  2. Spot check some non-linear algorithms on a dataset (e.g. KNN, SVM and CART).
  3. Spot-check some sophisticated ensemble algorithms on a dataset (e.g. random forest and stochastic gradient boosting).

Help: You can get a list of models that you can use in caret by typing: names(getModelInfo())

For example, the snippet below spot-checks two linear algorithms on the Pima Indians Diabetes dataset from the mlbench package.

Day 10: Model Comparison and Selection

Now that you know how to spot check machine learning algorithms on your dataset, you need to know how to compare the estimated performance of different algorithms and select the best model.

Thankfully the caret package provides a suite of tools to plot and summarize the differences in performance between models.

In todays lesson you will practice comparing the accuracy of machine learning algorithms in R.

  1. Use the summary() caret function to create a table of results (hint, there is an example in the previous lesson)
  2. Use the dotplot() caret function to compare results.
  3. Use the bwplot() caret function to compare results.
  4. Use the diff() caret function to calculate the statistical significance between results.

The snippet below extends yesterdays example and creates a plot of the spot-check results.

Day 11: Improve Accuracy with Algorithm Tuning

Once you have found one or two algorithms that perform well on your dataset, you may want to improve the performance of those models.

One way to increase the performance of an algorithm is to tune it’s parameters to your specific dataset.

The caret package provides three ways to search for combinations of parameters for a machine learning algorithm. Your goal in todays lesson is to practice each.

  1. Tune the parameters of an algorithm automatically (e.g. see the tuneLength argument to train()).
  2. Tune the parameters of an algorithm using a grid search that you specify.
  3. Tune the parameters of an algorithm using a random search.

Take a look at the help for the trainControl() and train() functions and take note of the method and the tuneGrid arguments.

The snippet below uses is an example of using a grid search for the random forest algorithm on the iris dataset.

You’re nearly at the end! Just a few more lessons.

Day 12: Improve Accuracy with Ensemble Predictions

Another way that you can improve the performance of your models is to combine the predictions from multiple models.

Some models provide this capability built-in such as random forest for bagging and stochastic gradient boosting for boosting. Another type of ensembling called stacking (or blending) can learn how to best combine the predictions from multiple models and is provided in the package caretEnsemble.

In todays lesson you will practice using ensemble methods.

  1. Practice bagging ensembles with the random forest and bagged CART algorithms in caret.
  2. Practice boosting ensembles with the gradient boosting machine and C5.0 algorithms in caret.
  3. Practice stacking ensembles using the caretEnsemble package and the caretStack() function.

The snippet below demonstrates how you can combine the predictions from multiple models using stacking.

Day 13: Finalize And Save Your Model

Once you have found a well performing model on your machine learning problem, you need to finalize it.

In todays lesson you will practice the tasks related to finalizing your model.

  1. Practice using the predict() function to make predictions with a model trained using caret.
  2. Practice training standalone versions of well performing models.
  3. Practice saving trained models to file and loading them up again using the saveRDS() and readRDS() functions.

For example, the snippet below shows how you can create a random forest algorithm trained on your entire dataset ready for general use.

Day 14: Hello World End-to-End Project

You now know how to complete each task of a predictive modeling machine learning problem.

In todays lesson you need to practice putting the pieces together and working through a standard machine learning dataset end-to-end.

  1. Work through the iris dataset end-to-end (the hello world of machine learning)

This includes the steps:

  1. Understanding your data using descriptive statistics and visualization.
  2. Pre-Processing the data to best expose the structure of the problem.
  3. Spot-checking a number of algorithms using your own test harness.
  4. Improving results using algorithm parameter tuning.
  5. Improving results using ensemble methods.
  6. Finalize the model ready for future use.

The End! (Look How Far You Have Come)

You made it. Well done!

Take a moment and look back at how far you have come.

  • You started off with an interest in machine learning and a strong desire to be able to practice and apply machine learning using R.
  • You downloaded, installed and started R, perhaps for the first time and started to get familiar with the syntax of the language.
  • Slowly and steadily over the course of a number of lessons you learned how the standard tasks of a predictive modeling machine learning project map onto the R platform.
  • Building upon the recipes for common machine learning tasks you worked through your first machine learning problems end-to-end using R.
  • Using a standard template, the recipes and experience you have gathered you are now capable of working through new and different predictive modeling machine learning problems on your own.

Don’t make light of this, you have come a long way in a short amount of time.

This is just the beginning of your machine learning journey with R. Keep practicing and developing your skills.

How Did You Go With The Mini-Course?

Did you enjoy this mini-course?

Do you have any questions? Were there any sticking points?

Let me know. Leave a comment below.

Frustrated With Your Progress In R Machine Learning?

Develop Your Own Models and Predictions in Minutes

...with just a few lines of R code

Discover how in my new Ebook: Machine Learning Mastery With R

It covers self-study tutorials and end-to-end projects on topics like:
Loading data, visualization, build models, algorithm tuning, and much more...

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.

2 Responses to R Machine Learning Mini-Course

  1. PAUL April 18, 2016 at 8:41 pm #

    Hi I have brought machine learning mastery with R book, just want to know where do i get the data set files and scripts, cause i dont find it in the zip file.

    • Jason Brownlee April 19, 2016 at 5:43 am #

      Hi Paul,

      After purchasing you will receive a link to download “machine_learning_mastery_with_r.zip”. Inside that ZIP, you will have two more files, the book: “machine_learning_mastery_with_r.pdf” and the code examples: “machine_learning_mastery_with_r_code.zip”. I hope that is clearer.

      Any more questions at all, please ask.

      Jason.

Leave a Reply