Open In App

How to Calculate MAE in R

Last Updated : 28 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are calculating the Mean Absolute Error in the R programming language.

Mean Absolute Error: It is the measure of errors between paired observations expressing the same phenomenon and the way to measure the accuracy of a given model. The formula to it is as follows:

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

Where,

  1. Σ: Sum
  2. yi: Observed value for ith observation
  3. xi: Predicted value for ith observation
  4. n: Total number of observations

Method 1:Using Actual Formulae

Mean Absolute Error (MAE) is calculated by taking the summation of the absolute difference between the actual and calculated values of each observation over the entire vector and then dividing the sum obtained by the number of observations in the vector.

In this example, we are taking the two vectors the actual vector and the calculated vector, and further, with the use of the basic formula of the MAE, we will be calculating it in R.

R




# a vector of integers for actual
actual = c(8,9,6,1,4,8,6,4,5,6)
  
# a vector of integers for actual
calculated = c(9,6,4,8,4,1,2,3,9,6)
  
n = 10
sum = 0
  
# for loop for iteration
for (i in 1:n){
  sum = abs(actual[i] - calculated[i]) + sum
}
error = sum/n
  
# display
error


Output:

[1] 2.9

Method 2:Using mae() function to calculate MAE  among vectors

With respect to measuring the Mean Absolute Error, here we have to call the mae() function from the Metrics package with the respected parameters passed to it to obtain the result.

Syntax to install the metrics package in the R console:

install.packages('Metrics")

Here, in this example, we are calculating the mae using the mae() function with the vector passed as its parameters the value to the vector is used the same as in the previous example.

R




# Import required package
library(Metrics)
  
# consider a vector of integers for actual
actual = c(8,9,6,1,4,8,6,4,5,6)
  
# consider a vector of integers for actual
calculated = c(9,6,4,8,4,1,2,3,9,6)
  
n = 10
sum = 0
  
# for loop for iteration
for (i in 1:n){
  sum = abs(actual[i] - calculated[i]) + sum
}
error = sum/n
  
# display
mae(actual,calculated)


Output:

[1] 2.9

Method 3: Calculate MAE for  Regression Model

In this method, we are using the regression model instead of the vector used in the previous examples as with the definition to the MAE said that it is used to get the accuracy of the built model, so in the approach, we are simply creating a dataframe of 10 columns and 3 rows containing integers value and with it, we are simply using the mae() function passed with the respected parameters of the columns of the built data frame to get the MAE of the model.

In this example, we are have created a dataframe of 3 rows and 10 columns and then further fitted this data frame to a linear regression model at the last, we are calculating the model accuracy MAE using the predicted value of the model vs actual values of it in R.

R




# Import required package
library(Metrics)
  
# Create Data frame
gfg_df <- data.frame(x1=c(4,8,9,4,1,8,9,4,1,6),
                 x2=c(1,3,2,8,9,9,6,7,4,1),
                 y=c(8,5,1,3,2,4,1,6,2,5))
  
# fit regression model
model <- lm(y~x1+x2, data=gfg_df)
  
# display mae
mae(gfg_df$y, predict(model))


Output:

[1] 1.782963


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads