首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用plyr的配对t检验

使用plyr的配对t检验
EN

Stack Overflow用户
提问于 2014-04-14 18:20:52
回答 2查看 1.3K关注 0票数 1

我想使用R包plyr在一个非常大的数据框架上运行一个配对的t测试,但是我不知道怎么做。我最近学习了如何使用plyr进行关联,我非常喜欢您如何指定要比较哪些组,然后plyr为您细分数据。例如,您可以让plyr计算虹膜数据集中的每种虹膜的萼片长度与萼片宽度之间的相关性,如下所示:

代码语言:javascript
复制
Correlations <- ddply(iris, "Species", function(x) cor(x$Sepal.Length, x$Sepal.Width))

我可以自己分解数据框架,方法是指定刚毛虹膜物种的数据列在第1行:50行,等等,但是plyr不太可能像我那样搞砸,不小心说第1行:51行。

那么,我如何用配对t检验来做类似的事情呢?我怎样才能指定哪对观测是对的?下面是一些与我所做的工作类似的例子数据,我希望这两对成为研究对象,我想用农药来分解数据:

代码语言:javascript
复制
 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检验:

代码语言:javascript
复制
 TTests <- dlply(Exposure, "Pesticide", function(x) t.test(x$Exposure ~ x$Season))

如果我在里面加上"paired=T“,plyr会做一个配对的t检验,但它假设我总是有相同的顺序。虽然在上面的示例数据框架中,它们都是按相同的顺序排列的,但在实际数据中却没有,因为有时我会丢失数据。

EN

回答 2

Stack Overflow用户

发布于 2014-04-14 19:06:47

您是想要这个吗?

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2014-04-14 18:28:07

我不知道ddply,但下面是使用一些base函数的方法。

代码语言:javascript
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23067338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档