【问题标题】:How to make specific pairwise comparisons in R如何在 R 中进行特定的成对比较
【发布时间】:2012-05-08 04:11:12
【问题描述】:

我继承了一些代码,这些代码生成了一个显着性水平矩阵,用于从预测均值进行成对比较。由于该模型包括来自多个站点和治疗的数据,但我只想比较一个站点内治疗中的基因型,因此只有比较的子集是有意义的。

这是当前生成的虚拟版本。

effect.nam <- expand.grid(site=c("A","B","C"), treat=c("low","high"), genotype=c("A1","B2"))
labels <-  paste(effect.nam[,1],effect.nam[,2],effect.nam[,3], sep=".")
mat <-matrix(sample(c(T,F),144,replace=T),12,12)
dimnames(mat) <- list(labels,labels)

显然,在这种情况下,T/F 是随机的。我想要的是只看到一个站点和治疗中的比较。最好也删除自我比较。理想情况下,我想以以下形式返回数据框:

   Site Treat Genotype1 Genotype2   Sig
1     A   low         A1         2  TRUE
2     A   low         A1         3  TRUE
3     A   low         B2         3  TRUE
4     A  high         A1         2  TRUE
5     A  high         A1         3 FALSE
6     A  high         B2         3 FALSE
7     B   low         A1         2 FALSE
8     B   low         A1         3  TRUE
9     B   low         B2         3 FALSE
10    B  high         A1         2  TRUE
11    B  high         A1         3  TRUE
12    B  high         B2         3  TRUE
13    C   low         A1         2  TRUE
14    C   low         A1         3  TRUE
15    C   low         B2         3 FALSE
16    C  high         A1         2  TRUE
17    C  high         B1         3  TRUE
18    C  high         A2         3  TRUE

我做了一些错误的开始,如果有人能快速指出正确的方向,我将不胜感激。

在下面 Chase 给出的非常有用的答案中,您可以看到虽然无意义的比较已被删除,但每个有用的比较都包含两次(基因型 1 与基因型 2,反之亦然)。我看不出如何轻松删除这些,因为它们并不是真正的重复...

--更新--

抱歉,我需要更改 mat,以便在实施 Chase 的解决方案时,Genotype1Genotype2factor,而不是 int,就像我的实际情况一样。我在下面给出的解决方案中添加了一些内容(添加排序列以避免重复比较)。

它有效,这很好,但添加这些列对我来说似乎很尴尬 - 有没有更优雅的方法?

mat.m <- melt(mat)
mat.m[,c("site1", "treat1", "genotype1")] <-  colsplit(mat.m$X1, "\\.", c("site1", "treat1", "genotype1"))
mat.m[,c("site2", "treat2", "genotype2")] <-  colsplit(mat.m$X2, "\\.", c("site2", "treat2", "genotype2"))
str(mat.m)
mat.m$genotype1sort <- mat.m$genotype1
mat.m$genotype2sort <- mat.m$genotype2
levels(mat.m$genotype1sort) <- c(1, 2)
levels(mat.m$genotype2sort) <- c(1, 2)
mat.m$genotype1sort <- as.numeric(levels(mat.m$genotype1sort))[mat.m$genotype1sort]
mat.m$genotype2sort <- as.numeric(levels(mat.m$genotype2sort))[mat.m$genotype2sort]

subset(mat.m, site1 == site2 & treat1 == treat2 & genotype1 != genotype2 & genotype1sort < genotype2sort,
   select = c("site1", "treat1", "genotype1", "genotype2", "value"))

#-----
    site1 treat1 genotype1 genotype2 value
73      A    low        A1        B2  TRUE
86      B    low        A1        B2  TRUE
99      C    low        A1        B2  TRUE
112     A   high        A1        B2  TRUE
125     B   high        A1        B2 FALSE
138     C   high        A1        B2 FALSE

【问题讨论】:

  • 如果性能成为问题,我会考虑只生成您需要的比较,而不是进行大量无用的比较并在事后缩减该列表。

标签: r comparison significance


【解决方案1】:

我认为这可以使用reshape2 中的一些函数得到你想要的。一、melt将数据转成长格式:

require(reshape2)
mat.m <- melt(mat)
#--------
        X1      X2 value
1  A.low.1 A.low.1  TRUE
2  B.low.1 A.low.1  TRUE

接下来,将 X1 和 X2 列拆分为“.”上的三列

mat.m[,c("site1", "treat1", "genotype1")] <-  colsplit(mat.m$X1, "\\.", c("site1", "treat1", "genotype1"))
mat.m[,c("site2", "treat2", "genotype2")] <-  colsplit(mat.m$X2, "\\.", c("site2", "treat2", "genotype2"))

现在,mat.m 看起来像:

> head(mat.m,3)
        X1      X2 value site1 treat1 genotype1 site2 treat2 genotype2
1  A.low.1 A.low.1  TRUE     A    low         1     A    low         1
2  B.low.1 A.low.1  TRUE     B    low         1     A    low         1
3  C.low.1 A.low.1 FALSE     C    low         1     A    low         1         

最后,对您想要抓取的行进行子集化,并按照您想要的顺序获取列。请注意,除了subset,还有很多方法可以做到这一点,但为了清楚起见,我在这里使用它:

subset(mat.m, site1 == site2 & treat1 == treat2 & genotype1 != genotype2,
       select = c("site1", "treat1", "genotype1", "genotype2", "value"))
#--------
    site1 treat1 genotype1 genotype2 value
7       A    low         2         1  TRUE
20      B    low         2         1  TRUE

您可能会做一些更聪明的事情并避免将两列分开,但这似乎可以满足您的需求并且应该相当直接。

【讨论】:

  • 太棒了,我几乎一路走到了那里。我刚刚应用它并且效果很好。我想我已经完成了一半,但你让我的生活轻松了很多。如果我可以贪婪,我想排除双重比较(即 1 对 2 和 2 对 1)。有什么快速的想法吗?
  • @alexwhan - 太棒了,很高兴这有帮助!想想genotype1genotype2 之间的关系,目前我们将其定义为不等式,或者简称为genotype1 != genotype2。您将如何修改该关系以确保您仅获得两个比较之一,即1 vs 2 OR 2 vs 1
  • 对不起,我绞尽脑汁失败了。唯一明显的事情似乎是 >,但这对因素没有意义。我可以转换为数字,但这似乎很麻烦......
  • @alexwhan - 我们的想法是一样的……&lt;&gt;mat.m的结构是什么,即str(mat.m)?我证明genotype1genotype2 都是int,所以布尔比较可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多