17.6 C
London
Monday, July 21, 2025
HomeStatistics TutorialRHow to Calculate Mean Absolute Error in R

How to Calculate Mean Absolute Error in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

In statistics, the mean absolute error (MAE) is a way to measure the accuracy of a given model. It is calculated as:

MAE = (1/n) * Σ|yi – xi|

where:

  • Σ: A Greek symbol that means “sum”
  • yi: The observed value for the ith observation
  • xi: The predicted value for the ith observation
  • n: The total number of observations

We can calculate the mean absolute error in R by using the mae(actual, predicted) function from the Metrics package.

This tutorial provides two examples of how to use this function in practice.

Example 1: Calculate Mean Absolute Error Between Two Vectors

The following code shows how to calculate the mean absolute error between a vector of observed values and a vector of predicted values:

library(Metrics)

#define observed and predicted values
observed #calculate mean absolute error between vectors
mae(observed, predicted)

[1] 1.909091

The mean absolute error (MAE) turns out to be 1.909.

This tells us that the average absolute difference between the observed values and the predicted values is 1.909.

Example 2: Calculate Mean Absolute Error for a Regression Model

The following code shows how to fit a regression model in R and then calculate the mean absolute error between the predictions made by the model and the actual observed response values:

library(Metrics)

#create data
df frame(x1=c(1, 3, 3, 4, 4, 6, 6, 8, 9, 3),
                 x2=c(7, 7, 4, 10, 13, 12, 17, 19, 20, 34),
                 y=c(17, 18, 19, 20, 24, 28, 25, 29, 30, 32))

#view first six rows of data
head(df)

  x1 x2  y
1  1  7 17
2  3  7 18
3  3  4 19
4  4 10 20
5  4 13 24
6  6 12 28

#fit regression model
model #calculate MAE between predicted values and observed values
mae(df$y, predict(model))

[1] 1.238241

The mean absolute error (MAE) turns out to be 1.238.

This tells us that the average absolute difference between the observed values and the predicted values is 1.238.

In general, the lower the value for the MAE the better a model is able to fit a dataset. When comparing two different models, we can compare the MAE of each model to know which one offers a better fit to a dataset.

Additional Resources

Mean Absolute Error Calculator
How to Calculate Mean Absolute Error in Excel
How to Calculate Mean Absolute Error in Python

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories