# Levene's Test in two different samples [closed]

How can I perform in R, a Levene's Test of two different samples to check homoscedasticity without being none of them a factor and not building new variables (what I really want to do is to compare variances of a variable in just a few levels of the factor, not all of them).

• hey it should be do-able, but can you have some example dataset, and also what you have tried so far Commented Apr 14, 2020 at 12:50
• Is this a statistics question or an R question. Are you in need of the syntax to apply Levene tests to only a subset of your data or do you have statistical questions about applying Levene test to only part of your data? Commented Apr 14, 2020 at 12:57
• just an R question @Bernhard Commented Apr 15, 2020 at 9:44
• Just R questions are not on topic on CrossValidated. Sometimes software questions are closed very fast here, so don't be surprised if that happens to your next R question. Better put statistics questions here and software questions on StackOverflow. Commented Apr 15, 2020 at 11:21

Let x1 and x2 be the two different samples.

x1<- rnorm(100, 50, 16)
x2<- rnorm(100, 60, 18)

leveneTest(x~gr, data=rbind(data.frame(x=x1, gr='gr1'),
data.frame(x=x2, gr='gr2')),
center='mean')


Result

Levene's Test for Homogeneity of Variance (center = "mean")
Df F value  Pr(>F)
group   1  7.0547 0.00855 **
198
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1


Creating a function

If you don't want to make any changes in the variable names in the code every time, you can create a new function as below:

library(car)

levtest<- function(x, y) {
leveneTest(dv~gr, data=rbind(data.frame(dv=x, gr='gr1'),
data.frame(dv=y, gr='gr2')), center='mean')
}

levtest(x1, x2)


Result

Levene's Test for Homogeneity of Variance (center = "mean")
Df F value  Pr(>F)
group   1  7.0547 0.00855 **
198
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

• Thank you very much! I was looking for some alternative to leveneTest that avoid defining new variables as factors but your answes solves my problem perfectly. Commented Apr 15, 2020 at 9:49
• @heyou You're welcome. Commented Apr 15, 2020 at 9:56