如何在R中跨多个独立向量执行成对t检验

  • 本文关键字:执行 向量 检验 独立
  • 更新时间 :
  • 英文 :


TL;DR版本

我有向量X1,X2,X3,。。。Xn。我想测试一下,对于每一个可能的向量组合,任何一个向量的平均值是否与任何其他向量的平均值有显著差异。我正在寻找一种在R中比运行n^2个单独的t.tests更好的方法。

完整故事

我有一个数据框架,里面装满了特定CSA的人口普查数据。每一行包含特定普查区的每个变量(列)的观测值。

我需要做的是比较不同MSA人口普查区内相同变量的平均值。换句话说,我想根据MSA指定变量(这是其中一列)对我的数据帧进行因子化,然后在每个新因子化的MSA中成对比较另一个感兴趣变量的平均值的差异。这本质上是在接下来的每个向量上进行成对的t.test,但我希望用一种比一遍又一遍地写t.test(MSAx,MSAy)更优雅的方式来做这件事。我该怎么做?

与@ashkan提出的方法相比,我下面的方法的优势在于我的方法可以删除重复项。(即X1对X2或X2对X1将出现在结果中,而不是同时出现)

# Generate dummy data
df <- data.frame(matrix(rnorm(100), ncol = 10))
colnames(df) <- paste0("X", 1:10)
# Create combinations of the variables
combinations <- combn(colnames(df),2, simplify = FALSE)
# Do the t.test
results <- lapply(seq_along(combinations), function (n) {
                  df <- df[,colnames(df) %in% unlist(combinations[n])]
                  result <- t.test(df[,1], df[,2])
                  return(result)})
# Rename list for legibility    
names(results) <- paste(matrix(unlist(combinations), ncol = 2, byrow = TRUE)[,1], matrix(unlist(combinations), ncol = 2, byrow = TRUE)[,2], sep = " vs. ")

只需使用pairwise.t.test,下面是一个示例:

x1 <- rnorm(50)
x2 <- rnorm(30, mean=0.2)
x3 <- rnorm(100,mean=0.1)
x4 <- rnorm(100,mean=0.4)
x <- data.frame(data=c(x1,x2,x3,x4),
                key=c(
                  rep("x1", length(x1)),
                  rep("x2", length(x2)),
                  rep("x3", length(x3)),
                  rep("x4", length(x4))) )
pairwise.t.test(x$data,
                x$key,
                pool.sd=FALSE)
#   Pairwise comparisons using t tests with non-pooled SD 
#
# data:  x$data and x$key 
#
#    x1     x2     x3    
# x2 0.7395 -      -     
# x3 0.9633 0.9633 -     
# x4 0.0067 0.9633 0.0121
#
# P value adjustment method: holm 

如果您有一个data.frame,并且您希望在data.frame的每列之间独立执行T-测试,则可以使用双重应用循环:

apply(MSA, 2, function(x1) {
  apply(MSA, 2, function(x2) {
    t.test(x1, x2)
  })
})

伴随这种暴力方法的一个很好的可视化是森林图:

cis <- apply(MSA, 2, function(x) mean(x) + c(-1, 1) * sd(x) * 1.96)
plot.new()
plot.window(xlim=c(1, ncol(cis)), ylim=range(cis))
segments(1:ncol(cis), cis[1, ], 1:ncol(cis), cis[2, ])
axis(1, at=1:ncol(cis), labels=colnames(MSA))
axis(2)
box()
abline(h=mean(MSA), lty='dashed')
title('Forest plot of 95% confidence intervals of MSA')

除了来自quarzgar的响应外,还有另一种方法可以在R中的多个因子之间执行成对ttest。基本上,这是通过创建因子水平的组合来使用的两个(或多个)因子的技巧。

2x2经典设计示例:

df <- data.frame(Id=c(rep(1:100,2),rep(101:200,2)),
               dv=c(rnorm(100,10,5),rnorm(100,20,7),rnorm(100,11,5),rnorm(100,12,6)),
             Group=c(rep("Experimental",200),rep("Control",200)),
             Condition=rep(c(rep("Pre",100),rep("Post",100)),2))
#ANOVA
summary(aov(dv~Group*Condition+Error(Id/Condition),data = df))
#post-hoc across all factors
df$posthoclevels <- paste(df$Group,df$Condition) #factor combination
pairwise.t.test(df$dv,df$posthoclevels)
#   Pairwise comparisons using t tests with pooled SD 
#
# data:  df$dv and df$posthoclevels 
#
#                 Control Post Control Pre Experimental Post
# Control Pre       0.60         -           -                
# Experimental Post <2e-16       <2e-16      -                
# Experimental Pre  0.26         0.47        <2e-16           
#
# P value adjustment method: holm 

最新更新