Hi again. I'm trying to use the map function to use the dunn.test
package to perform some post-hoc analyses. The below code sort of works in that I can a) split the data by one variable and b) apply a dunn test of some values against another variable. Unfortuantely the returned results are identical for each of the split groupings.
library(dunn.test)
data <- data.frame(
groupA = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "A",
"A", "A", "A", "A", "B", "B", "B", "B", "B", "B"),
groupB = c("L", "M", "N", "L", "M", "L", "M", "N", "L", "M", "N", "L",
"M", "N", "L", "M", "L", "M", "N", "L", "M", "N", "L",
"M", "N", "L", "M", "L", "M", "N", "L", "M", "N"),
value = c(10, 111.5, 360, 90, 120, 180, 50, 60, 70, 80, 90, 10, 100,
360, 90, 120, 180, 50, 60, 70, 80, 90, 10, 100, 360,
90, 120, 180, 50, 60, 70, 80, 90)
)
data %>%
split(.$groupA) %>%
map(~dunn.test(x = data$value, g = data$groupB, method = "bonferroni")) %>%
map_dfr(~ as.data.frame(.), id = "groupA")
Kruskal-Wallis rank sum test
data: x and group
Kruskal-Wallis chi-squared = 1.0649, df = 2, p-value = 0.59
Comparison of x by group
(Bonferroni)
Col Mean-|
Row Mean | L M
---------+----------------------
M | -0.477697
| 0.9493
|
N | -1.031944 -0.589682
| 0.4531 0.8331
alpha = 0.05
Reject Ho if p <= alpha/2
Kruskal-Wallis rank sum test
data: x and group
Kruskal-Wallis chi-squared = 1.0649, df = 2, p-value = 0.59
Comparison of x by group
(Bonferroni)
Col Mean-|
Row Mean | L M
---------+----------------------
M | -0.477697
| 0.9493
|
N | -1.031944 -0.589682
| 0.4531 0.8331
alpha = 0.05
Reject Ho if p <= alpha/2
At first, what I thought was happening is that dunn.test
is getting applied to the whole dataset rather than first splitting the data, then doing a dunn.test
within each group, but as you can see by the output, some of the values do not match up with the above (edit: I missed out the method = bonferroni
which when included outputs the same as above):
dunn.test(x = data$value, g = data$groupB)
Kruskal-Wallis rank sum test
data: x and group
Kruskal-Wallis chi-squared = 1.0649, df = 2, p-value = 0.59
Comparison of x by group
(No adjustment)
Col Mean-|
Row Mean | L M
---------+----------------------
M | -0.477697
| 0.3164
|
N | -1.031944 -0.589682
| 0.1510 0.2777
alpha = 0.05
Reject Ho if p <= alpha/2
Finally, I'm also trying to output the data into a data.frame - or anything really - which I can pass to KableExtra to neatly output a table (I'm using this in a Rmarkdown document). map_dfr
almost works, but it drops the variable by which I split the data.
EDIT: So I think the dunn.test is getting applied to the whole dataset, I missed out the method = "bonferroni"
in the second bit of code... When I include that, the output is the same as in the first example