使用 purr 包循环 Dunn 测试
Looping Dunn's test with purr package
我想 运行 使用 purrr
包进行 Dunn
测试。但是,提取 parameter/results 的 tidy
函数似乎不适用于此测试。有人对我有什么建议吗?
我的例子。
df <- data.frame(Type = c("n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4", "n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4"),
group = c("gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2"),
value = runif(16, min = 1, max = 30))
tbl <- df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest(data = -group) %>%
mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")),
tidied = map(fit, tidy))
Error: Problem with `mutate()` column `tidied`.
i `tidied = map(fit, tidy)`.
x No tidy method for objects of class dunnTest
Run `rlang::last_error()` to see where the error occurred.
把dunnTest
(假设是从FSA
)returns一个list
作为输出。我们可能需要提取 'res' 元素
library(dplyr)
library(tidyr)
library(FSA)
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest_by(group) %>%
mutate(fit = list(dunnTest(value~Type, data= data, method = "bh")$res)) %>%
ungroup %>%
unnest(c(fit))
-输出
# A tibble: 12 × 6
group data Comparison Z P.unadj P.adj
<chr> <list<tibble[,2]>> <chr> <dbl> <dbl> <dbl>
1 gr1 [8 × 2] n1 - n2 0.408 0.683 0.683
2 gr1 [8 × 2] n1 - n3 -0.612 0.540 0.648
3 gr1 [8 × 2] n2 - n3 -1.02 0.307 0.615
4 gr1 [8 × 2] n1 - n4 1.02 0.307 0.922
5 gr1 [8 × 2] n2 - n4 0.612 0.540 0.810
6 gr1 [8 × 2] n3 - n4 1.63 0.102 0.615
7 gr2 [8 × 2] n1 - n2 1.43 0.153 0.918
8 gr2 [8 × 2] n1 - n3 0.816 0.414 0.828
9 gr2 [8 × 2] n2 - n3 -0.612 0.540 0.810
10 gr2 [8 × 2] n1 - n4 1.02 0.307 0.922
11 gr2 [8 × 2] n2 - n4 -0.408 0.683 0.820
12 gr2 [8 × 2] n3 - n4 0.204 0.838 0.838
如果我们不需要 'data' 列,可以 select
或使用 transmute
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest_by(group) %>%
transmute(fit = list(dunnTest(value~Type, data= data,
method = "bh")$res)) %>%
ungroup %>%
unnest(c(fit))
-输出
# A tibble: 12 × 5
group Comparison Z P.unadj P.adj
<chr> <chr> <dbl> <dbl> <dbl>
1 gr1 n1 - n2 0.408 0.683 0.683
2 gr1 n1 - n3 -0.612 0.540 0.648
3 gr1 n2 - n3 -1.02 0.307 0.615
4 gr1 n1 - n4 1.02 0.307 0.922
5 gr1 n2 - n4 0.612 0.540 0.810
6 gr1 n3 - n4 1.63 0.102 0.615
7 gr2 n1 - n2 1.43 0.153 0.918
8 gr2 n1 - n3 0.816 0.414 0.828
9 gr2 n2 - n3 -0.612 0.540 0.810
10 gr2 n1 - n4 1.02 0.307 0.922
11 gr2 n2 - n4 -0.408 0.683 0.820
12 gr2 n3 - n4 0.204 0.838 0.838
如果我们提取 list
,输出已经是 tidy
ied data.frame
,即检查输出
的 str
> str(dunnTest(value ~ Type, data = subset(df, group == 'gr1')))
List of 3
$ method: chr "Holm"
$ res :'data.frame': 6 obs. of 4 variables:
..$ Comparison: chr [1:6] "n1 - n2" "n1 - n3" "n2 - n3" "n1 - n4" ...
..$ Z : num [1:6] 0.408 -0.612 -1.021 1.021 0.612 ...
..$ P.unadj : num [1:6] 0.683 0.54 0.307 0.307 0.54 ...
..$ P.adj : num [1:6] 0.683 1 1 1 1 ...
$ dtres : chr [1:22] " Kruskal-Wallis rank sum test" "" "data: x and g" "Kruskal-Wallis chi-squared = 2.8333, df = 3, p-value = 0.42" ...
- attr(*, "class")= chr "dunnTest"
如果我们使用具有 rowwise
属性的 nest_by
,则无需在此处使用 purrr
。以防万一,我们只是 nest
然后
library(purrr)
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest(data = -group) %>%
mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")$res)) %>%
unnest(fit)
-输出
# A tibble: 12 × 6
group data Comparison Z P.unadj P.adj
<chr> <list> <chr> <dbl> <dbl> <dbl>
1 gr1 <tibble [8 × 2]> n1 - n2 0.408 0.683 0.683
2 gr1 <tibble [8 × 2]> n1 - n3 -0.612 0.540 0.648
3 gr1 <tibble [8 × 2]> n2 - n3 -1.02 0.307 0.615
4 gr1 <tibble [8 × 2]> n1 - n4 1.02 0.307 0.922
5 gr1 <tibble [8 × 2]> n2 - n4 0.612 0.540 0.810
6 gr1 <tibble [8 × 2]> n3 - n4 1.63 0.102 0.615
7 gr2 <tibble [8 × 2]> n1 - n2 1.43 0.153 0.918
8 gr2 <tibble [8 × 2]> n1 - n3 0.816 0.414 0.828
9 gr2 <tibble [8 × 2]> n2 - n3 -0.612 0.540 0.810
10 gr2 <tibble [8 × 2]> n1 - n4 1.02 0.307 0.922
11 gr2 <tibble [8 × 2]> n2 - n4 -0.408 0.683 0.820
12 gr2 <tibble [8 × 2]> n3 - n4 0.204 0.838 0.838
我想 运行 使用 purrr
包进行 Dunn
测试。但是,提取 parameter/results 的 tidy
函数似乎不适用于此测试。有人对我有什么建议吗?
我的例子。
df <- data.frame(Type = c("n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4", "n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4"),
group = c("gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2"),
value = runif(16, min = 1, max = 30))
tbl <- df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest(data = -group) %>%
mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")),
tidied = map(fit, tidy))
Error: Problem with `mutate()` column `tidied`.
i `tidied = map(fit, tidy)`.
x No tidy method for objects of class dunnTest
Run `rlang::last_error()` to see where the error occurred.
把dunnTest
(假设是从FSA
)returns一个list
作为输出。我们可能需要提取 'res' 元素
library(dplyr)
library(tidyr)
library(FSA)
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest_by(group) %>%
mutate(fit = list(dunnTest(value~Type, data= data, method = "bh")$res)) %>%
ungroup %>%
unnest(c(fit))
-输出
# A tibble: 12 × 6
group data Comparison Z P.unadj P.adj
<chr> <list<tibble[,2]>> <chr> <dbl> <dbl> <dbl>
1 gr1 [8 × 2] n1 - n2 0.408 0.683 0.683
2 gr1 [8 × 2] n1 - n3 -0.612 0.540 0.648
3 gr1 [8 × 2] n2 - n3 -1.02 0.307 0.615
4 gr1 [8 × 2] n1 - n4 1.02 0.307 0.922
5 gr1 [8 × 2] n2 - n4 0.612 0.540 0.810
6 gr1 [8 × 2] n3 - n4 1.63 0.102 0.615
7 gr2 [8 × 2] n1 - n2 1.43 0.153 0.918
8 gr2 [8 × 2] n1 - n3 0.816 0.414 0.828
9 gr2 [8 × 2] n2 - n3 -0.612 0.540 0.810
10 gr2 [8 × 2] n1 - n4 1.02 0.307 0.922
11 gr2 [8 × 2] n2 - n4 -0.408 0.683 0.820
12 gr2 [8 × 2] n3 - n4 0.204 0.838 0.838
如果我们不需要 'data' 列,可以 select
或使用 transmute
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest_by(group) %>%
transmute(fit = list(dunnTest(value~Type, data= data,
method = "bh")$res)) %>%
ungroup %>%
unnest(c(fit))
-输出
# A tibble: 12 × 5
group Comparison Z P.unadj P.adj
<chr> <chr> <dbl> <dbl> <dbl>
1 gr1 n1 - n2 0.408 0.683 0.683
2 gr1 n1 - n3 -0.612 0.540 0.648
3 gr1 n2 - n3 -1.02 0.307 0.615
4 gr1 n1 - n4 1.02 0.307 0.922
5 gr1 n2 - n4 0.612 0.540 0.810
6 gr1 n3 - n4 1.63 0.102 0.615
7 gr2 n1 - n2 1.43 0.153 0.918
8 gr2 n1 - n3 0.816 0.414 0.828
9 gr2 n2 - n3 -0.612 0.540 0.810
10 gr2 n1 - n4 1.02 0.307 0.922
11 gr2 n2 - n4 -0.408 0.683 0.820
12 gr2 n3 - n4 0.204 0.838 0.838
如果我们提取 list
,输出已经是 tidy
ied data.frame
,即检查输出
str
> str(dunnTest(value ~ Type, data = subset(df, group == 'gr1')))
List of 3
$ method: chr "Holm"
$ res :'data.frame': 6 obs. of 4 variables:
..$ Comparison: chr [1:6] "n1 - n2" "n1 - n3" "n2 - n3" "n1 - n4" ...
..$ Z : num [1:6] 0.408 -0.612 -1.021 1.021 0.612 ...
..$ P.unadj : num [1:6] 0.683 0.54 0.307 0.307 0.54 ...
..$ P.adj : num [1:6] 0.683 1 1 1 1 ...
$ dtres : chr [1:22] " Kruskal-Wallis rank sum test" "" "data: x and g" "Kruskal-Wallis chi-squared = 2.8333, df = 3, p-value = 0.42" ...
- attr(*, "class")= chr "dunnTest"
如果我们使用具有 rowwise
属性的 nest_by
,则无需在此处使用 purrr
。以防万一,我们只是 nest
然后
library(purrr)
df %>%
mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
nest(data = -group) %>%
mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")$res)) %>%
unnest(fit)
-输出
# A tibble: 12 × 6
group data Comparison Z P.unadj P.adj
<chr> <list> <chr> <dbl> <dbl> <dbl>
1 gr1 <tibble [8 × 2]> n1 - n2 0.408 0.683 0.683
2 gr1 <tibble [8 × 2]> n1 - n3 -0.612 0.540 0.648
3 gr1 <tibble [8 × 2]> n2 - n3 -1.02 0.307 0.615
4 gr1 <tibble [8 × 2]> n1 - n4 1.02 0.307 0.922
5 gr1 <tibble [8 × 2]> n2 - n4 0.612 0.540 0.810
6 gr1 <tibble [8 × 2]> n3 - n4 1.63 0.102 0.615
7 gr2 <tibble [8 × 2]> n1 - n2 1.43 0.153 0.918
8 gr2 <tibble [8 × 2]> n1 - n3 0.816 0.414 0.828
9 gr2 <tibble [8 × 2]> n2 - n3 -0.612 0.540 0.810
10 gr2 <tibble [8 × 2]> n1 - n4 1.02 0.307 0.922
11 gr2 <tibble [8 × 2]> n2 - n4 -0.408 0.683 0.820
12 gr2 <tibble [8 × 2]> n3 - n4 0.204 0.838 0.838