# How to use Levene test function in R?

I'm a newbie to statistics and R and I have a trouble with using Levene function (I would like to check the equality of variance of two samples). The documentation says that I should run:

levene.test(y, group)

But I have no idea what I should put as y and group? I have two different samples which of I would like to check the equality of variance. Should I put one of the sample's values as y and the second as group parameter?

Any hints?

-

Let's say that, in R, your 1st sample is stored in a vector named sample1 and your 2nd sample is stored in a vector named sample2.

You first have to combine your two samples in a single vector and to create another vector defining the two groups:

y <- c(sample1, sample2)


and

group <- as.factor(c(rep(1, length(sample1)), rep(2, length(sample2))))


Now, you can call

library(Rcmdr)
levene.test(y, group)


EDIT

When trying this in R, I got the following warning:

Message d'avis :
'levene.test' is deprecated.
See help("Deprecated") and help("car-deprecated").


According to this, you should have a look at leveneTest instead...

-
Thanks! But would you be so kind and explain why it should go this way? I would love to understand it so that next time I don't have to ask questions and could help the others. – Jakub Sep 18 '11 at 15:23
@Jakub: Well, it goes this way because it was implemented using that structure. The help states that the first argument has to be the response variable whereas the second argument has to be the group variable. – ocram Sep 18 '11 at 15:29
In many cases R seems to prefer this type of data format, often referred to as "long". The reshape package provides functions called melt and cast that can be used to reshape your data, but they are more complex than what you need for a simple two variable case. – rpierce Sep 18 '11 at 16:42
Just to confirm, this wouldn't test the frequency spectra of sample 1 and sample 2, correct? So, for instance, say sample 1 is : 1,0,2,1,0 and sample 2 is: 1,1,3,0,0. It wouldn't bin the 1s and 0s of sample 1 to create the distribution of sample 1, correct? I hope that my follow-up question makes sense? – Atticus29 Dec 2 '13 at 17:45

Ocram's answer has all of the important pieces. However, you don't need to load all of Rcmdr if you don't want to. The relevant library is "car". But as ocram indicates, levene.test is deprecated. Note that the deprecation is not a change of functionality or code (at this point, 09/18/2011). It simply is a change in the function name. So levene.test and leveneTest will work the same. For the record I thought I'd provide an example using leveneTest and reusable reshaping code for this simple case:

#Creating example code
sample1 <- rnorm(20)
sample2 <- rnorm(20)

#General code to reshape two vectors into a long data.frame
twoVarWideToLong <- function(sample1,sample2) {
res <- data.frame(
GroupID=as.factor(c(rep(1, length(sample1)), rep(2, length(sample2)))),
DV=c(sample1, sample2)
)
}

#Reshaping the example data
long.data <- twoVarWideToLong(sample1,sample2)

#There are many different calls here that will work... but here is an example
leveneTest(DV~GroupID,long.data)

-
Very useful, thanks – Mac Feb 12 at 16:08

The easiest way (in my opinion) to prepare the data is using reshape2 package:

#Load packages
library(reshape2)
library(car)

#Creating example data
sample1 <- rnorm(20)
sample2 <- rnorm(20)

#Combine data
sample <- as.data.frame(cbind(sample1, sample2))

#Melt data
dataset <- melt(sample)

#Compute test
leveneTest(value ~ variable, dataset)

-