Table of Contents
Mean Absolute Error (MAE) in R is calculated by using the “MAE” function from the “Metrics” library. This function takes a vector of observed values and a vector of predicted values as input and outputs a single numerical value which is the mean absolute error between the two vectors. This value is the average absolute difference between the predicted values and the observed values.
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 <- c(12, 13, 14, 15, 15, 22, 27, 29, 29, 30, 32) predicted <- c(11, 13, 14, 14, 16, 19, 24, 30, 32, 36, 30) #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 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 <- data.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 <- lm(y~x1+x2, data=df) #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.