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
How to Calculate MAE in R
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,
- Σ: Sum
- yi: Observed value for ith observation
- xi: Predicted value for ith observation
- n: Total number of observations