整理或取消嵌套 S4 BFBayesFactor 对象
Tidy or unnest S4 BFBayesFactor object
我想 运行 几个嵌套的 t.test
和 ttestBF
(使用 tidyr::nest()
)但我无法整理或取消嵌套 S4: BFBayesFactor
对象从 ttestBF
函数出来。
示例数据:
set.seed(354654)
d = tibble(value = rnorm(100),
category = sample(1:5, replace = TRUE, 100),
group = sample(c('A', 'B'), replace = TRUE, 100)) %>%
arrange(category)
我 运行 这段代码用于 t.test
并且工作正常:
library('tidyverse')
library('broom')
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test = map2(A, B, ~{t.test(.x$value, .y$value) %>% tidy()}),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
但是,如果我尝试这样做:
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) %>% tidy() }),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
我得到:Error: No tidy method for objects of class BFBayesFactor
。如果我删除 tidy()
调用,那么:
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) })
我仍然收到以下错误:
Error: All nested columns must have the same number of elements.
知道如何取消嵌套 ttestBF
输出吗?
您可以使用 S4 对象的 @
符号将数据帧从每个对象的 bayesFactor
S4 插槽中拉出:
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value,
nullInterval = c(0, Inf))@bayesFactor}[,-3]),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
#> # A tibble: 10 x 6
#> # Groups: category [5]
#> category A B bf error code
#> <int> <int> <int> <dbl> <dbl> <fct>
#> 1 1 10 10 -1.04 0.0000592 159448f630f7
#> 2 1 10 10 -0.797 0.0000000429 1594124c5471
#> 3 2 7 6 -0.519 0.000000105 15946a5667c9
#> 4 2 7 6 -1.01 0.000141 15946b70910
#> 5 3 8 9 -1.32 0.00000260 15944c833396
#> 6 3 8 9 -0.214 0.000000168 159433103012
#> 7 4 15 11 -0.709 0.0000450 15942a13701
#> 8 4 15 11 -1.26 0.000123 15947c9d5ed8
#> 9 5 11 13 -1.11 0.000122 15945bcc7d07
#> 10 5 11 13 -0.850 0.00000969 1594311d17e2
我想 运行 几个嵌套的 t.test
和 ttestBF
(使用 tidyr::nest()
)但我无法整理或取消嵌套 S4: BFBayesFactor
对象从 ttestBF
函数出来。
示例数据:
set.seed(354654)
d = tibble(value = rnorm(100),
category = sample(1:5, replace = TRUE, 100),
group = sample(c('A', 'B'), replace = TRUE, 100)) %>%
arrange(category)
我 运行 这段代码用于 t.test
并且工作正常:
library('tidyverse')
library('broom')
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test = map2(A, B, ~{t.test(.x$value, .y$value) %>% tidy()}),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
但是,如果我尝试这样做:
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) %>% tidy() }),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
我得到:Error: No tidy method for objects of class BFBayesFactor
。如果我删除 tidy()
调用,那么:
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) })
我仍然收到以下错误:
Error: All nested columns must have the same number of elements.
知道如何取消嵌套 ttestBF
输出吗?
您可以使用 S4 对象的 @
符号将数据帧从每个对象的 bayesFactor
S4 插槽中拉出:
d %>%
group_by(category, group) %>%
nest() %>%
spread(key = group, value = data) %>%
mutate(
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value,
nullInterval = c(0, Inf))@bayesFactor}[,-3]),
A = map(A, nrow),
B = map(B, nrow)
) %>%
unnest()
#> # A tibble: 10 x 6
#> # Groups: category [5]
#> category A B bf error code
#> <int> <int> <int> <dbl> <dbl> <fct>
#> 1 1 10 10 -1.04 0.0000592 159448f630f7
#> 2 1 10 10 -0.797 0.0000000429 1594124c5471
#> 3 2 7 6 -0.519 0.000000105 15946a5667c9
#> 4 2 7 6 -1.01 0.000141 15946b70910
#> 5 3 8 9 -1.32 0.00000260 15944c833396
#> 6 3 8 9 -0.214 0.000000168 159433103012
#> 7 4 15 11 -0.709 0.0000450 15942a13701
#> 8 4 15 11 -1.26 0.000123 15947c9d5ed8
#> 9 5 11 13 -1.11 0.000122 15945bcc7d07
#> 10 5 11 13 -0.850 0.00000969 1594311d17e2