-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
195 lines (164 loc) · 4.5 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# tukeygrps
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![CRAN status](https://www.r-pkg.org/badges/version/tukeygrps)](https://cran.r-project.org/package=tukeygrps)
<!-- badges: end -->
Tukeygrps provides simple wrapper functions for the annotation of (gg)plots according to statistical differences between groups determined by a parametric Tukey-HSD test from {stats} or a non-parametric Kruskal-Wallis test with Dunn's test for multiple comparisons from {dunn.test}.
## Installation
You can install tukeygrps from github using remotes:
``` {r, eval=FALSE}
install.packages("remotes")
library("remotes")
install_github("leonardblaschek/tukeygrps")
```
## Examples
*Parametric* multiple comparisons like the Tukey HSD (honest significant differences) test shown in section **1** are only recommended in cases where the data fulfill all of the following conditions:
* **normally** distributed
* **homoscedastic**
* **independent** within and between groups
* **equal** in sample size
If you have strong evidence that they do not fulfill these conditions, consider a *non-parametric* method of comparison, like the Kruskal-Wallis test followed by Dunn's multiple comparisons shown in section **2**.
### 1. Parametric multiple comparisons
Here we use letter_groups() with stat_method = "tukey" to add letters to a geom_point plot. Alpha is set to 0.001, the letters are printed at y = 0, and there are no additional grouping variables.
```{r example no groups, message=FALSE}
library(tukeygrps)
library(tidyverse)
data(mpg)
head(mpg)
tukey_letters <- letter_groups(mpg, hwy, class, "tukey", print_position = 0, stat_alpha = 0.001)
head(tukey_letters)
ggplot() +
geom_jitter(
data = mpg,
aes(
x = class,
y = hwy
),
width = 0.1
) +
geom_text(
data = tukey_letters,
aes(
x = class,
y = hwy,
label = Letters
)
) +
coord_flip()
```
Here we split the statistical analysis by two grouping variables ("cut" and "color"), set the alpha to 0.05 and print the letters 0.5 standard deviations below the respective minimum value.
```{r example, warning=FALSE, message=FALSE}
library(tukeygrps)
library(tidyverse)
data(diamonds)
diamonds <- diamonds %>%
filter(cut %in% c("Ideal", "Premium", "Very Good") & color %in% c("D", "E", "F"))
head(diamonds)
tukey_letters <- letter_groups(
diamonds,
price,
clarity,
"tukey",
cut,
color,
print_position = "below",
print_adjust = 0.5,
stat_alpha = 0.05,
)
head(tukey_letters)
ggplot() +
geom_jitter(
data = diamonds,
aes(
x = clarity,
y = price
),
size = 1,
width = 0.1,
alpha = 0.25
) +
geom_boxplot(
data = diamonds,
aes(
x = clarity,
y = price
),
outlier.alpha = 0,
fill = rgb(1, 1, 1, 0.5)
) +
geom_text(
data = tukey_letters,
aes(
x = clarity,
y = price,
label = Letters
),
size = 3
) +
facet_grid(cut ~ color) +
coord_flip()
```
### 2. Non-parametric multiple comparisons
In case the above requirements for parametric tests are not met, we can fall back to the non-parametric Kruskal–Wallis test followed by Dunn's test and *p*-value adjustment for multiple comparisons. Here we place the letter codes 0.5 standard deviations above the maximum values.
```{r example non-parametric, warning=FALSE, message=FALSE, results='hide'}
library(tukeygrps)
library(tidyverse)
data(diamonds)
diamonds <- diamonds %>%
filter(cut %in% c("Ideal", "Premium", "Very Good") & color %in% c("D", "E", "F"))
kruskal_letters <- letter_groups(
diamonds,
price,
clarity,
"kruskal",
cut,
color,
print_position = "above",
print_adjust = 0.5,
p_adj_method = "holm"
)
head(kruskal_letters)
ggplot() +
geom_jitter(
data = diamonds,
aes(
x = clarity,
y = price
),
size = 1,
width = 0.1,
alpha = 0.25
) +
geom_boxplot(
data = diamonds,
aes(
x = clarity,
y = price
),
outlier.alpha = 0,
fill = rgb(1, 1, 1, 0.5)
) +
geom_text(
data = kruskal_letters,
aes(
x = clarity,
y = price,
label = Letters
),
size = 3
) +
facet_grid(cut ~ color) +
coord_flip()
```