Your name: Ahhyun Lee
The goal of this exercise is to use R to analyze data using the Kruskal-Wallis test.
Let the following simulated dataset reflects BMI among patients with type 2 diabetes. Patients were randomly assigned to one of four medications to reduce hunger, and each continued the medication for six months. BMI was measured at month 6. Assume BMI was the same across all groups at baseline.
Research Question: Does BMI at month 6 differ between the 4 groups after 6 months?
library(car)
## Loading required package: carData
#install.packages("dunn.test")
library(dunn.test)
# Setting up the data
set.seed(89489)
x1 <- rnorm(100, 33.0, 2.8)
x2 <- runif(150, 35, 45)
x3 <- rnorm(125, 38.0, 1.8)
x4 <- runif(110, 25, 38)
bmidata <- as.data.frame(rbind(cbind(x1, "Group 1"),
cbind(x2, "Group 2"),
cbind(x3, "Group 3"),
cbind(x4, "Group 4")))
bmidata[,1] <- as.numeric(bmidata[,1])
colnames(bmidata) <- c("BMI","Group")
Briefly describe key findings as related to the research question.
# Numerical summary
tapply(bmidata$BMI, bmidata$Group, length)
## Group 1 Group 2 Group 3 Group 4
## 100 150 125 110
tapply(bmidata$BMI, bmidata$Group, mean)
## Group 1 Group 2 Group 3 Group 4
## 32.80546 39.76490 37.93974 31.15361
tapply(bmidata$BMI, bmidata$Group, sd)
## Group 1 Group 2 Group 3 Group 4
## 2.503489 2.976291 1.669114 3.598900
tapply(bmidata$BMI, bmidata$Group, median)
## Group 1 Group 2 Group 3 Group 4
## 32.94170 39.48772 38.03233 31.08803
tapply(bmidata$BMI, bmidata$Group, IQR)
## Group 1 Group 2 Group 3 Group 4
## 3.344406 4.990172 2.473244 5.252027
Briefly describe key findings as related to the research question.
# Visualize
par(mfrow = c(1, 2))
boxplot(BMI ~ Group, data = bmidata, ylab = "BMI at month 6")
hist(bmidata$BMI, main = "All groups", xlab = "BMI")
par(mfrow = c(1, 1))
Summarize the findings and interpret as related to the null hypothesis.
kruskal.test(BMI ~ Group, data = bmidata)
##
## Kruskal-Wallis rank sum test
##
## data: BMI by Group
## Kruskal-Wallis chi-squared = 321.88, df = 3, p-value < 2.2e-16
Use Dunn’s test with the Bonferonni correction. Note: it is possible that you did not reject the overall test in the step above. In that case, please rerun the analysis with a different seed.
Summarize the findings.
# Post hoc: Dunn's test with Bonferroni (install.packages("dunn.test") once)
out <- capture.output(dunn.test(bmidata$BMI, bmidata$Group, method = "bonferroni"))
## Kruskal-Wallis rank sum test
##
## data: x and group
## Kruskal-Wallis chi-squared = 321.8806, df = 3, p-value = 0
##
##
## Dunn's Pairwise Comparison of x by group
## (Bonferroni)
##
## Col Mean-│
## Row Mean │ Group 1 Group 2 Group 3
## ─────────┼─────────────────────────────────
## Group 2 │ -12.91384
## │ 0.0000*
## │
## Group 3 │ -9.881550 2.819187
## │ 0.0000* 0.0144*
## │
## Group 4 │ 1.467005 14.89586 11.69141
## │ 0.4271 0.0000* 0.0000*
##
## FWER = 0.05
## Reject Ho if adjusted p ≤ FWER/2, where (unadjusted) p = Pr(Z ≥ |z|)
cat(gsub("\u2502", "|", out), sep = "\n")