How to Calculate MAE in R Last Updated : 02 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Mean Absolute Error (MAE) is a metric used to measure the accuracy of a model by calculating the average of the absolute differences between the observed (actual) values and the predicted values. It is a commonly used metric in regression analysis and model evaluation because it gives a clear understanding of how far off a model's predictions are from the actual values. A lower MAE indicates a more accurate model, while a higher MAE suggests larger discrepancies between predictions and actual values.Formula for MAEMAE = \frac{1}{n} \sum_{i=1}^{n} |y_i - x_i|Where:y_i = Observed value for the i^{th} observationx_i = Predicted value for the i^{th} observationn = Total number of observations\Sigma = Sum of the absolute differencesMethods to Calculate MAE in RWe will discuss the different methods to calculate Mean Absolute Error (MAE) in R programming language.1. Using the Actual FormulaIn this method, we manually calculate the Mean Absolute Error (MAE) by applying the formula for the absolute differences between the actual and predicted values. This method involves using a loop to iterate over the vectors of actual and predicted values, summing the absolute differences, and then dividing by the number of observations.The vectors actual and calculated represent the observed and predicted values.We calculate the absolute difference for each pair of actual and predicted values using the abs() function.The sum of all these differences is stored in the variable sum.Finally, we divide the total sum by the number of observations n to get the MAE. R actual = c(8,9,6,1,4,8,6,4,5,6) calculated = c(9,6,4,8,4,1,2,3,9,6) n = 10 sum = 0 for (i in 1:n){ sum = abs(actual[i] - calculated[i]) + sum } error = sum / n error Output:[1] 2.92. Using the mae() Function from the Metrics PackageThis method simplifies the calculation by using the mae() function from the Metrics package, which directly computes the Mean Absolute Error for given vectors of actual and predicted values.Install and load the Metrics package to access the mae() function.The mae() function takes two arguments: the actual values and the predicted values, and it returns the MAE directly. R install.packages('Metrics') library(Metrics) actual = c(8,9,6,1,4,8,6,4,5,6) calculated = c(9,6,4,8,4,1,2,3,9,6) mae(actual, calculated) Output:[1] 2.93. Calculating MAE for a Regression ModelIn this method, we calculate the MAE for a regression model. First, a regression model is built using a data frame , and then the predicted values are compared to the actual values to calculate the MAE. This method helps in evaluating the performance of the model.A data frame is created with predictor variables (x1, x2) and the response variable (y).A linear regression model is fitted using lm() function.The mae() function from the Metrics package is used to calculate the MAE between the actual values (y) and the predicted values obtained from the model. R install.packages('Metrics') library(Metrics) 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)) model <- lm(y ~ x1 + x2, data = gfg_df) mae(gfg_df$y, predict(model)) Output:[1] 1.782963These three methods provide different approaches to calculating the Mean Absolute Error (MAE) in R, from manual calculation to using built-in functions for easier computation, and evaluating model performance in regression analysis. Comment More infoAdvertise with us Next Article How to Calculate Geometric Mean in R? G geetansh044 Follow Improve Article Tags : R Language Geeks Premier League Geeks-Premier-League-2022 R Machine-Learning R Machine Learning AI-ML-DS With R R Language +2 More Similar Reads How to Calculate MAPE in R In this article, we are going to see how to calculate MAPE in R Programming Language. MAPE: It is an acronym for mean absolute percentage error. MAPE is used to measure the accuracy of a forecast system. The accuracy is measured in terms of percentage. MAPE can be calculated using the following form 3 min read How to Calculate SMAPE in R SMAPE stands for symmetric mean absolute percentage error. It is an accuracy measure and is used to determine the predictive accuracy of models that are based on relative errors. The relative error is computed as: relative error =  x / y Where x is the absolute error and y is the magnitude of exact 3 min read How to Calculate SMAPE in Excel? In statistics, we often use Forecasting Accuracy which denotes the closeness of a quantity to the actual value of that particular quantity. The actual value is also known as the true value. It basically denotes the degree of closeness or a verification process that is highly used by business profess 3 min read How to Calculate SMAPE in Python? In this article, we will see how to compute one of the methods to determine forecast accuracy called the Symmetric Mean Absolute Percentage Error (or simply SMAPE) in Python. The SMAPE is one of the alternatives to overcome the limitations with MAPE forecast error measurement. In contrast to the me 3 min read How to Calculate Geometric Mean in R? In this article, we will discuss how to calculate the Geometric Mean in R Programming Language.We can define the geometric mean as the average rate of return of a set of values calculated using the products of the terms.Method 1: Compute Geometric Mean ManuallyIn this method, the user can calculate 2 min read How to Calculate Variance in MATLAB? Pre-requisites: Calculate Variance In statistical mathematics, Variance is the measure of dispersion of a given data from its average value. This is a very important quantity in statistics and many other fields like Machine Learning and AI. Variance in MATLAB:MATLAB provides a simple function to ca 2 min read Like