[R] Levene's test

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Sep 1 08:03:06 CEST 2000

On Fri, 1 Sep 2000, Murray Jorgensen wrote:

> I'm teaching one-way anova at the moment and throwing in a few snippets of
> R on the way. I'd like to present a modified version of Levene's test for
> homogeneity of variance between groups and give some R code for it.
> The test that I was thinking of basically does an anova on a modified
> response variable that is the absolute value of the difference between an
> observation and the median of its group (more robust than Levene's original
> choice, the mean).
> A colleague has suggested an approach involving the functions 
> ord() split() sapply() as.vector() rep()
> which I have not quite got working yet. Even if I do get it working it
> seems to make fairly heavy weather of would I would have thought was a
> simple enough transformation. I'd like something that I could show my
> students without discouraging them!
> Suggestions gratefully received!

Is this the sort of thing you want?

Levene <- function(y, group)
    group <- as.factor(group)  # precautionary
    meds <- tapply(y, group, median)
    resp <- abs(y - meds[group])
    anova(lm(resp ~ group))[1, 4:5]

> data(warpbreaks)
> attach(warpbreaks)
> Levene(breaks, tension)
      F value  Pr(>F)
group   2.818 0.06905

I could (and probably would) dress it up with a formula interface,
but that would obscure the simplicity of the calculation.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list