The Median Absolute Deviation is calculated in R Language using the mad() function. It is a statistical measurement of a dataset's dispersion or variability. Because it is resistant to outliers and extreme values, the MAD can serve as a robust alternative to standard deviation.
The Median Absolute Deviation (MAD) is calculated using the following formula:
MAD = median (|xi - x)|
where:
- xi = Each observation in the dataset is represented.
- The dataset's median is represented as median(x).
Syntax:
mad(x)
Parameters:
- x : A vector of numeric values.
Calculate MAD for vectors
We can calculate the Median Absolute Deviation for vectors.
Example 1:
# Creating a vector
x <- c(1:9)
# Calling mad() Function
mad(x)
Output
[1] 2.9652
Example 2:
# Creating a vector
x <- c(1, 4, 2, 3, 7, 3, 8, 9, 2)
# Calling mad() Function
mad(x)
Output
[1] 1.4826
Calculate MAD for a Single Column in a Data
We can calculate MAD for a single column in a data set so we can take the iris dataset.
data(iris)
#calculate the mad for single columns.
mad(iris$Sepal.Width)
Output
[1] 0.44478
Calculate MAD for multiple columns in a data
To find the MAD for multiple columns, we can apply the apply() or sapply() function together with the mad() function. Here's how you can find the MAD for all the columns except the categorical ones in the iris dataset:
library(dplyr)
# remove Species column from dataset
data=select(iris,-('Species'))
# calculate the mad for all columns
sapply(data,mad)
Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width
1.03782 0.44478 1.85325 1.03782
The sapply() function uses the mad() function for every column in the dataset except for the categorical Species column. The MAD value for every numerical column is returned.
Related Articles: