Does the mean petal width differ among the three species of iris flowers — setosa, versicolor, and virginica?
#install.packages("datasets")
library(datasets)
mydata <- iris
head(mydata)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
The unit of observation: an individual iris flower.
Sample Size: The dataset consists of 150 observations, each representing a different iris flower.
Units of Measurement: All measurements (Sepal Length, Sepal Width, Petal Length, Petal Width) are in centimeters.
Sepal Length: The length of the sepal, measured in centimeters.
Sepal Width: The width of the sepal, measured in centimeters.
Petal Length: The length of the petal, measured in centimeters.
Petal Width: The width of the petal, measured in centimeters.
Species: The species of iris flowers.
Iris dataset was introduced by Sir Ronald A. Fisher in 1936, and it is included in the base R installation.
mydata$SpeciesFactor <- factor(mydata$Species,
levels = c("setosa", "versicolor", "virginica"),
labels = c("Setosa", "Versicolor", "Virginica"))
H0: The mean petal width is the same across all three species of iris flowers — Setosa, Versicolor, and Virginica.
H1: At least one pair of the Setosa, Versicolor, or Virginica species of iris flowers has a difference in the mean petal width.
Analyzed variable is numeric.
Homogeneity of variances.
Variable in the population is normaly distributed within each group.
No significant outliers.
summary(iris[, 1:4])
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
library(psych)
##
## Attaching package: 'psych'
## The following object is masked from 'package:onewaytests':
##
## describe
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
## The following object is masked from 'package:car':
##
## logit
describe(mydata$Petal.Width)
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 150 1.2 0.76 1.3 1.18 1.04 0.1 2.5 2.4 -0.1 -1.36
## se
## X1 0.06
describeBy(x = mydata$Petal.Width, group = mydata$SpeciesFactor)
##
## Descriptive statistics by group
## group: Setosa
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 50 0.25 0.11 0.2 0.24 0 0.1 0.6 0.5 1.18 1.26
## se
## X1 0.01
## ----------------------------------------------------
## group: Versicolor
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 50 1.33 0.2 1.3 1.32 0.22 1 1.8 0.8 -0.03 -0.59
## se
## X1 0.03
## ----------------------------------------------------
## group: Virginica
## vars n mean sd median trimmed mad min max range skew kurtosis
## X1 1 50 2.03 0.27 2 2.03 0.3 1.4 2.5 1.1 -0.12 -0.75
## se
## X1 0.04
Mean 2.03 in the Virginica group: On average, the petal width of the Virginica group of iris flowers is 2.03 cm.
Sd 0.27 in the Virginica group: The standard deviation of 0.27 indicates the average amount by which individual petal widths in the Virginica group differ from the mean.
Median 2 in the Virginica group: Half of the petal widths in the Virginica group are less than or equal to 2 cm, while the other half are greater than this value.
Range 1.1 in the Virginica group: The range of 1.1 cm represents the difference between the widest and narrowest petal widths observed in the Virginica group.
Se 0.04 in the Virginica group: The standard error of 0.04 indicates how much estimation error there is the true average petal width of all Virginica iris flowers from the average petal width of the sampled flowers.
library(car)
leveneTest(mydata$Petal.Width, group = mydata$SpeciesFactor)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 2 19.892 2.261e-08 ***
## 147
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
H0: The variances of petal widths are equal across all three species of iris flowers—Setosa, Versicolor, and Virginica.
H1: At least one variance of petal widths in one of the species is different.
We reject H0 at p-value < 0.001. The assumption of homogeneity of variances is violated.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(rstatix)
##
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
##
## filter
mydata %>%
group_by(SpeciesFactor) %>%
shapiro_test(Petal.Width)
## # A tibble: 3 × 4
## SpeciesFactor variable statistic p
## <fct> <chr> <dbl> <dbl>
## 1 Setosa Petal.Width 0.800 0.000000866
## 2 Versicolor Petal.Width 0.948 0.0273
## 3 Virginica Petal.Width 0.960 0.0870
H0: The Petal Width variable is normally distributed within the Setosa species.
H1: The Petal Width variable is not normally distributed within the Setosa species.
p-value < 0.001, we reject H0.
H0: The Petal Width variable is normally distributed within the Versicolor species.
H1: The Petal Width variable is not normally distributed within the Versicolor species.
p-value:0.028, we reject H0.
H0: The Petal Width variable is normally distributed within the Virginica species.
H1: The Petal Width variable is not normally distributed within the Virginica species.
p-value: 0.087, can’t reject H0.
The assumption of normality is violated for Setosa and Versicolor species, but not for Virginica based on the Shapiro-Wilk Test.
#install.packages("onewaytests")
library(onewaytests)
welch.test(Petal.Width ~ SpeciesFactor,
data = mydata)
##
## Welch's Heteroscedastic F Test (alpha = 0.05)
## -------------------------------------------------------------
## data : Petal.Width and SpeciesFactor
##
## statistic : 1276.885
## num df : 2
## denom df : 84.95125
## p.value : 4.138739e-64
##
## Result : Difference is statistically significant.
## -------------------------------------------------------------
H0:The mean petal width is the same among the three species of iris flowers (Setosa, Versicolor, and Virginica).
H1:At least one pair of the Setosa, Versicolor, or Virginica species of iris flowers has a difference in the mean petal width.
The Welch’s test results indicate a difference in the mean petal width among the three species of iris flowers (p-value = 4.138739e-64). Therefore, we reject the null hypothesis. The assumption of homogeneity of variances for one-way ANOVA is violated.
However, in this case we do not need Welch ANOVA because normality is violated. We need Kruskal-Wallis test.
kruskal.test(Petal.Width ~ SpeciesFactor,
data = mydata)
##
## Kruskal-Wallis rank sum test
##
## data: Petal.Width by SpeciesFactor
## Kruskal-Wallis chi-squared = 131.19, df = 2, p-value < 2.2e-16
H0: Distribution of locations of petal width is the same for all three species of iris flowers.
H1: At least one distribution of locations of petal width is different among the three species of iris flowers.
Based on the Kruskal-Wallis test, there is a difference in the distribution of petal width among the three species of iris flowers (Setosa, Versicolor, and Virginica).
kruskal_effsize(Petal.Width ~ SpeciesFactor,
data = mydata)
## # A tibble: 1 × 5
## .y. n effsize method magnitude
## * <chr> <int> <dbl> <chr> <ord>
## 1 Petal.Width 150 0.879 eta2[H] large
The Kruskal effect size of 0.88 suggests that a large proportion of the variability in Petal Width is associated with the differences between the three species (Setosa, Versicolor, and Virginica).
library(rstatix)
groups_nonpar <- wilcox_test(Petal.Width ~ SpeciesFactor,
paired = FALSE,
p.adjust.method = "bonferroni",
data = mydata)
groups_nonpar
## # A tibble: 3 × 9
## .y. group1 group2 n1 n2 statistic p p.adj
## * <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl>
## 1 Petal.Width Setosa Versi… 50 50 0 2.28e-18 6.84e-18
## 2 Petal.Width Setosa Virgi… 50 50 0 2.43e-18 7.29e-18
## 3 Petal.Width Versicol… Virgi… 50 50 49 9.70e-17 2.91e-16
## # ℹ 1 more variable: p.adj.signif <chr>
Setosa vs. Versicolor:
Test Statistic: 0
p-value: 2.28e-18 (extremely small)
The p-value is highly significant (< 0.001), indicating strong evidence to reject the null hypothesis. There is a significant difference in the distribution of Petal Width between Setosa and Versicolor species.
Setosa vs. Virginica:
Test Statistic: 0
p-value: 2.43e-18 (extremely small)
The p-value is highly significant, suggesting a significant difference in the distribution of Petal Width between Setosa and Virginica species.
Versicolor vs. Virginica:
Test Statistic: 49
p-value: 9.70e-17 (extremely small)
The p-value is highly significant, indicating a significant difference in the distribution of Petal Width between Versicolor and Virginica species.
In conclusion, for all three comparisons, the p-values are very small ( < 0.001), providing strong evidence to reject the null hypothesis. This suggests that there are significant differences in the distribution of Petal Width between each pair of species.
pwc <- mydata %>%
wilcox_test(Petal.Width ~ SpeciesFactor,
paired = FALSE,
p.adjust.method = "bonferroni")
Kruskal_results <- kruskal_test(Petal.Width ~ SpeciesFactor,
data = mydata)
library(rstatix)
pwc <- pwc %>%
add_y_position(fun = "median", step.increase = 0.35)
#install.packages("ggpubr")
library(ggpubr)
ggboxplot(mydata, x = "SpeciesFactor", y = "Petal.Width", add = "point", ylim=c(0, 6)) +
stat_pvalue_manual(pwc, hide.ns = FALSE) +
stat_summary(fun = median, geom = "point", shape = 16, size = 4,
aes(group = SpeciesFactor), color = "darkred",
position = position_dodge(width = 0.8)) +
stat_summary(fun = median, colour = "darkred",
position = position_dodge(width = 0.8),
geom = "text", vjust = -0.5, hjust = -1,
aes(label = round(after_stat(y), digits = 2), group = SpeciesFactor)) +
labs(subtitle = get_test_label(Kruskal_results, detailed = TRUE),
caption = get_pwc_label(pwc))