Basic machine learning in R: a Tutorial (2023)

Coming from Python, I recently experimented with some basic machine learning in R. Here are some basic steps to do so.
James Birkenau



December 5, 2023


I started out in machine learning like a lot of others, curious and eagerly sifting through bits of data, trying to make sense of it all. The world of algorithms and data can be daunting at first, but I found R to be a good language for this. It’s one thing to grasp the theories behind machine learning, and quite another to see them come to life as your computer begins to predict, classify, and even grasp the nuances of the data you’re analyzing.

Introduction to Machine Learning in R

A screenshot showcasing rstudio with an r script containing machine learning code.

Machine learning has firmly established itself in the toolkit of any data scientist, and R, with its rich ecosystem of packages, is an excellent language to harness its power. I found starting with R to be a great way to get my feet wet with machine learning concepts without being overwhelmed by programming complexities.

First off, it’s crucial to understand what machine learning actually is. Machine learning encompasses algorithms that give computers the ability to learn from and make predictions on data. This can range from predicting housing prices to determining if an email is spam.

Now let’s get our hands dirty with some data. One of the greatest perks of R is the wealth of datasets available for our experimenting pleasure. The iris dataset, a classic in machine learning examples, is built-in and ready for use. It contains measurements of different iris flower species.

Here’s how you can load it and take a quick glance:


The head function lets us peek at the first few rows of the dataset. It’s always a good practice to familiarize ourselves with the data format and structure before we plow ahead.

So, what’s next after loading data? Plotting and understanding the relationships within it. The pairs function in R gives us an easy way to visualize the data in pairs, which can provide insight into correlations between features.

pairs(iris[1:4], main = "Iris Data", pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])

In machine learning, it is paramount to pick the right model. There’s a variety of models available, but a good starting point for classification problems is the k-nearest neighbors algorithm (KNN). It’s intuitive and straightforward, which makes for an excellent starting point.

The code to train and test a KNN model looks something like this:

# We'll split data into a train and test set
indexes <- sample(1:nrow(iris), nrow(iris) * 0.7)
train_data <- iris[indexes,]
test_data <- iris[-indexes,]
train_label <- iris[indexes, 5]
test_label <- iris[-indexes, 5]

# Apply the knn algorithm
knn_pred <- knn(train = train_data[,1:4], test = test_data[,1:4], cl = train_label, k=5)

# We can now print out the predictions

Before running this code, you’d need to split your datasets into training and testing sets—a process I find to be akin to a chef carefully dividing ingredients for the perfect recipe. The sample function does the trick wonderfully for creating random indexes used in data splitting. Remember, testing on separate data is key to knowing if our model truly learned something or just memorized the training data.

Lastly, let me plant a seed of advice: start with simple models and increase complexity as needed. The temptation is to latch onto the newest, shiniest algorithms, but often, simple models will surprise you with their effectiveness, and they are also easier to understand and explain.

As you go on with the tutorial, you’ll see that iterating on a model, interpreting results, and then improving it is a cyclical process—a machine learning practitioner is always on the lookout for ways to squeeze out more performance from their models. After establishing a baseline with KNN or another simple algorithm, you’ll learn how to use other algorithms, tune your models, work with different types of data, and even venture into more advanced machine learning techniques.

Understanding and implementing machine learning models in R is a rewarding endeavor—one that I have found not only intellectually stimulating, but also immensely practical, and I’m confident you will too.

Setting Up Your R Environment

Illustration of the rstudio interface and the packages installation process.

Before we jump into the intricacies of machine learning with R, it’s essential to get your R environment up and running. For those who might be new to R, it’s an open-source programming language that’s great for data analysis and visualizations, and yes, machine learning too. I reckon the setup part isn’t the most thrilling segment of learning, but trust me, having a slick working environment makes life easier in the long run. And hey, we’ll get through this together.

You’ll first want to download R from CRAN, the Comprehensive R Archive Network. Choose the version compatible with your operating system. Here’s how I do it, copy and paste these lines into your browser’s address bar, substituting ‘mac’ with ‘win’ or ‘linux’ if you need to:

Once you have R installed, I highly recommend using RStudio as your IDE (Integrated Development Environment). It’s user-friendly and enhances R’s interface significantly, making your coding experience a lot nicer. Snag the appropriate RStudio version from here:

Post-installation, launch RStudio. Here’s a tip: RStudio comes with panes for multiple purposes - keep an eye on the Console pane, since that’s where you’ll see the result of your code executions. Now, to test whether R is ready to work, I typically run a simple command just to make sure. Let’s do that together:

# Check R version

You should see the version of R printed in the console, confirming that R is ready to ply its trade.

Next up is setting up the necessary packages. Think of packages like extra tools R can use - for machine learning, you’ll want tools like caret or tidymodels. Install them using the install.packages() function. It’s as easy as this:

# Install caret or tidymodels

Once they’re installed, make sure they’re ready to use with:

# Load libraries

Now, to keep our projects organized, we’ll create a new R project within RStudio—an essential step for maintaining sanity when projects get more complex.

# In RStudio, go to:
# File -> New Project -> New Directory -> New Project
# Enter your project's name and decide on a directory to house the project.

This will create a fresh workspace with its own script, workspace and history – a clean slate.

Last but not least, let’s get version control into the picture. If you’re not familiar, think of version control as a time machine for your code. We’ll use Git, which is nicely integrated into RStudio. Set it up in RStudio and connect it to a repository from GitHub or a similar service. This allows you to keep track of changes and collaborate with ease. Here’s how you’d initialize a Git repository for version control within your RStudio project:

# In RStudio, go to:
# Tools -> Version Control -> Project Setup
# Choose ‘Git’ and follow instructions to initialize a repository.

And there you have it. Your R environment is primed with all the groundwork laid out. We’ve installed R, decided on an IDE, added necessary packages, organized our workspace with a new project, and introduced version control. It’s no rocket science and I can tell you’ve nailed it. Now, you’re set to plunge into crafting that first machine learning model, without any hiccups, I hope. Let’s roll up our sleeves and get to the fun part!

Your First Machine Learning Model in R

A flowchart demonstrating the steps to create a basic machine learning model in r.

Creating your first machine learning model in R can be quite an adventure. I remember the first time I dipped my toes into machine learning - it was exhilarating to see my machine interpret data and make predictions. Now, I’ll take you through this process step by step.

First, ensure you have the necessary packages installed. You’ll want caret, which stands for Classification And REgression Training. This package streamlines the model training process for complex regression and classification problems.


Let’s say we’re working with the famous Iris dataset. This is a beginner’s goldmine because it’s relatively simple and widely understood. The dataset includes various measurements of iris flowers and their species. The goal is often to predict the species based on the measurements.

Load the dataset, which comes by default with R:


Before we throw our data into a model, take a quick look with summary(iris) to understand what you’re working with.

Now, let’s split this dataset into two sets: one to train the model and one to test it. A common split is 70% training, 30% testing. The createDataPartition() function from caret can help with that.

set.seed(123) # for reproducibility
trainIndex <- createDataPartition(iris$Species, p = 0.7, list = FALSE)
trainData <- iris[trainIndex, ]
testData <- iris[-trainIndex, ]

When I first built a model, I kept it simple with a linear discriminant analysis (LDA). It’s a basic technique for classification problems.

Let’s train an LDA model using trainData.

ldaModel <- train(Species~., data=trainData, method='lda')

The formula Species~. tells R to predict Species using all other columns in trainData. The method='lda' specifies the type of model.

Once the model is trained, it’s time to make predictions on testData and see how the model performs.

predictions <- predict(ldaModel, testData)

Check how well your model did. Most beginners, including me, start by looking at the confusion matrix. It’s an intuitive way to see where your model is going right or wrong.

confusionMatrix(predictions, testData$Species)

These results can tell you how many predictions were correct and where the model may have slipped up.

Remember, the point of this first model isn’t to break records but to lay the foundation for understanding, iterating, and improving. It took me several tries to get a grip on the nuances and even more to refine and choose more complex models and tuning parameters. The beauty of caret and R lies in how effortlessly you can explore these aspects once you’ve got the basics down.

And that’s it! You’ve just built and evaluated your first machine learning model in R. Sure, as you delve deeper, you’ll learn about cross-validation, hyperparameter tuning, and other algorithms. But for now, pat yourself on the back, you’ve taken the first big step into machine learning in R!

Evaluating Model Performance

A graph displaying the results of a model accuracy test like a confusion matrix or roc curve.

As I transition from training a model to understanding how well it’s likely to perform in the real world, evaluating its performance becomes critical. This takes us beyond simply getting a model up and running; it’s about making sure the model we’ve created actually makes good predictions.

To start with, we need to consider evaluation metrics. There are different metrics for different types of problems. For example, accuracy is a typical measure for classification problems, but for a regression problem, we might look at mean squared error (MSE) or mean absolute error (MAE). Let’s say I’ve built a classification model. Here’s how I’d calculate accuracy in R:

# Assuming 'pred' is your predictions and 'actual' is the true labels
accuracy <- sum(pred == actual) / length(actual)
print(paste("Accuracy:", accuracy))

That’s a start, but accuracy isn’t always the best measure, especially if my classes are imbalanced. In such cases, a confusion matrix helps me to see where the model is going right or wrong:

confusionMatrix(as.factor(pred), as.factor(actual))

The caret package provides a handy confusionMatrix function that gives not just the matrix, but also a slew of other useful metrics like sensitivity and specificity.

Now, say I’m working with a regression model. To get the MSE or MAE, it’s a bit different:

mse <- mean((pred - actual)^2)
mae <- mean(abs(pred - actual))
print(paste("Mean Squared Error:", mse))
print(paste("Mean Absolute Error:", mae))

We can’t look at these metrics in isolation, though. Sometimes it helps to visualize the errors. Let’s say I want to see how errors are distributed. A quick plot of actual vs. predicted values can be enlightening:

ggplot() +
geom_point(aes(x=actual, y=pred), color="blue") +
geom_abline(intercept=0, slope=1, color="red") +
ggtitle("Actual vs. Predicted")

That red line represents where the points would lie if my predictions were perfect. Deviations from that line show me the errors.

An important aspect of evaluating model performance is cross-validation. I don’t want to make the mistake of just evaluating the model on the data I used to train it. That could lead me to think my model is performing better than it actually is. So, I use the train function from the caret package and specify my cross-validation method:

control <- trainControl(method="cv", number=10) # 10-fold cross-validation
model_cv <- train(target~., data=train_data, method="lm", trControl=control)

The trControl parameter lets me specify that I want to use 10-fold cross-validation, which is a good balance most of the time.

But before you run off and use these techniques, remember, each dataset and problem is unique—there’s no universal “best” evaluation method. What’s important is understanding the why behind each metric and what it tells me about my model within the specific context it’s being applied.

Evaluating model performance isn’t just a one-off check; it’s an iterative process. Each time I adjust my model, I go through these steps again to see if the changes are truly improvements. This is how I gradually hone my model’s predictive abilities, always with an eye on the metrics that matter most for my project’s objectives.

Improving Your Model and Next Steps

A roadmap graphic indicating potential paths for learning more advanced machine learning techniques in r.

In the journey of picking up machine learning with R, building a model is just the beginning. I’ve learned that refining the model and iterating upon it is where real progress is made. After evaluating our initial model’s performance, it’s clear that there’s always room for improvement.

One method I find effective is tuning hyperparameters. Hyperparameters are the settings that can be adjusted to control the behavior of a machine learning algorithm. In R, you can use the caret package for this purpose. Here’s an example where I’ll use the train() function to tune a random forest model:

# Assuming we have a dataset named 'training' and 'target_variable'
tuneGrid <- expand.grid(.mtry = seq(2, 14, by = 2))
control <- trainControl(method = "repeatedcv", number = 10, repeats = 3)

tunedModel <- train(target_variable ~ ., data = training,
method = "rf",
metric = "Accuracy",
tuneGrid = tuneGrid,
trControl = control)
summary(tunedModel) # Provides a summary of the tuned model

By playing with the number of variables selected at each split (mtry), the model’s performance can often be pushed further. The trainControl() function sets the method for cross-validation, helping to avoid overfitting.

Another aspect I consider crucial is feature engineering. It’s about creating new input features from your existing ones and can make a huge difference. For example, combining two variables to create a new one that provides more distinct information can boost model accuracy. To implement this, I might add a new column to our data frame:

training$interactionFeature <- training$feature1 * training$feature2

Then, you could go on to include this new feature in your model training process.

It’s mandatory to stay updated with the latest research and trends in machine learning. I regularly check out resources from universities and leading researchers. For instance, Stanford University’s Machine Learning course by Andrew Ng on Coursera is a must-watch. GitHub repositories like tidymodels offer a rich suite of packages that make machine learning in R more tidy and transparent. You can find it here:

I also believe in the value of sharing and learning from others. Communities like GitHub, Stack Overflow, r/machinelearning on Reddit, and YCombinator’s Hacker News are excellent for collaborating and getting feedback. Don’t hesitate to showcase your work and ask for advice.

Lastly, documenting progress is key. Take notes and write codes as you experiment with different models. Not only does this help with keeping track of what you’ve done, but it also facilitates learning from mistakes. Here’s how simple documentation could look like:

# This code block is for a simple linear regression model
# Dataset: mtcars
# Date: YYYY-MM-DD
# Objectives: Predicting miles per gallon (mpg) from a set of variables

linearMod <- lm(mpg ~ wt + qsec + factor(am), data = mtcars)
summary(linearMod) # Output the summary of our regression model

In summary, the path to improving your model in R is filled with numerous small but significant steps. Hyperparameter tuning, feature engineering, staying informed, collaboration, and documentation – all these facets combine to push your machine learning capabilities further. Treat it as a continuous cycle; build, test, learn, and iterate. The machine learning landscape evolves rapidly, and so should your methods and models.