[英]using tapply/dapply etc for t.tests
I have a data frame from an interlab study as follows http://pastebin.com/AD57AYD1 我有一个来自interlab研究的数据框架,如下所示: http://pastebin.com/AD57AYD1
Essentially lab=Laboratory, mat=material, fab=strength, thick=thickness 本质上是实验室=实验室,垫子=材料,制造厂=强度,厚度=厚度
I want t.test data to compare each lab for each type of material. 我想要t.test数据来比较每种材料的每种实验室。
Ie, for mat=v, I want to run a t.test to compare lab B against lab S. 即,对于mat = v,我想运行t.test以将实验室B与实验室S进行比较。
Similarly for materials c, n and l. 对于材料c,n和l同样。
Due to my inability to work out how to do this efficiently, I create a column using the intaraction function (I know there must be easier ways!) and then run t.tests for each combination I am interested in. 由于无法解决如何有效执行此操作,因此我使用intaraction函数创建了一个列(我知道必须有更简单的方法!),然后对我感兴趣的每个组合运行t.tests。
# create a new column with lab/mat factors combined
interlab$allfacts<-interaction(interlab$lab,interlab$mat)
tv<-with(interlab, t.test(fab[allfacts == "S.v"],
fab[allfacts == "B.v"],var.equal=FALSE))
tv
tl<-with(interlab, t.test(fab[allfacts == "S.l"],
fab[allfacts == "B.l"],var.equal=FALSE))
tl
... etc etc
I am sure that I should be able to use one of the plyr functions, perhaps something like this: 我确信我应该能够使用plyr函数之一,也许是这样的:
tapply(interlab$fab, list(interlab$lab,interlab$mat), t.test)
but this isn't working out. 但这没有解决。
Any help much appreciated. 任何帮助,不胜感激。 Pete
皮特
EDIT: Further to the comment below, I had also looked at the pairwise.t.test function in this respect, but it did too many comparisons (ie, it did a t-test of lab B nitrile versus lab S vinyl - which is irrelevant. I called it like this: 编辑:除了下面的评论,我在这方面也查看了pairwise.t.test函数,但是它进行了太多比较(即,它进行了实验室B腈与实验室S乙烯基的t检验-这是无关紧要,我这样称呼它:
pairwise.t.test(interlab$fab,interaction(interlab$mat,interlab$lab),paired=FALSE, pool.sd=FALSE)
and it gave me 它给了我
> pairwise.t.test(interlab$fab,interaction(interlab$mat,interlab$lab),paired=FALSE, pool.sd=FALSE)
Pairwise comparisons using t tests with non-pooled SD
data: interlab$fab and interaction(interlab$mat, interlab$lab)
c.B l.B n.B v.B c.S l.S n.S
l.B 0.54484 - - - - - -
n.B 3.8e-07 1.9e-06 - - - - -
v.B 0.93881 0.22393 3.6e-07 - - - -
c.S 0.00576 0.93881 1.2e-05 0.00026 - - -
l.S 0.00067 0.48601 2.5e-05 4.6e-05 0.89883 - -
n.S 4.3e-12 2.2e-10 0.92366 5.4e-12 6.7e-10 7.7e-10 -
v.S 0.93881 0.93881 1.9e-06 0.31885 0.01217 0.00169 1.3e-10
P value adjustment method: holm
I understand the problem with undertaking multiple comparisons and having to adjust the significance criteria. 我了解进行多次比较并必须调整重要性标准的问题。 I just couldn't get the pairwise call to only do the tests I was interested in.
我只是无法成对调用仅执行我感兴趣的测试。
there is a solution using the package plyr 有使用plyr软件包的解决方案
FUN <- function(x) {
mat <- matrix(numeric(), 1, 2)
colnames(mat) <- tail(colnames(x),2)
for (i in 1:ncol(mat)) {
mat[1,i] <- t.test(x[,"thick"]~x[,"lab"])$p.value
mat[1,i] <- t.test(x[,"fab"] ~ x[,"lab"])$p.value
}
mat
}
# if you are only interested by fab :
FUN <- function(x) {
t.test(x[,"fab"] ~ x[,"lab"])$p.value
}
ddply(tab, .(mat), FUN)
HTH 高温超导
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.