R中的配对t检验
我试图对按因素分组的数据进行R配对t检验:R中的配对t检验,r,for-loop,hypothesis-test,R,For Loop,Hypothesis Test,我试图对按因素分组的数据进行R配对t检验: > head(i.o.diff,n=20) # Difference Tree.ID Tree.Name Ins Outs #1 0.20 AK-1 Akun 1.20 1.0 #2 -1.60 AK-2 Akun 0.40 2.0 #3 -0.60 AK-3 Akun 1.40 2.0 #4 0.40 AK-4
> head(i.o.diff,n=20)
# Difference Tree.ID Tree.Name Ins Outs
#1 0.20 AK-1 Akun 1.20 1.0
#2 -1.60 AK-2 Akun 0.40 2.0
#3 -0.60 AK-3 Akun 1.40 2.0
#4 0.40 AK-4 Akun 0.40 0.0
#5 1.30 AK-5 Akun 1.80 0.5
#6 2.70 J-1 Jaror 10.20 7.5
#7 6.60 J-2 Jaror 10.60 4.0
#8 2.50 J-3 Jaror 6.00 3.5
#9 7.50 J-4 Jaror 22.00 14.5
#10 -4.50 J-5 Jaror 5.00 9.5
#11 3.50 Ce-1 Ku'ch 4.00 0.5
#12 -0.70 Ce-2 Ku'ch 4.80 5.5
#13 1.60 Ce-3 Ku'ch 2.60 1.0
#14 -2.40 Ce-4 Ku'ch 2.60 5.0
#15 -1.75 Ce-5 Ku'ch 2.25 4.0
我首先尝试使用:
pairwise.t.test(i.o.diff$In,i.o.diff$Out,g=i.o.diff$Tree.Name,paired=TRUE,pool=FALSE,p.adj="none",alternative=c("less"),mu=0)
但是我得到了错误
complete中出错。案例(x,y):并非所有参数都具有相同的长度
这对我来说没什么意义
我考虑过使用ddply()
、apply()
、和summaryBy()
,但无法使其工作,因为成对t检验的输入需要2个向量,而且我提到的大多数函数似乎在仅对一列进行“操作”时工作得最好
为了解决这个问题,我尝试使用for循环来达到相同的目的:
for(i in unique(i.o.diff$Tree.Name)) {
pair_sub<-subset(i.o.diff,Tree.Name==i)
t.pair<-t.test(pair_sub$Ins,pair_sub$Outs,paired="True")
print(t.pair)
}
for(唯一的i.o.diff$Tree.Name)){
pair\u sub删除了for循环中关于TRUE的引号。现在效果很好。来自R文档:
t、 测试{stats}R文档学生的t-测试:
Description:
Performs one and two sample t-tests on vectors of data. Usage t.test(x, …)
Default S3 method:
t.test(x, y = NULL, alternative = c(“two.sided”, “less”, “greater”), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, …)
i、 o.diff$In应该是i.o.diff$Ins,结尾加个“s”…i.o.diff$OUT也是一样。不?如果提问者不删除,简单的打字错误就是关闭的原因。我尝试了复制此内容;即使更正了打字错误,我也不会得到与您相同的错误。您可能希望重新启动R会话并重试。这是正确的,罗杰,但我只是修复了输入错误,得到了完全相同的错误。我确实重新启动了我的R会话,但没有任何效果……此外,我的for循环中没有输入错误,因此这并不能解决这个问题。