aov 在 group_by 的上下文中使用时抛出 "could not find function 'Error'"

aov throws "could not find function 'Error'" when used in the context of group_by

我正在尝试 运行 重复测量 ANCOVA。以下代码工作正常:

tidy(aov(FA ~ sex * study + Error(PartID), data = DTI.TRACTlong))

其中 FA 是连续测量,性别和学习是学习指示的因素(时间 1 或时间 2),PartID 是个人 ID。但是,我必须 运行 针对两种不同条件(协调与非协调)的多个区域 (ROI) 进行此分析。使用带 group_by 的 tidyverse 似乎很容易(见下文),但它会在 Error(.$PartID) 中抛出错误:找不到函数“错误”。知道这里发生了什么吗?为什么 Error 函数在单独使用时被识别,而在将 tidyverse 与 group_by 一起使用时却不能识别?

  group_by(harmonize,ROI) %>%
  do(tidy(aov(.$FA ~ .$GOBS_Gender * .$study + Error(.$PartID))))

指定 data 并删除 .$

library(broom)
library(dplyr)
...
    group_by(harmonize,ROI) %>%
    do(tidy(aov(FA ~ sex * study + Error(PartID), data = .)))

另一个选项可能是使用 nest_by

library(tidyr)
...
  nest_by(harmonize, ROI) %>%
  mutate(out = list(tidy(aov(FA ~ sex * study + Error(PartID), data = data)))) %>%
  select(-data) %>%
  unnest(c(out))

使用可重现的例子

> data(npk)
> npk$grp <- rep(c('a', 'b'), each = 12)

-做方法

> npk %>% 
     group_by(grp) %>% 
     do(tidy(aov(yield ~  N*P*K + Error(block), data = .)))
# A tibble: 18 x 8
# Groups:   grp [2]
   grp   stratum term         df   sumsq  meansq statistic  p.value
   <chr> <chr>   <chr>     <dbl>   <dbl>   <dbl>     <dbl>    <dbl>
 1 a     block   N:P:K         1  69.0    69.0      3.12    0.328  
 2 a     block   Residuals     1  22.1    22.1     NA      NA      
 3 a     Within  N             1 119.    119.       6.89    0.0786 
 4 a     Within  P             1   0.270   0.270    0.0156  0.908  
 5 a     Within  K             1  58.1    58.1      3.36    0.164  
 6 a     Within  N:P           1  12.2    12.2      0.705   0.463  
 7 a     Within  N:K           1  23.4    23.4      1.35    0.329  
 8 a     Within  P:K           1  44.6    44.6      2.58    0.207  
 9 a     Within  Residuals     3  51.8    17.3     NA      NA      
10 b     block   N:P:K         1  29.3    29.3      0.431   0.630  
11 b     block   Residuals     1  67.9    67.9     NA      NA      
12 b     Within  N             1  73.0    73.0     57.3     0.00478
13 b     Within  P             1  21.3    21.3     16.7     0.0264 
14 b     Within  K             1  38.2    38.2     29.9     0.0120 
15 b     Within  N:P           1   8.52    8.52     6.68    0.0814 
16 b     Within  N:K           1  31.5    31.5     24.7     0.0156 
17 b     Within  P:K           1  47.3    47.3     37.1     0.00888
18 b     Within  Residuals     3   3.82    1.27    NA      NA 

-group_modify @andrew reece

> npk %>% 
     group_by(grp) %>% 
     group_modify(~aov(yield ~  N*P*K + Error(block), data = .) %>% 
     tidy)
# A tibble: 18 x 8
# Groups:   grp [2]
   grp   stratum term         df   sumsq  meansq statistic  p.value
   <chr> <chr>   <chr>     <dbl>   <dbl>   <dbl>     <dbl>    <dbl>
 1 a     block   N:P:K         1  69.0    69.0      3.12    0.328  
 2 a     block   Residuals     1  22.1    22.1     NA      NA      
 3 a     Within  N             1 119.    119.       6.89    0.0786 
 4 a     Within  P             1   0.270   0.270    0.0156  0.908  
 5 a     Within  K             1  58.1    58.1      3.36    0.164  
 6 a     Within  N:P           1  12.2    12.2      0.705   0.463  
 7 a     Within  N:K           1  23.4    23.4      1.35    0.329  
 8 a     Within  P:K           1  44.6    44.6      2.58    0.207  
 9 a     Within  Residuals     3  51.8    17.3     NA      NA      
10 b     block   N:P:K         1  29.3    29.3      0.431   0.630  
11 b     block   Residuals     1  67.9    67.9     NA      NA      
12 b     Within  N             1  73.0    73.0     57.3     0.00478
13 b     Within  P             1  21.3    21.3     16.7     0.0264 
14 b     Within  K             1  38.2    38.2     29.9     0.0120 
15 b     Within  N:P           1   8.52    8.52     6.68    0.0814 
16 b     Within  N:K           1  31.5    31.5     24.7     0.0156 
17 b     Within  P:K           1  47.3    47.3     37.1     0.00888
18 b     Within  Residuals     3   3.82    1.27    NA      NA      

-nest_by方法

> npk %>%
     nest_by(grp) %>% 
     mutate(out = list(tidy(aov(yield ~  N*P*K + Error(block), 
            data = data)))) %>%
     select(-data) %>% 
     unnest(out)
# A tibble: 18 x 8
# Groups:   grp [2]
   grp   stratum term         df   sumsq  meansq statistic  p.value
   <chr> <chr>   <chr>     <dbl>   <dbl>   <dbl>     <dbl>    <dbl>
 1 a     block   N:P:K         1  69.0    69.0      3.12    0.328  
 2 a     block   Residuals     1  22.1    22.1     NA      NA      
 3 a     Within  N             1 119.    119.       6.89    0.0786 
 4 a     Within  P             1   0.270   0.270    0.0156  0.908  
 5 a     Within  K             1  58.1    58.1      3.36    0.164  
 6 a     Within  N:P           1  12.2    12.2      0.705   0.463  
 7 a     Within  N:K           1  23.4    23.4      1.35    0.329  
 8 a     Within  P:K           1  44.6    44.6      2.58    0.207  
 9 a     Within  Residuals     3  51.8    17.3     NA      NA      
10 b     block   N:P:K         1  29.3    29.3      0.431   0.630  
11 b     block   Residuals     1  67.9    67.9     NA      NA      
12 b     Within  N             1  73.0    73.0     57.3     0.00478
13 b     Within  P             1  21.3    21.3     16.7     0.0264 
14 b     Within  K             1  38.2    38.2     29.9     0.0120 
15 b     Within  N:P           1   8.52    8.52     6.68    0.0814 
16 b     Within  N:K           1  31.5    31.5     24.7     0.0156 
17 b     Within  P:K           1  47.3    47.3     37.1     0.00888
18 b     Within  Residuals     3   3.82    1.27    NA      NA