1

我想使用 R 包 plyr 在一个非常大的数据帧上运行成对 t 测试,但我不知道该怎么做。我最近学习了如何使用 plyr 进行相关性,我非常喜欢如何指定要比较的组,然后 plyr 为您分解数据。例如,您可以让 plyr 计算 iris 数据集中每种鸢尾花的萼片长度和萼片宽度之间的相关性,如下所示:

Correlations <- ddply(iris, "Species", function(x) cor(x$Sepal.Length, x$Sepal.Width))

可以通过指定 setosa 鸢尾属植物的数据在 1:50 行等来分解数据框.

那么如何使用配对 t 检验做类似的事情呢?我怎样才能指定哪些观察是成对的?这是一些与我正在使用的数据相似的示例数据,我希望这些对成为主题,并且我想按 Pesticide 分解数据:

 Exposure <- data.frame("Subject" = rep(1:4, 6), 
                     "Season" = rep(c(rep("summer", 4), rep("winter", 4)),3),
                     "Pesticide" = rep(c("atrazine", "metolachlor", "chlorpyrifos"), each=8),
                      "Exposure" = sample(1:100, size=24))
 Exposure$Subject <- as.factor(Exposure$Subject)

换句话说,我想评估的问题是每个人在冬季和夏季的农药接触是否存在差异,我想分别针对这三种农药分别回答这个问题。

非常感谢提前!

编辑:为了澄清,这是如何在 plyr 中进行非配对 t 检验:

 TTests <- dlply(Exposure, "Pesticide", function(x) t.test(x$Exposure ~ x$Season))

如果我在其中添加“paired=T”,plyr将进行配对 t 检验,但它假定我总是有相同顺序的配对。虽然我在上面的示例数据框中确实以相同的顺序将它们全部保存,但我没有在我的真实数据中,因为我有时会丢失数据。

4

2 回答 2

2

你想要这个吗?

library(data.table)

# convert to data.table in place
setDT(Exposure)

# make sure data is sorted correctly
setkey(Exposure, Pesticide, Season, Subject)

Exposure[, list(res = list(t.test(Exposure[Season == "summer"],
                                  Exposure[Season == "winter"],
                                  paired = T)))
         , by = Pesticide]$res
#[[1]]
#
#        Paired t-test
#
#data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
#t = -4.1295, df = 3, p-value = 0.02576
#alternative hypothesis: true difference in means is not equal to 0
#95 percent confidence interval:
# -31.871962  -4.128038
#sample estimates:
#mean of the differences 
#                    -18 
#
#
#[[2]]
#
#        Paired t-test
#
#data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
#t = -6.458, df = 3, p-value = 0.007532
#alternative hypothesis: true difference in means is not equal to 0
#95 percent confidence interval:
# -73.89299 -25.10701
#sample estimates:
#mean of the differences 
#                  -49.5 
#
#
#[[3]]
#
#        Paired t-test
#
#data:  Exposure[Season == "summer"] and Exposure[Season == "winter"]
#t = -2.5162, df = 3, p-value = 0.08646
#alternative hypothesis: true difference in means is not equal to 0
#95 percent confidence interval:
# -30.008282   3.508282
#sample estimates:
#mean of the differences 
#                 -13.25
于 2014-04-14T19:06:47.897 回答
0

我不知道ddply,但这就是我如何使用某些base功能。

by(data = Exposure, INDICES = Exposure$Pesticide, FUN = function(x) {
  t.test(Exposure ~ Season, data = x)
})

Exposure$Pesticide: atrazine

    Welch Two Sample t-test

data:  Exposure by Season
t = -0.1468, df = 5.494, p-value = 0.8885
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -49.63477  44.13477
sample estimates:
mean in group summer mean in group winter 
               60.50                63.25 

---------------------------------------------------------------------------------------------- 
Exposure$Pesticide: chlorpyrifos

    Welch Two Sample t-test

data:  Exposure by Season
t = -0.8932, df = 4.704, p-value = 0.4151
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -83.58274  41.08274
sample estimates:
mean in group summer mean in group winter 
               52.25                73.50 

---------------------------------------------------------------------------------------------- 
Exposure$Pesticide: metolachlor

    Welch Two Sample t-test

data:  Exposure by Season
t = 0.8602, df = 5.561, p-value = 0.4252
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -39.8993  81.8993
sample estimates:
mean in group summer mean in group winter 
                62.5                 41.5 
于 2014-04-14T18:28:07.177 回答