1. F-test in R

str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...

Shapiro Test

Shapiro-Wilk normality test

shapiro.test(ToothGrowth$len)
## 
##  Shapiro-Wilk normality test
## 
## data:  ToothGrowth$len
## W = 0.96743, p-value = 0.1091

The p value is greater than 0.05, we can assume the normality.

res.ftest <- var.test(len ~ supp, data = ToothGrowth)
res.ftest
## 
##  F test to compare two variances
## 
## data:  len by supp
## F = 0.6386, num df = 29, denom df = 29, p-value = 0.2331
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.3039488 1.3416857
## sample estimates:
## ratio of variances 
##          0.6385951

Conclusion

The p-value of 0.2331 is greater than the significance level of 0.05. We can conclude that there is no significant difference between the two variances.

Compare more than two sample variances in R

2. Bartlett’s test in R

str(PlantGrowth)
## 'data.frame':    30 obs. of  2 variables:
##  $ weight: num  4.17 5.58 5.18 6.11 4.5 4.61 5.17 4.53 5.33 5.14 ...
##  $ group : Factor w/ 3 levels "ctrl","trt1",..: 1 1 1 1 1 1 1 1 1 1 ...

Shapiro Test

shapiro.test(PlantGrowth$weight)
## 
##  Shapiro-Wilk normality test
## 
## data:  PlantGrowth$weight
## W = 0.98268, p-value = 0.8915

We can assume that data is normally distributed.

Bartlett test of homogeneity of variances

res <- bartlett.test(weight ~ group, data = PlantGrowth)
res
## 
##  Bartlett test of homogeneity of variances
## 
## data:  weight by group
## Bartlett's K-squared = 2.8786, df = 2, p-value = 0.2371

Conclusion

The p-value is 0.2371 is greater than the significance level of 0.05. We can conclude that there is no significant difference between the tested sample variances.

3. Levene’s test in R

library(car)
## Loading required package: carData
leveneTest(weight ~ group, data = PlantGrowth)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  2  1.1192 0.3412
##       27

Conclusion

The p-value is 0.3412 is greater than the significance level of 0.05. We can conclude that there is no significant difference between the tested sample variances.

Levene’s test with multiple independent variables can check based on ToothGrowth dataset,

ToothGrowth dataset dose column stored as numeric variable let’s convert into factor variable first,

ToothGrowth$dose <- as.factor(ToothGrowth$dose)
leveneTest(len ~ supp*dose, data = ToothGrowth)
## Levene's Test for Homogeneity of Variance (center = median)
##       Df F value Pr(>F)
## group  5  1.7086 0.1484
##       54

4. Fligner-Killeen test in R

fligner.test(weight ~ group, data = PlantGrowth)
## 
##  Fligner-Killeen test of homogeneity of variances
## 
## data:  weight by group
## Fligner-Killeen:med chi-squared = 2.3499, df = 2, p-value = 0.3088

Conclusion

The p-value is 0.3088 is greater than the significance level of 0.05. We can conclude that there is no significant difference was observed between the tested sample variances.