在R中,我执行dunn的测试。我使用的函数没有根据输入变量的统计显着性差异对其进行分组的选项。然而,这是我真正感兴趣的,所以我试着写我自己的函数。不幸的是,我不能理解它。也许有人能帮上忙。
我使用R附带的airquality数据集作为示例。我需要的结果可能看起来有点像这样:
> library (tidyverse)
> ozone_summary <- airquality %>% group_by(Month) %>% dplyr::summarize(Mean = mean(Ozone, na.rm=TRUE))
# A tibble: 5 x 2
Month Mean
<int> <dbl>
1 5 23.6
2 6 29.4
3 7 59.1
4 8 60.0
5 9 31.4当我运行dunn.test时,我得到以下结果:
> dunn.test::dunn.test (airquality$Ozone, airquality$Month, method = "bh", altp = T)
Kruskal-Wallis rank sum test
data: x and group
Kruskal-Wallis chi-squared = 29.2666, df = 4, p-value = 0
Comparison of x by group
(Benjamini-Hochberg)
Col Mean-|
Row Mean | 5 6 7 8
---------+--------------------------------------------
6 | -0.925158
| 0.4436
|
7 | -4.419470 -2.244208
| 0.0001* 0.0496*
|
8 | -4.132813 -2.038635 0.286657
| 0.0002* 0.0691 0.8604
|
9 | -1.321202 0.002538 3.217199 2.922827
| 0.2663 0.9980 0.0043* 0.0087*
alpha = 0.05
Reject Ho if p <= alpha从这个结果中,我推断出5月与7月和8月不同,6月与7月不同(但不同于8月),等等。因此,我想将显著不同的组添加到我的结果表中:
# A tibble: 5 x 3
Month Mean Group
<int> <dbl> <chr>
1 5 23.6 a
2 6 29.4 ac
3 7 59.1 b
4 8 60.0 bc
5 9 31.4 a 虽然我是手工完成这项工作的,但我认为一定有可能将此过程自动化。然而,我没有找到一个好的起点。我创建了一个包含所有比较的数据帧:
> ozone_differences <- dunn.test::dunn.test (airquality$Ozone, airquality$Month, method = "bh", altp = T)
> ozone_differences <- data.frame ("P" = ozone_differences$altP.adjusted, "Compare" = ozone_differences$comparisons)
P Compare
1 4.436043e-01 5 - 6
2 9.894296e-05 5 - 7
3 4.963804e-02 6 - 7
4 1.791748e-04 5 - 8
5 6.914403e-02 6 - 8
6 8.604164e-01 7 - 8
7 2.663342e-01 5 - 9
8 9.979745e-01 6 - 9
9 4.314957e-03 7 - 9
10 8.671708e-03 8 - 9我认为一个遍历此数据框并使用选择变量从letters()中选择正确字母的函数可能会起作用。然而,我甚至想不到一个起点,因为改变行数必须同时考虑……
也许有人有个好主意?
发布于 2021-10-03 08:41:14
也许您可以查看rcompanion库中的cldList()函数,您可以通过管道从dunnTest()输出中输出res结果,并创建一个表来指定每个组的紧凑字母显示比较。
发布于 2021-11-26 16:38:20
遵循@TylerRuddenfort的建议,下面的代码将会工作。第一个cld是使用rcompanion::cldList创建的,第二个直接使用multcompView::multcompLetters创建。请注意,要使用multcompLetters,必须从比较名称中删除空格。
在这里,我使用了FSA:dunnTest进行邓恩测试(1964)。
一般来说,我建议在运行dunnTest之前按照中位数或均值对组进行排序,例如,如果你计划使用cld,那么cld就会以合理的顺序出现。
library (tidyverse)
ozone_summary <- airquality %>% group_by(Month) %>% dplyr::summarize(Mean = mean(Ozone, na.rm=TRUE))
library(FSA)
Result = dunnTest(airquality$Ozone, airquality$Month, method = "bh")$res
### Use cldList()
library(rcompanion)
cldList(P.adj ~ Comparison, data=Result)
### Use multcompView
library(multcompView)
X = Result$P.adj <= 0.05
names(X) = gsub(" ", "", Result$Comparison)
multcompLetters(X)https://stackoverflow.com/questions/62694809
复制相似问题