pairwise.wilcox.test - 重新格式化输出

发布于 2024-12-11 06:01:21 字数 1120 浏览 0 评论 0原文

这是测试的正常输出:

attach(airquality)
pw <- pairwise.wilcox.test(Ozone, Month, p.adj = "bonf")
pw

data:  Ozone and Month 

     May    Jun    Jul    Aug   
Jun 1.0000 -      -      -     
Jul 0.0003 0.1414 -      -     
Aug 0.0012 0.2591 1.0000 -     
Sep 1.0000 1.0000 0.0074 0.0325

我最近必须进行 10 个因子水平的测试。虽然pairwise.wilcox.test的下三角格式有用且简洁,但我认为以与Tukey HSD输出类似的方式排列它会很方便,其中列出了每个成对组合及其相关的p值。这是我的尝试:

pw.df <- as.data.frame(pw$p.value)
pw.diff <- vector("character")
pw.pval <- vector("numeric")
for (i in 1:ncol(pw.df) )
  for (j in i:length(pw.df) ) {
    pw.diff <- c(pw.diff,paste(colnames(pw.df[i]),"-",rownames(pw.df)[j]))
    pw.pval <- c(pw.pval,pw.df[j,i])
  }


# order them by ascending p value
v <- order(pw.pval,decreasing = F)
pw.df <- data.frame(pw.diff[v],pw.pval[v])


# display those that are significant at the 5% level
pw.df[pw.df$pw.pval<0.05,]

  pw.diff.v.  pw.pval.v.
1  May - Jul 0.000299639
2  May - Aug 0.001208078
3  Jul - Sep 0.007442604
4  Aug - Sep 0.032479550

如果有人有一些关于如何使这更容易和/或更优雅的提示/技巧/建议,我将不胜感激。

This is the normal output from the test:

attach(airquality)
pw <- pairwise.wilcox.test(Ozone, Month, p.adj = "bonf")
pw

data:  Ozone and Month 

     May    Jun    Jul    Aug   
Jun 1.0000 -      -      -     
Jul 0.0003 0.1414 -      -     
Aug 0.0012 0.2591 1.0000 -     
Sep 1.0000 1.0000 0.0074 0.0325

I recently had to conduct a test with 10 levels of a factor. While the lower triangular format of the pairwise.wilcox.test is useful and concise, I thought it would be convenient to arrange it in a simlar way to the Tukey HSD output where each pairwise combination is listed along with it's asociated p value. This was my attempt to do this:

pw.df <- as.data.frame(pw$p.value)
pw.diff <- vector("character")
pw.pval <- vector("numeric")
for (i in 1:ncol(pw.df) )
  for (j in i:length(pw.df) ) {
    pw.diff <- c(pw.diff,paste(colnames(pw.df[i]),"-",rownames(pw.df)[j]))
    pw.pval <- c(pw.pval,pw.df[j,i])
  }


# order them by ascending p value
v <- order(pw.pval,decreasing = F)
pw.df <- data.frame(pw.diff[v],pw.pval[v])


# display those that are significant at the 5% level
pw.df[pw.df$pw.pval<0.05,]

  pw.diff.v.  pw.pval.v.
1  May - Jul 0.000299639
2  May - Aug 0.001208078
3  Jul - Sep 0.007442604
4  Aug - Sep 0.032479550

If anyone has some tips/tricks/advice on how to make this easier and/or more elegant I would be grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-12-18 06:01:21

我将使用 reshapereshape2 包来完成此任务,特别是 melt() 命令。 pairwise.wilcox.test 返回的对象包含第三个槽中感兴趣的数据,因此像 melt(pw[[3]]) 这样的东西应该可以解决问题:

    X1  X2       value
1  Jun May 1.000000000
2  Jul May 0.000299639
3  Aug May 0.001208078
4  Sep May 1.000000000
5  Jun Jun          NA
....

I would use the reshape or reshape2 package for this task, specifically the melt() command. The object returned by pairwise.wilcox.test contains the data of interest in the third slot, so something like melt(pw[[3]]) should do the trick:

    X1  X2       value
1  Jun May 1.000000000
2  Jul May 0.000299639
3  Aug May 0.001208078
4  Sep May 1.000000000
5  Jun Jun          NA
....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文