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?

  • ocram

    ocram Correct answer

    9 years ago

    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)


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

    Now, you can call

    levene.test(y, group)


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

    'levene.test' has now been removed. Use 'leveneTest' instead...

    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: 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.

    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.

    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?

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM