Spot Check Machine Learning Algorithms in R (algorithms to try on your next project)

Spot checking machine learning algorithms is how you find the best algorithm for your dataset.

But what algorithms should you spot check?

In this post you discover the 8 machine learning algorithms you should spot check on your data.

You also get recipes of each algorithm that you can copy and paste into your current or next machine learning project in R.

Let’s get started.

Spot Check Machine Learning Algorithms in R

Spot Check Machine Learning Algorithms in R
Photo by Nuclear Regulatory Commission, some rights reserved.

Best Algorithm For Your Dataset

You cannot know which algorithm will work best on your dataset before hand.

You must use trial and error to discover a short list of algorithms that do well on your problem that you can then double down on and tune further. I call this process spot checking.

The question is not:

What algorithm should I use on my dataset?

Instead it is:

What algorithms should I spot check on my dataset?

Which Algorithms To Spot Check

You can guess at what algorithms might do well on your dataset, and this can be a good starting point.

I recommend trying a mixture of algorithms and see what is good at picking out the structure in your data.

  • Try a mixture of algorithm representations (e.g. instances and trees).
  • Try a mixture of learning algorithms (e.g. different algorithms for learning the same type of representation).
  • Try a mixture of modeling types (e.g. linear and non-linear functions or parametric and non-parametric).

Let’s get specific. In the next section, we will look algorithms that you can use to spot check on your next machine learning project in R.

Need more elp with R for Machine Learning?

Take my free 14-day email course and discover how to use R on your project (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

Algorithms To Spot Check in R

There are hundreds of machine learning algorithms available in R.

I would recommend exploring many of them, especially, if making accurate predictions on your dataset is important and you have the time.

Often you don’t have the time, so you need to know the few algorithms that you absolutely must test on your problem.

In this section you will discover the linear and nonlinear algorithms you should spot check on your problem in R. This excludes ensemble algorithms such as as boosting and bagging, that can come later once you have a baseline.

Each algorithm will be presented from two perspectives:

  1. The package and function used to train and make predictions for the algorithm.
  2. The caret wrapper for the algorithm.

You need to know which package and function to use for a given algorithm. This is needed when:

  • You are researching the algorithm parameters and how to get the most from the algorithm.
  • You have a discovered the best algorithm to use and need to prepare a final model.

You need to know how to use each algorithm with caret, so that you can efficiently evaluate the accuracy of the algorithm on unseen data using the preprocessing, algorithm evaluation and tuning capabilities of caret.

Two standard datasets are used to demonstrate the algorithms:

  • Boston Housing dataset for regression (BostonHousing from the mlbench library).
  • Pima Indians Diabetes dataset for classification (PimaIndiansDiabetes from the mlbench library).

Algorithms are presented in two groups:

  • Linear Algorithms that are simpler methods that have a strong bias but are fast to train.
  • Nonlinear Algorithms that are more complex methods that have a large variance but are often more accurate.

Each recipe presented in this section is complete and will produce a result, so that you can copy and paste it into your current or next machine learning project.

Let’s get started.

Linear Algorithms

These are methods that make large assumptions about the form of the function being modeled. As such they are have a high bias but are often fast to train.

The final models are also often easy (or easier) to interpret, making them desirable as final models. If the results are suitably accurate, you may not need to move onto non-linear methods if a linear algorithm.

1. Linear Regression

The lm() function is in the stats library and creates a linear regression model using ordinary least squares.

The lm implementation can be used in caret as follows:

2. Logistic Regression

The glm function is in the stats library and creates a generalized linear model. It can be configured to perform a logistic regression suitable for binary classification problems.

The glm algorithm can be used in caret as follows:

3. Linear Discriminant Analysis

The lda function is in the MASS library and creates a linear model of a classification problem.

The lda algorithm can be used in caret as follows:

4. Regularized Regression

The glmnet function is in the glmnet library and can be used for classification or regression.

Classification Example:

Regression Example:

It can also be configured to perform three important types of regularization: lasso, ridge and elastic net by configuring the alpha parameter to 1, 0 or in [0,1] respectively.

The glmnet implementation can be used in caret for classification as follows:

The glmnet implementation can be used in caret for regression as follows:

Nonlinear Algorithms

Thees are machine learning algorithms that make fewer assumptions about the function being modeled. As such, they have a higher variance but are often result in higher accuracy. They increased flexibility also can make them slower to train or increase their memory requirements.

1. k-Nearest Neighbors

The knn3 function is in the caret library and does not create a model, rather makes predictions from the training set directly. It can be used for classification or regression.

Classification Example:

Regression Example:

The knn implementation can be used within the caret train() function for classification as follows:

The knn implementation can be used within the caret train() function for regression as follows:

2. Naive Bayes

The naiveBayes function is in the e1071 library and models the probabilistic of each attribute to the outcome variable independently. It can be used for classification problems.

A very similar naive bayes implementation (NaiveBayes from the klaR library) can be used with caret as follows:

3. Support Vector Machine

The ksvm function is in the kernlab package and can be used for classification or regression. It is a wrapper for the LIBSVM library and provides a suite of kernel types and configuration options.

These example uses a Radial Basis kernel.

Classification Example:

Regression Example:

The SVM with Radial Basis kernel implementation can be used with caret for classification as follows:

The SVM with Radial Basis kernel implementation can be used with caret for regression as follows:

4. Classification and Regression Trees

The rpart function in the rpart library provides an implementation of CART for classification and regression.

Classification Example:

Regression Example:

The rpart implementation can be used with caret for classification as follows:

The rpart implementation can be used with caret for regression as follows:

Other Algorithms

There are many other algorithms provided by R and available in caret.

I would advise you to explore them and add more algorithms to your own short list of must try algorithms on your next machine learning project.

You can find a mapping of machine learning functions and packages to their name in the caret package on this page:

This page is useful if you are using an algorithm in caret and want to know which package it belongs to so that you can read up on the parameters and get more out of it.

This page is also useful if you are using a machine learning algorithm directly in R and want to know how it can be used in caret.

Summary

In this post you discovered a diverse set of 8 algorithms that you can use to spot check on your datasets. Specifically:

  • Linear Regression
  • Logistic Regression
  • Linear Discriminant Analysis
  • Regularized Regression
  • k-Nearest Neighbors
  • Naive Bayes
  • Support Vector Machine
  • Classification and Regression Trees

You learned which packages and functions to use for each algorithm. You also learned how you can use each algorithm with the caret package that provides algorithm evaluation and tuning capabilities.

You can use these algorithms as a template for spot checking on your current or next machine learning project in R.

Your Next Step

Did you try out these recipes?

  1. Start your R interactive environment.
  2. Type or copy-paste the recipes above and try them out.
  3. Use the built-in help in R to learn more about the functions used.

Do you have a question. Ask it in the comments and I will do my best to answer it.


Frustrated With Your Progress In R Machine Learning?

Master Machine Learning With R

Develop Your Own Models in Minutes

…with just a few lines of R code

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

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, build models, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.


4 Responses to Spot Check Machine Learning Algorithms in R (algorithms to try on your next project)

  1. David Samson November 29, 2016 at 2:59 am #

    Hi Jason,

    This is a nice overview. 2 minor suggestions:
    1. You might want to add a section/additional post on how to easily compare between the various models, to determine which is best.
    2. For simplicity of your readers, you might attach a single file with all of the calls (rather than needing to copy from each of the little windows.)

    Thanks very much. — dbs

  2. V Mustafa March 17, 2017 at 1:07 am #

    Hi Jason,

    Thanks for this overview article. It certainly helps for a versatile package like caret.

    I was looking for a way to perform multi-class logistic regression using caret. glm is for 2-class only. Any idea how one can perform this using caret?

    Thanks – Mustafa

Leave a Reply