我想使用R包plyr在一个非常大的数据框架上运行一个配对的t测试,但是我不知道怎么做。我最近学习了如何使用plyr进行关联,我非常喜欢您如何指定要比较哪些组,然后plyr为您细分数据。例如,您可以让plyr计算虹膜数据集中的每种虹膜的萼片长度与萼片宽度之间的相关性,如下所示:
Correlations <- ddply(iris, "Species", function(x) cor(x$Sepal.Length, x$Sepal.Width))我可以自己分解数据框架,方法是指定刚毛虹膜物种的数据列在第1行:50行,等等,但是plyr不太可能像我那样搞砸,不小心说第1行:51行。
那么,我如何用配对t检验来做类似的事情呢?我怎样才能指定哪对观测是对的?下面是一些与我所做的工作类似的例子数据,我希望这两对成为研究对象,我想用农药来分解数据:
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检验,但它假设我总是有相同的顺序。虽然在上面的示例数据框架中,它们都是按相同的顺序排列的,但在实际数据中却没有,因为有时我会丢失数据。
发布于 2014-04-14 19:06:47
您是想要这个吗?
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-14 18:28:07
我不知道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 https://stackoverflow.com/questions/23067338
复制相似问题