我尝试在R中对以下数据框进行t检验。
df <- structure(list(freq = c(9, 11, 14, 12, 10, 9, 16, 10, 11, 15,
13, 12, 12, 13, 13, 9, 16, 14, 12, 15, 16, 10, 11, 13, 14, 14,
14, 16, 8, 10, 14, 14, 11, 11, 11, 11, 13, 7, 12, 13, 14, 11,
11, 13, 10, 14, 10, 10, 12, 8, 9, 12, 14, 11, 12, 12, 14, 14,
14, 15, 12, 13, 14, 8, 9, 11, 10, 14, 12, 12, 9, 10, 8, 14, 11,
14, 9, 13, 13, 13, 10, 9, 13, 10, 13, 10, 13, 12, 11, 12, 10,
12, 8, 11, 12, 15, 12, 12, 11, 13, 12, 10, 13, 9, 11, 9, 11,
8, 12, 12, 12, 10, 11, 12, 9, 13, 14, 11, 11, 14, 13, 12, 14,
15, 12, 12, 12, 14), class = structure(c(3L, 3L, 2L, 2L, 2L,
2L, 2L, 3L, 2L, 3L, 4L, 4L, 4L, 4L, 3L, 2L, 3L, 2L, 1L, 4L, 1L,
4L, 1L, 4L, 2L, 2L, 3L, 3L, 2L, 4L, 1L, 4L, 4L, 4L, 3L, 3L, 3L,
2L, 1L, 4L, 3L, 3L, 1L, 4L, 1L, 2L, 2L, 3L, 3L, 4L, 2L, 2L, 3L,
3L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 4L, 1L, 1L, 1L, 2L, 2L, 3L,
2L, 3L, 2L, 3L, 3L, 4L, 2L, 1L, 4L, 1L, 1L, 3L, 2L, 2L, 2L, 3L,
1L, 1L, 1L, 1L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 4L,
4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 1L, 4L, 4L, 1L, 4L, 4L, 1L, 3L,
1L, 2L, 2L, 1L, 2L, 1L, 1L, 3L, 3L, 2L, 1L), .Label = c("ending",
"mobile", "stem.first", "stem.second"), class = "factor")), .Names = c("freq",
"class"), row.names = c(NA, -128L), class = "data.frame")
正如我在之前的帖子中所读到的,R中有不止一种方法可以做到这一点。
我尝试了两种方法,一种是使用t.test
函数,另一种是使用pairwise.t.test
函数。
对于使用t.test
,我按要比较的类别对数据框进行了子集处理,并在子集上运行了连续的t检验。
ending.vs.mobile <- df[df$class=="ending"|df$class=="mobile",]
ending.vs.first <- df[df$class=="ending"|df$class=="stem.first",]
ending.vs.second <- df[df$class=="ending"|df$class=="stem.second",]
mobile.vs.first <- df[df$class=="mobile"|df$class=="stem.first",]
mobile.vs.second <- df[df$class=="mobile"|df$class=="stem.second",]
first.vs.second <- df[df$class=="stem.first"|df$class=="stem.second",]
t.test(ending.vs.mobile$freq ~ ending.vs.mobile$class, var.equal=T)
t.test(ending.vs.first$freq ~ ending.vs.first$class, var.equal=T)
t.test(ending.vs.second$freq ~ ending.vs.second$class, var.equal=T)
t.test(mobile.vs.first$freq ~ mobile.vs.first$class, var.equal=T)
t.test(mobile.vs.second$freq ~ mobile.vs.second$class, var.equal=T)
t.test(first.vs.second$freq ~ first.vs.second$class, var.equal=T)
据我所理解(这里我可能会有错误),
pairwise.t.test
更加方便,因为我不需要创建所有的子集,可以直接在原始数据框上运行。pairwise.t.test(df$freq, df$class, p.adjust.method="none", paired=FALSE, pooled.sd=FALSE)
然而,我在这里得到不同的结果,最明显的是比较结束与词干.second:使用
t.test
得出 p=0.7,使用 pairwise.t.test
得出 p=0.1。出了什么问题?我做错了什么吗?
虽然问题本身已经解决,但我认为它发生的原因让我有点偏执(不再信任自己):
仅仅因为打错了
pooled.sd
而不是 pool.sd
,我得不到我期望的结果。难道这不容易出错吗?在许多其他情况下,您可以输入变体,例如
bonf
或 bonferroni
,fa()
或 factor()
等等。但是,在这里,pooled.sd
完全被忽略了,尽管实际上是想要 "pooled sd"。好吧,如果您仔细阅读输出的标题,您就可以猜到
pooled.sd
没有被识别,因为它仍然说 "t tests with pooled SD",但是如果我甚至不打印它,例如当将输出导入自编写的函数时,会发生什么呢?有可能这个错误永远不会被发现。我应该写信给 R 的一些开发者,在将来的版本中,这两个拼写变体都应该是有效的吗?