Machine Learning in R: mlr Tutorial

This web page provides an in-depth introduction to Machine Learning in R: mlr, a framework for machine learning experiments in R.

In this tutorial, we focus on basic functions and applications. More detailed technical information can be found in the manual pages which are regularly updated and reflect the documentation of the current package version on CRAN.

For a quick overview please check out the mlr cheatsheet!

Offline versions of this tutorial are also available for download:

The tutorial aims to walkthrough basic data analysis tasks step by step. We will use simple examples from classification, regression, cluster and survival analysis to illustrate the main features of the package.

Enjoy reading!

Quick start

Here we show the mlr workflow to train, make predictions, and evaluate a learner on a classification problem. We walk through 5 basic steps that work on any learning problem or method supported by mlr.


## 1) Define the task
## Specify the type of analysis (e.g. classification) and provide data and response variable
task = makeClassifTask(data = iris, target = "Species")

## 2) Define the learner
## Choose a specific algorithm (e.g. linear discriminant analysis)
lrn = makeLearner("classif.lda")

n = nrow(iris)
train.set = sample(n, size = 2/3*n)
test.set = setdiff(1:n, train.set)

## 3) Fit the model
## Train the learner on the task using a random subset of the data as training set
model = train(lrn, task, subset = train.set)

## 4) Make predictions
## Predict values of the response variable for new observations by the trained model
## using the other part of the data as test set
pred = predict(model, task = task, subset = test.set)

## 5) Evaluate the learner
## Calculate the mean misclassification error and accuracy
performance(pred, measures = list(mmce, acc))
#> mmce  acc 
#> 0.04 0.96