Skip to contents

Calculates the mean absolute deviation from a center point, typically the sample mean or the median.

Usage

meanAD(x, weights = NULL, center = meanX, na.rm = FALSE)

Arguments

x

a vector containing the observations

weights

a numerical vector of weights the same length as x giving the weights to use for elements of x

center

a numeric center or a function applied to x. User-defined functions must accept weights when weights are supplied. Defaults to meanX().

na.rm

logical; whether to remove missing values. Defaults to FALSE.

Value

a numeric scalar containing the mean absolute deviation

Details

The meanAD function calculates the mean absolute deviation from the mean value (or from another supplied center point) of x, after having removed NA values (if requested): $$\frac{1}{n} \cdot \sum_{i=1}^{n}\left | x_{i}-c \right | \; \; \; \textup{where} \; c=mean(x) \; \textup{or} \; c=med(x)$$
The function supports the use of weights. The default function for the center value meanX() has a weights arguments, too. If a user defined function is used it must be assured that it has a weights argument.

Note

Rewritten following an idea of Danielle Navarro (aad in the lsr package).

See also

Examples


x <- runif(100)
meanAD(x)
#> [1] 0.2136102

speed <- c(58, 88, 40, 60, 72, 66, 80, 48, NA)
meanAD(speed)
#> [1] NA
meanAD(speed, na.rm=TRUE)
#> [1] 12.5


# using the median as centerpoint
x <- c(2,3,5,3,1,15,23)

meanAD(x, center=mean)
#> [1] 6.612245
meanAD(x, center=median)
#> [1] 5.285714

# define a fixed center
meanAD(x, center=4)
#> [1] 5.428571

# use of weights
meanAD(x=0:6, weights=c(21,46,54,40,24,10,5))
#> [1] 1.1825