有没有办法应用按站点分组的 wilcoxon 测试?

Is there a way to apply wilcoxon test grouped by site?

我想对多个组的两个治疗使用 Wilcoxon 2 侧检验,即几个样本点中的每一个都有治疗前后 (Conc)。我想按站点将数据集拆分为一个列表,然后应用测试,这样我就可以为每个站点单独输出,但是,我无法将其设置为可以重复的函数。

我有多个站点(Site)和两个级别的处理(Scenario),得到的分数(Conc):

'data.frame':   7344 obs. of  6 variables:
 $ Site        : chr  "A" "B" "C" "D" ...
 $ Scenario    : chr  "1" "1" "1" "1" "2" "2" "2" "2" ...
 $ Conc        : num  4.7727 0.055 0.0552 0.055 0.055 ...

每个 Site/Scenario 组合中有多个 Conc 数据点 (~60)。我选择 Wilcoxon 测试的原因主要是因为每个站点的处理(场景)之间的样本数量略有不均匀。

当我对整个数据集使用这段代码时,我得到了一个合理的结果:

t1 <- wilcox.test(Conc ~ Scenario, data = data.frame)
t1

但是,此代码不会对每个网站单独应用测试。

我查看了我能找到的所有类似示例(在 SO 和其他地方),这是我能想到的最好的代码:

t2 = data.frame %>% group_by(Site) %>% do(tidy(wilcox.test(Conc~Scenario, data=data.frame), na.rm=TRUE, equal.var=FALSE))
t2

此代码为我提供了每个站点的输出,但所有测试输出都是相同的,甚至是 p 值:

# A tibble: 107 x 5
# Groups:   Site [107]
   Site     statistic p.value method                                      alternative
   <chr>       <dbl>   <dbl> <chr>                                             <chr>      
 1 A         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  
 2 B         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  
 3 C         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  
 4 D         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  
 5 E         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  
 6 F         6145702   0.690 Wilcoxon rank sum test with continuity correction two.sided  

谁能看出我做错了什么? 感谢您的帮助

2020 年 8 月 21 日编辑,以更准确地反映您的数据

这是一个 dplyrpurrr 的解决方案已编辑以包含 broom::tidy 结果...

# 'data.frame': 5626 obs. of 3 variables: 
# $ Site.Year: Factor w/ 3 levels "Baffle Creek at Newton Road_2018_2019",..: 1 1 1 1 1 1 1 1 1 1 ... 
# $ Scenario : chr "FF_Total" "FF_Total" "FF_Total" "FF_Total" ... 
# $ PAF : num 4.77 4.77 4.77 4.77 4.77

set.seed(2020)

Site.Year <- rep(c("Baffle Creek at Newton Road_2018_2019", 
                   "Baffle Creek at Newton Road_2017_2018", 
                   "Baffle Creek at Newton Road_2019_2020"), 50)
Scenario <- rep_len(c(rep("FF_Total", 4), rep("Not_FF_Total", 4)), 150)
PAF <- rnorm(150, mean = 2.5, sd = 1)

DailyPAF_long <- data.frame(Site.Year, Scenario, PAF)

DailyPAF_long$Site.Year <- factor(DailyPAF_long$Site.Year)
# str(DailyPAF_long)
# wilcox.test(PAF ~ Scenario, data = DailyPAF_long)

library(dplyr)
library(purrr)

DailyPAF_long %>% 
  base::split(Site.Year) %>% 
  purrr::map(~ wilcox.test(PAF ~ Scenario, data = .)) %>% 
  purrr::map_dfr(~ broom::tidy(.)) 

#> # A tibble: 3 x 4
#>   statistic p.value method                       alternative
#>       <dbl>   <dbl> <chr>                        <chr>      
#> 1       361  0.355  Wilcoxon rank sum exact test two.sided  
#> 2       219  0.0723 Wilcoxon rank sum exact test two.sided  
#> 3       380  0.195  Wilcoxon rank sum exact test two.sided