\HeaderA{mad}{Median Absolute Deviation}{mad}
\keyword{univar}{mad}
\keyword{robust}{mad}
\begin{Description}\relax
Compute the median absolute deviation, i.e., the (lo-/hi-) median of
the absolute deviations from the median, and (by default) adjust by a
factor for asymptotically normal consistency.
\end{Description}
\begin{Usage}
\begin{verbatim}
mad(x, center = median(x), constant = 1.4826, na.rm = FALSE,
    low = FALSE, high = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] a numeric vector.
\item[\code{center}] Optionally, the centre: defaults to the median.
\item[\code{constant}] scale factor.
\item[\code{na.rm}] if \code{TRUE} then \code{NA} values are stripped
from \code{x} before computation takes place.
\item[\code{low}] if \code{TRUE}, compute the \dQuote{lo-median}, i.e., for even
sample size, do not average the two middle values, but take the
smaller one.
\item[\code{high}] if \code{TRUE}, compute the \dQuote{hi-median}, i.e., take the
larger of the two middle values for even sample size.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The actual value calculated is \code{constant * cMedian(abs(x - center))}
with the default value of \code{center} being \code{median(x)}, and
\code{cMedian} being the usual, the \dQuote{low} or \dQuote{high} median, see
the arguments description for \code{low} and \code{high} above.

The default \code{constant = 1.4826} (approximately
\eqn{1/\Phi^{-1}(\frac 3 4)}{1/ Phi^(-1)(3/4)} = \code{1/qnorm(3/4)})
ensures consistency, i.e.,
\deqn{E[mad(X_1,\dots,X_n)] = \sigma}{}
for \eqn{X_i}{} distributed as \eqn{N(\mu,\sigma^2)}{} and large \eqn{n}{}.

If \code{na.rm} is \code{TRUE} then \code{NA}
values are stripped from \code{x} before computation takes place.
If this is not done then an \code{NA} value in
\code{x} will cause \code{mad} to return \code{NA}.
\end{Details}
\begin{SeeAlso}\relax
\code{\LinkA{IQR}{IQR}} which is simpler but less robust,
\code{\LinkA{median}{median}}, \code{\LinkA{var}{var}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
mad(c(1:9))
print(mad(c(1:9),     constant=1)) ==
      mad(c(1:8,100), constant=1)       # = 2 ; TRUE
x <- c(1,2,3, 5,7,8)
sort(abs(x - median(x)))
c(mad(x, co=1), mad(x, co=1, lo = TRUE), mad(x, co=1, hi = TRUE))
\end{ExampleCode}
\end{Examples}

