# Store the data in the variable my_data
my_data <- ToothGrowth
head(my_data , 10)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
## 7 11.2 VC 0.5
## 8 11.2 VC 0.5
## 9 5.2 VC 0.5
## 10 7.0 VC 0.5
# F-test
res.ftest <- var.test(len ~ supp, data = my_data)
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
# The p-value of F-test is p = 0.2331433 which is greater than the significance level 0.05. In conclusion, there is no significant difference between the two variances.
# ratio of variances
res.ftest$estimate
## ratio of variances
## 0.6385951
# p-value of the test
res.ftest$p.value
## [1] 0.2331433
# Load the data
data(ToothGrowth)
data(PlantGrowth)
set.seed(123)
# Show PlantGrowth
dplyr::sample_n(PlantGrowth, 10)
## weight group
## 1 5.87 trt1
## 2 4.32 trt1
## 3 3.59 trt1
## 4 5.18 ctrl
## 5 5.14 ctrl
## 6 4.89 trt1
## 7 5.12 trt2
## 8 4.81 trt1
## 9 4.50 ctrl
## 10 4.69 trt1
# PlantGrowth data structure
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 ...
# Show ToothGrowth
dplyr::sample_n(ToothGrowth, 10)
## len supp dose
## 1 17.3 VC 1.0
## 2 24.5 OJ 2.0
## 3 26.4 VC 2.0
## 4 32.5 VC 2.0
## 5 26.7 VC 2.0
## 6 6.4 VC 0.5
## 7 25.5 OJ 2.0
## 8 30.9 OJ 2.0
## 9 21.5 VC 2.0
## 10 5.2 VC 0.5
# ToothGrowth data structure
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 ...
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
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
bartlett.test(len ~ interaction(supp,dose), data=ToothGrowth)
##
## Bartlett test of homogeneity of variances
##
## data: len by interaction(supp, dose)
## Bartlett's K-squared = 6.9273, df = 5, p-value = 0.2261
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
# Levene's test with one independent variable
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
# Levene's test with multiple independent variables
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
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