组之间的相关性和不同样本的等级与 R
Correlation between groups and ranks over different samples with R
我正在研究一些关于 DNA 的分数,每个位置都有一个分数。
我想找到一种方法来了解某些样本是否更有可能获得高分,不是一般情况下,而是每个位置的位置。
有些位置没有在所有样本上定义,有些样本没有给定位置的分数。
data.frame('pos'=c(1,2,3,1,2,3,1,2,5), 'sample'=c('A','A','A','B','B','B','C','C','C'), 'score'=c(1,10,5,20,40,10,0.1,5,4))
我想知道使用 spearman 相关性(我正在寻找排名,因为没有真正的生物学原因来比较位置 1 和 2,例如)一些样本是否更有可能具有 "top" 得分值。
我的困难在于我实际上有两个定性值:样本 ID 和位置,只有一个定量值。我没有设法向 R 表明我想以某种方式按位置对数据进行分组,然后对每个位置进行排名以研究排名的相关性。
最后,我想要一个 spearman 相关分数来评估样本 B 在大多数位置上的得分最高的数据集。
知道如何实现吗?
非常感谢!
也许这指出了一个有用的方向。
library(tidyverse)
df = data.frame('pos'=c(1,2,3,1,2,3,1,2,3), # Using 3 as the last position
'sample'=c('A','A','A','B','B','B','C','C','C'),
'score'=c(1,10,5,20,40,10,0.1,5,4))
# Compute rank of each sample within each position
ranked = df %>% group_by(pos) %>%
mutate(rank=rank(score, ties.method='min')) %>%
ungroup()
# B seems to consistently score higher
ggplot(ranked, aes(pos, rank, color=sample)) +
geom_point(size=5)
# Kruskal-Wallis rank sum test of the null hypothesis that the rankings
# are from the same distribution for all samples.
kruskal.test(ranked$rank, ranked$sample)
#>
#> Kruskal-Wallis rank sum test
#>
#> data: ranked$rank and ranked$sample
#> Kruskal-Wallis chi-squared = 8, df = 2, p-value = 0.01832
# Pairwise Wilcoxon test for B vs C
df %>% filter(sample!='A') %>%
group_by(pos) %>%
mutate(rank=rank(score, ties.method='min')) %>%
ungroup() %>%
pivot_wider(id_cols='pos', names_from='sample', values_from='rank') %>%
{wilcox.test(.$B, .$C, paired=TRUE)}
#> Warning in wilcox.test.default(.$B, .$C, paired = TRUE): cannot compute exact p-
#> value with ties
#>
#> Wilcoxon signed rank test with continuity correction
#>
#> data: .$B and .$C
#> V = 6, p-value = 0.1489
#> alternative hypothesis: true location shift is not equal to 0
如果所有分数都来自同一个分布,我认为您可以直接对分数进行这些相同的测试,而无需排名。
由 reprex package (v0.3.0)
于 2020 年 1 月 10 日创建
我正在研究一些关于 DNA 的分数,每个位置都有一个分数。 我想找到一种方法来了解某些样本是否更有可能获得高分,不是一般情况下,而是每个位置的位置。 有些位置没有在所有样本上定义,有些样本没有给定位置的分数。
data.frame('pos'=c(1,2,3,1,2,3,1,2,5), 'sample'=c('A','A','A','B','B','B','C','C','C'), 'score'=c(1,10,5,20,40,10,0.1,5,4))
我想知道使用 spearman 相关性(我正在寻找排名,因为没有真正的生物学原因来比较位置 1 和 2,例如)一些样本是否更有可能具有 "top" 得分值。 我的困难在于我实际上有两个定性值:样本 ID 和位置,只有一个定量值。我没有设法向 R 表明我想以某种方式按位置对数据进行分组,然后对每个位置进行排名以研究排名的相关性。
最后,我想要一个 spearman 相关分数来评估样本 B 在大多数位置上的得分最高的数据集。
知道如何实现吗?
非常感谢!
也许这指出了一个有用的方向。
library(tidyverse)
df = data.frame('pos'=c(1,2,3,1,2,3,1,2,3), # Using 3 as the last position
'sample'=c('A','A','A','B','B','B','C','C','C'),
'score'=c(1,10,5,20,40,10,0.1,5,4))
# Compute rank of each sample within each position
ranked = df %>% group_by(pos) %>%
mutate(rank=rank(score, ties.method='min')) %>%
ungroup()
# B seems to consistently score higher
ggplot(ranked, aes(pos, rank, color=sample)) +
geom_point(size=5)
# Kruskal-Wallis rank sum test of the null hypothesis that the rankings
# are from the same distribution for all samples.
kruskal.test(ranked$rank, ranked$sample)
#>
#> Kruskal-Wallis rank sum test
#>
#> data: ranked$rank and ranked$sample
#> Kruskal-Wallis chi-squared = 8, df = 2, p-value = 0.01832
# Pairwise Wilcoxon test for B vs C
df %>% filter(sample!='A') %>%
group_by(pos) %>%
mutate(rank=rank(score, ties.method='min')) %>%
ungroup() %>%
pivot_wider(id_cols='pos', names_from='sample', values_from='rank') %>%
{wilcox.test(.$B, .$C, paired=TRUE)}
#> Warning in wilcox.test.default(.$B, .$C, paired = TRUE): cannot compute exact p-
#> value with ties
#>
#> Wilcoxon signed rank test with continuity correction
#>
#> data: .$B and .$C
#> V = 6, p-value = 0.1489
#> alternative hypothesis: true location shift is not equal to 0
如果所有分数都来自同一个分布,我认为您可以直接对分数进行这些相同的测试,而无需排名。
由 reprex package (v0.3.0)
于 2020 年 1 月 10 日创建