[RsR] Case where options in mad() seemingly have no effect

John Buggeln jhb @end|ng |rom ude|@edu
Tue Jul 1 17:39:34 CEST 2025


Hello!

I have what seems to be a quick question. Below I have pasted the code for the mad() function, from “package::stats”. 

function (x, center = median(x), constant = 1.4826, na.rm = FALSE, 
  low = FALSE, high = FALSE) 
{
  if (na.rm) 
    x <- x[!is.na(x)]
  n <- length(x)
  constant * if ((low || high) && n%%2 == 0) {
    if (low && high) 
      stop("'low' and 'high' cannot be both TRUE")
    n2 <- n%/%2 + as.integer(high)
    sort(abs(x - center), partial = n2)[n2]
  }
  else median(abs(x - center))
}

The documentation states that the options “low” and “high” update the way the median is being calculated. However, in the code there is no update to “center" from the value of low or high. What am I missing here? There must be something fundamental I don’t understand about R implementing this function. 

I also have a small example where the options seemingly have to effect on the MAD calculation.

###################VERSION INFO#############################

R 4.5.0 on MacOSX 14.5 (M2 Apple Silicon)
Sys.getlocale() is "en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8”

Information on package ‘stats’

Description:

Package: stats
Version: 4.5.0
Priority: base
Title: The R Stats Package
Author: R Core Team and contributors worldwide
Maintainer: R Core Team <do-use-Contact-address using r-project.org>
Contact: R-help mailing list <r-help using r-project.org>
Description: R statistical functions.
License: Part of R 4.5.0
Imports: utils, grDevices, graphics
Suggests: MASS, Matrix, SuppDists, methods, stats4
NeedsCompilation: yes
Encoding: UTF-8
Enhances: Kendall, coin, multcomp, pcaPP, pspearman, robustbase
Built: R 4.5.0; aarch64-apple-darwin20; 2025-04-11 20:15:23 UTC; unix

###################EXAMPLE#############################

> var <-scan(text="3.2 3.6 4.3 4.3 4.4 4.7 5.1 5.3 5.5 5.8 6.7 8.9")
Read 12 items
> mad(var) 
[1] 0.88956
> mad(var, high=TRUE)
[1] 0.88956
> mad(var, low=TRUE)
[1] 0.88956

This is confusing because when calculated manually the MAD is different:

> # Manually
> median(abs(var-median(var)))*1.4826
[1] 0.88956
> median(abs(var-4.7))*1.4826 # LOW for median
[1] 1.03782
> median(abs(var-5.1))*1.4826 # HIGH for median
[1] 1.11195


Best,

John Buggeln, MS
PhD Candidate
University of Delaware


More information about the R-SIG-Robust mailing list