整洁或最不整洁的S4 bBayesFactor对象
我想运行几个嵌套的整洁或最不整洁的S4 bBayesFactor对象,r,bayesian,tidy,broom,R,Bayesian,Tidy,Broom,我想运行几个嵌套的t.test和ttestBF(使用tidyr::nest()),但我无法整理或取消测试ttestBF函数中的S4:BFBayesFactor对象 示例数据: set.seed(354654) d = tibble(value = rnorm(100), category = sample(1:5, replace = TRUE, 100), group = sample(c('A', 'B'), replace = TRUE, 100
t.test
和ttestBF
(使用tidyr::nest()
),但我无法整理或取消测试ttestBF
函数中的S4:BFBayesFactor
对象
示例数据:
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()
我得到:错误:BFBayesFactor类的对象没有整洁的方法。如果我删除tidy()
调用,那么:
t_test_bf = map2(A, B, ~{ttestBF(.x$value, .y$value, nullInterval = c(0, Inf)) })
我仍然得到以下错误:
错误:所有嵌套列的元素数必须相同。
关于如何取消测试TTESBF
输出的任何想法?您可以使用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
d%>%
组别(类别、组别)%>%
嵌套()%>%
排列(键=组,值=数据)%>%
变异(
t_test_bf=map2(A,B,{ttestBF(.x$value,.y$value,
nullInterval=c(0,Inf))@bayesFactor}[,-3]),
A=地图(A,nrow),
B=地图(B,nrow)
) %>%
unnest()
#>#tibble:10 x 6
#>#群体:类别[5]
#>A类B bf错误代码
#>
#>111010-1.040.0000592159448F630F7
#>211010-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