I want to run several nested t.test
and ttestBF
(using tidyr::nest()
) but I can't manage to either tidy or to unnest the S4: BFBayesFactor
object that comes out the ttestBF
function.
Example data:
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)
I run this piece of code for the t.test
and it works just fine:
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()
However, if I try this:
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()
I get: Error: No tidy method for objects of class BFBayesFactor
. If I remove the tidy()
call, so:
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) })
I still get the following error:
Error: All nested columns must have the same number of elements.
Any idea on how to unnest the ttestBF
output?