5

TL;DR 版

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

全文

我有一个包含特定 CSA 的人口普查数据的数据框。每行包含特定人口普查区域的每个变量(列)的观测值。

我需要做的是比较不同 MSA 的人口普查区域中相同变量的均值。换句话说,我想根据 MSA 指定变量(这是列之一)对我的 data.frame 进行分解,然后在每个新分解的 MSA 中成对比较另一个感兴趣的变量的均值差异。这本质上是在每个随后的向量上进行成对的 t.tests,但我希望以一种比一遍又一遍地编写 t.test(MSAx, MSAy) 更优雅的方式来做到这一点。我怎样才能做到这一点?

4

4 回答 4

8

下面我的方法对@ashkan 提出的方法的优势是我的方法可以删除重复项。(即 X1 vs X2 或 X2 vs 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. ")
于 2013-02-07T07:58:10.923 回答
8

只需使用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 
于 2013-08-26T15:20:07.020 回答
4

如果您有一个 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')
于 2013-02-07T01:22:54.007 回答
0

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

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 
于 2021-05-20T09:43:54.953 回答