[R] levene.test

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jul 14 11:51:57 CEST 2010


On Wed, 2010-07-14 at 01:37 -0700, martanair wrote:
> I am trying to use Levene's test (of package car), but I do 
> not understand quite well how to use it. '?levene.test' does 
> not unfortunately provide any example.

Err, yes it does, several examples in fact.

>  My data are in a data 
> frame and correspond to 1 factor plus response. Could 
> someone please give me an example about how to use the command 
>  
> levene.test(y, group) 
> 
> Thanks in advance, 
>  
> marta

Here is an example where data aren't in a data.frame.

require(car)
## dummy data with know different variances
set.seed(123)
Y <- c(rnorm(20, sd = 6), rnorm(20, sd = 1))
G <- gl(2, 20, labels = paste("Group", 1:2, sep = ""))
## Y is the thing we measured on our two groups,
## G is the group indicator

## levene test of different variances
levene.test(y = Y, group = G)

## Gives:
Levene's Test for Homogeneity of Variance
      Df F value    Pr(>F)    
group  1  21.033 4.792e-05 ***
      38                      
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

If Y and G were in a data frame, then the easiest way to run it is:

## put data above in to a data frame
dat <- data.frame(Y = Y, G = G)
head(dat)
str(dat)
## ok so G is a factor within data frame

## run levene's test
with(dat, leven.test(y = Y, group = G))

?leven.test even shows you this usage!

## or if you like $
levene.test(dat$Y, dat$G)

Just replace dat Y and G with the relevant objects and components of the
data frame holding your data.

HTH

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list