15.7 Kruskal-Wallis Test
Michael Sullivan
2023-08-30
To perform a Kruskal-Wallis test, the data must be saved into two columns. One column represents the value of the response variable; the other column indicates the treatment.
We will follow Example 1 from Section 15.7. For convenience, we will let 1 represent the 20- to 29-year-olds; let 2 represent the 40- to 49-year-olds; let 3 represent the 60- to 69-year-olds. Enter the response variable as a vector using the c() command. Enter the treatment using the rep() function. Each treatment is repeated 12 times because there are 12 observations for each level of treatment.
data <- c(54, 43, 38, 30, 61, 53, 35, 34, 39, 46, 50, 35, 61, 41, 44, 47, 33, 29, 59, 35, 34, 74, 50, 65, 44, 65, 62, 53, 51, 49, 49, 42, 35, 44, 37, 38)
treatment
<- c(rep(1, 12), rep(2, 12), rep(3, 12))
Now, save the data as a data.frame.
Table15 <- data.frame(data, treatment)
head(Table15)
## data treatment
##
1 54 1
##
2 43 1
##
3 38 1
##
4 30 1
##
5 61 1
##
6 53 1
To run the Kruskal-Wallis Test, use the kruskal.test function.
kruskal.test(data ~ treatment,data=Table15)
##
##
Kruskal-Wallis rank sum test
##
##
data: data by treatment
##
Kruskal-Wallis chi-squared = 1.0121, df = 2, p-value = 0.6029
The P-value is 0.6029.