Mean Absolute Percentage Error in R

Filed Under: R Programming
MAPE In R Programming

Hello, readers! Today, we will be having a look at an important error metric in Data Science and Machine Learning — Mean Absolute Percentage Error in R programming, in detail.

So, let us begin!!

What is Mean Absolute Percentage Error in R?

Error metrics help us evaluate the efficiency of the model. That is, by using error metrics, we can evaluate the performance of a model on the fed dataset and then decide which model would better suit the dataset.

There are various regression error metrics available such as,

Today, we will be discussing about MAPE in detail.

Mean Absolute Percentage Error is a statistical error metric that estimates the accuracy of a model for the input dataset.

That is, it is a loss function that estimates the error rate termed by the model on the data.

Using MAPE, one can understand the difference between the actual and the predicted values in terms of misclassification error as well as accuracy.

Have a look at the below formula!


Here, we initially calculate the absolute differences between the actual and the predicted values. And, then we frame the mean of this difference to get the end result i.e. MAPE.

Usually, lower the value of MAPE, better fit is the applied model.

Calculating MAPE using the MAPE() function in R

R MLmetrics library provides us with MAPE() function to calculate the value of misclassification error for the fed values.


#Removed all the existing objects
rm(list = ls())


y_actual = c(10,20,30,40,50)		 
y_predict = c(9.8,19.8,30,40,52.5)	 
MAPE_value = MAPE(y_actual, y_predict) 




Practical Implementation of MAPE on a dataset!

Here, we have made use of Bike Rental Count Prediction dataset, wherein our main task is to estimate the number of customers who would opt for a bike based on certain environmental and other factors. You can find the dataset here!

Initially, we load the dataset into the R environment using read.csv() function. Further to which, we perform sampling of the dataset into train and test portions using createDataPartition() method.

Then, we build an inbuilt function to calculate MAPE(). At first, we calculate the absolute differences between the actual and predicted values and then find the mean of this value to get the value of MAPE.

At last we have applied linear regression model and have tested the metrics defined.


#Removed all the existing objects
rm(list = ls())
#Setting the working directory
setwd("D:/Ediwsor_Project - Bike_Rental_Count/")

#Load the dataset
bike_data = read.csv("day.csv",header=TRUE)

#######################SAMPLING OF DATA -- Splitting of Data columns into Training and Test dataset###########################
categorical_col_updated = c('season','yr','mnth','weathersit','holiday')
bike = bike_data
bike =,categorical_col_updated)

#Separating the depenedent and independent data variables into two dataframes.
split_val = createDataPartition(bike$cnt, p = 0.80, list = FALSE) 
train_data = bike[split_val,]
test_data = bike[-split_val,]

MAPE = function(y_actual,y_predict){

linear_model = lm(cnt~., train_data) #Building the Linear Regression Model on our dataset
linear_predict=predict(linear_model,test_data[-27]) #Predictions on Testing data
LR_MAPE = MAPE(test_data[,27],linear_predict)
Accuracy_Linear = 100 - LR_MAPE

print("MAPE: ")
print('Accuracy of Linear Regression: ')


It results into 17.6% misclassification error and thus we obtain an accuracy of 82.3%, which is pretty good!

> print("MAPE: ")
[1] "MAPE: "
> print(LR_MAPE)
[1] 17.61674
> print('Accuracy of Linear Regression: ')
[1] "Accuracy of Linear Regression: "
> print(Accuracy_Linear)
[1] 82.38326


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned and till then, Happy learning!! 馃檪

Generic selectors
Exact matches only
Search in title
Search in content