首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用apply和t-test时出错

使用apply和t-test时出错
EN

Stack Overflow用户
提问于 2017-03-16 21:44:52
回答 3查看 58关注 0票数 1

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

例如:

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

因此,使用这段代码,我希望得到6个t测试,其中每一行X对应于y的两个组。但是,当我运行代码时,我会得到这个错误:

代码语言:javascript
复制
 Error in t.test(y[x == 1], y[x == 0]) : 
  promise already under evaluation: recursive default argument reference or earlier problems? 

有人能解释错误并修改我的代码以得到我想要的东西吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-03-16 21:49:02

问题来自于在函数参数中重复使用变量名。这应该是可行的:

代码语言:javascript
复制
apply(x,1,function(x.f,y.f=y)t.test(y.f[x.f==1],y.f[x.f==0]))
票数 2
EN

Stack Overflow用户

发布于 2017-03-16 21:53:06

关于

代码语言:javascript
复制
apply(x,1,function(x,z)t.test(y[x==1],y[x==0]),y)

如果要在函数中使用第二个参数,还应该将其传递给apply

票数 1
EN

Stack Overflow用户

发布于 2017-03-17 01:04:05

以下作品:

代码语言:javascript
复制
> apply(x,1,function(a)t.test(y[a==1],y[a==0]))    
[[1]]

您应该为data.frames和向量中的数据提供更好的名称,以便x和y等可以用作通用变量。而且,没有必要将y发送到函数,因为所有测试都是相同的。

输出:

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

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

https://stackoverflow.com/questions/42845327

复制
相关文章

相似问题

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