使用apply和t-test时出错

时间:2017-03-16 21:44:52

标签: r runtime-error apply

我找不到代码中的错误和/或逻辑中的缺陷。我有一个矩阵X,0和1以及一个连续值的y向量,我想在R中进行2个样本t检验,其中行{{1}表示X的不同组。

例如:

y

因此,我希望使用此代码进行6次t检验,其中x = matrix(rbinom(60,1,.5),ncol=10) y = abs(rnorm(ncol(x))) apply(x,1,function(x,y=y)t.test(y[x==1],y[x==0])) 的每一行对应两组X。但是,当我运行代码时出现此错误:

y

有人可以解释错误并修改我的代码以获得我想要的内容。

3 个答案:

答案 0 :(得分:2)

问题来自于在函数参数中重用变量名。这应该有效:

apply(x,1,function(x.f,y.f=y)t.test(y.f[x.f==1],y.f[x.f==0]))

答案 1 :(得分:1)

怎么样?
apply(x,1,function(x,z)t.test(y[x==1],y[x==0]),y)

如果你想在函数中使用第二个参数,你也应该将它传递给apply

答案 2 :(得分:1)

以下作品:

> apply(x,1,function(a)t.test(y[a==1],y[a==0]))    
[[1]]

您应该为data.frames和向量中的数据提供更好的名称,以便x和y等可以用作常规变量。此外,无需向函数发送y,因为它对所有测试都是相同的。

输出:

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.43835, df = 5.377, p-value = 0.6782
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.6356057  0.9036413
sample estimates:
mean of x mean of y 
0.5807408 0.4467230 


[[2]]

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -0.80208, df = 5.5382, p-value = 0.4555
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.0985419  0.5644195
sample estimates:
mean of x mean of y 
0.4337110 0.7007722 


[[3]]

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.58194, df = 7.3884, p-value = 0.5779
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.5584942  0.9283034
sample estimates:
mean of x mean of y 
0.6329878 0.4480832 


[[4]]

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 1.1148, df = 4.8236, p-value = 0.3174
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.4919082  1.2308641
sample estimates:
mean of x mean of y 
0.7622223 0.3927443 


[[5]]

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = 0.23436, df = 5.5539, p-value = 0.8231
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -0.7818960  0.9439901
sample estimates:
mean of x mean of y 
0.5729543 0.4919073 


[[6]]

        Welch Two Sample t-test

data:  y[a == 1] and y[a == 0]
t = -1.015, df = 7.9168, p-value = 0.3401
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -1.0152988  0.3954558
sample estimates:
mean of x mean of y 
0.3855747 0.6954962 

仅适用于p值:

> apply(x,1,function(a)t.test(y[a==1],y[a==0])$p.value)
[1] 0.6781895 0.4555338 0.5779255 0.3173567 0.8231019 0.3400979