如何使用 R 按组执行嵌套方差分析

How to perform nested anova by groups using R

我有几个样品,每天测量两次,持续 20 天,格式如下:

sample, concentration, day, replicate

如何使用以下函数获取每个样本的嵌套方差分析?

nest <- aov(concentration ~ day / factor(replicate))

如何在不创建 8 个子集的情况下 运行 每个“plant_type”的嵌套方差分析?

如果我理解正确的话,这个例子可能会对你有所帮助

图书馆

library(tidyverse)
library(broom)

示例

代码

mtcars %>% 
  # Create 2 factors variables
  mutate(
    gear = as.factor(gear),
    cyl = as.factor(cyl)
    ) %>% 
  # Group and nest data by each level of gear
  group_nest(gear) %>% 
  mutate(
    # Apply ANOVA por each level of gear, for drat~cyl
    anova = map(.x = data,.f = ~anova(aov(drat~cyl,data = .x))),
    # Get results of ANOVA
    results = map(anova,tidy)
    ) %>% 
  # "Show" the results for each gear level
  unnest(results)

输出

# A tibble: 6 x 9
  gear                 data anova           term         df sumsq meansq statistic p.value
  <fct> <list<tibble[,10]>> <list>          <chr>     <int> <dbl>  <dbl>     <dbl>   <dbl>
1 3               [15 x 10] <anova [2 x 5]> cyl           2 0.414 0.207      3.91   0.0491
2 3               [15 x 10] <anova [2 x 5]> Residuals    12 0.634 0.0529    NA     NA     
3 4               [12 x 10] <anova [2 x 5]> cyl           1 0.107 0.107      1.10   0.318 
4 4               [12 x 10] <anova [2 x 5]> Residuals    10 0.967 0.0967    NA     NA     
5 5                [5 x 10] <anova [2 x 5]> cyl           2 0.158 0.0790     0.352  0.740 
6 5                [5 x 10] <anova [2 x 5]> Residuals     2 0.449 0.225     NA     NA