Let us load the libraries
library(purrr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(broom)
library(knitr)
library(tibble)
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
Let us use inbuilt iris data for our work.
iris %>%
pivot_longer(-Species, names_to="measurement", values_to="value") %>%
nest(data=c(Species,value)) %>%
mutate(model=map(data, ~TukeyHSD(aov (value ~ Species, data=.))),
tidy=map(model,tidy)) %>%
unnest(c("tidy")) %>%
select(c(5,10)) %>%
add_column(Measurement="",.before="contrast") %>%
mutate(adj.p.value=-log10(adj.p.value)) %>%
kable(., "html", booktabs = T, escape = F, caption = "Flower measurement statistics",
col.names = c("","Comparison", "Ajdusted P value, (-log10p)")) %>%
kable_styling("striped",full_width = F) %>%
column_spec(2:3, width = "5cm") %>%
pack_rows("Sepal Length", 1, 3) %>%
pack_rows("Sepal Width", 4, 6) %>%
pack_rows("Petal Length", 7, 9) %>%
pack_rows("Petal Width", 10,12)
Comparison | Ajdusted P value, (-log10p) | |
---|---|---|
Sepal Length | ||
versicolor-setosa | 13.470290 | |
virginica-setosa | 14.523226 | |
virginica-versicolor | 8.081573 | |
Sepal Width | ||
versicolor-setosa | 13.508986 | |
virginica-setosa | 8.866226 | |
virginica-versicolor | 2.056495 | |
Petal Length | ||
versicolor-setosa | 14.523226 | |
virginica-setosa | 14.523226 | |
virginica-versicolor | 14.523226 | |
Petal Width | ||
versicolor-setosa | 14.523226 | |
virginica-setosa | 14.523226 | |
virginica-versicolor | 14.523226 |
How are we sure that our results are correct. Let us run anova and TukeyHSD on one variable (Sepal Width)
sl_anova <- aov(Sepal.Width ~ Species, data=iris)
-log10(TukeyHSD(sl_anova)$Species[,4])
## versicolor-setosa virginica-setosa virginica-versicolor
## 13.508986 8.866226 2.056495