Skip to content

median methods

3 messages · Rob Hyndman, Brian Ripley

#
On Sat, 26 Apr 2008, Rob Hyndman wrote:

            
Not without a reasoned case -- see 'Writing R Extensions' as to why it is 
a non-trivial change that affects all existing methods (and there are 
some) -- also S4 setMethod("median") calls (which there are too).

There is also an argument as to where the ... should come -- probably

function (x, ..., na.rm = FALSE)

(but that could break some existing calls), and why should na.rm be on the 
generic as well (it is not for mean nor quantile, for example)?

So there have to be some pretty compelling arguments in favour.
Hmm, we do explicitly ask you not to do send that.
#
Brian. The example I have in mind is for functional data where there
are several ways to define a median. For example, it can be defined as
the pointwise median of a set of functions {f_j(x); j=1,...,n}. Or it
can be defined as m(x) = arg min_{g(x)} \sum_j |g(x) - f_j(x)| dx. The
latter can be calculated using several different algorithms, one of
which is the Hossjer-Croux algorithm.  So I want to write a function
of the form

median.fd <- function(x, method=c("hossjer-croux","pointwise"))

where x is of class "fd" (functional data). Without a ... argument in
the generic median function, I can't do this.

There are no doubt other similar examples where an additional argument
(or more) is required.

Furthermore, this would make it consistent with mean() and quantile().

You can easily retain existing code by defining generic median as
median(x,...). Then if an na.rm argument is passed without name, it
will go through to median.default(x,na.rm) without a hitch.  It should
not affect existing methods since they must all have na.rm as a second
argument also.

So I am suggesting median(x, ...) which makes it more flexible for
method writers, and I don't think it causes a problem for existing
code.

Best wishes,
Rob
On 27/04/2008, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote: