How to perform homogeneity of variance test for two-way anova in R?

R ProgrammingServer Side ProgrammingProgramming

In general, we can say that the homogeneity of variance test is the type of test that compares the variance of two or more variables and finds the significant difference between or among them if exists. For a two-way anova, one of the most commonly used homogeneity of variance test is Levene’s Test and it can be easily done with the help of leveneTest function of car package in base R.

Consider the below data frame −

Example

 Live Demo

set.seed(151)
x1<-sample(c("C1","C2","C3"),20,replace=TRUE)
x2<-sample(c("S1","S2","S3","S4","S5"),20,replace=TRUE)
y<-rnorm(20,5,2)
df1<-data.frame(x1,x2,y)
df1

Output

  x1 x2   y
1 C2 S2 2.255857
2 C3 S5 1.726474
3 C3 S4 4.280697
4 C2 S3 7.402230
5 C2 S3 3.708252
6 C2 S4 3.978782
7 C2 S1 3.801754
8 C3 S3 6.091206
9 C2 S3 4.017412
10 C3 S3 5.383071
11 C3 S1 3.882945
12 C1 S5 6.845399
13 C1 S1 7.307996
14 C3 S4 2.255179
15 C1 S5 7.580363
16 C2 S5 7.309804
17 C2 S4 7.891359
18 C2 S3 5.522026
19 C3 S4 8.858292
20 C1 S1 3.800228

Loading car package and performing Levene’s test on df1 −

library(car) leveneTest(y~x1*x2,data=df1) 
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 9 1.5987 0.2374 10

Let’s have a look at another example −

Example

 Live Demo

Age_group<-sample(c("First","Second"),20,replace=TRUE)
Ethnicity<-sample(c("Asian","NorthAmerican","Chinese","Japanese"),20,replace=TRUE)
Salary<-sample(20000:50000,20)
df2<-data.frame(Age_group,Ethnicity,Salary)
df2

Output

Age_group Ethnicity Salary
1 Second NorthAmerican 25678
2 Second Asian 34597
3 Second Chinese 49861
4 Second Chinese 37386
5 First Japanese 38426
6 Second NorthAmerican 45889
7 Second Asian 35033
8 Second NorthAmerican 46098
9 First Japanese 34070
10 Second Japanese 33618
11 First Japanese 35760
12 Second Chinese 33376
13 Second NorthAmerican 30630
14 First Asian 23820
15 Second Asian 40899
16 First Asian 35095
17 Second Chinese 43439
18 First Japanese 35641
19 Second Asian 41754
20 Second NorthAmerican 35337

Performing Levene’s test on df2 −

leveneTest(Salary~Age_group*Ethnicity,data=df2)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 6 0.6593 0.6835 13
raja
Published on 19-Oct-2020 13:52:19
Advertisements