2013-03-27 68 views
3

我得到这个错误我的代码错误 - 没有足够的“Y”观察

Error in t.test.default(dat$Value, x[[i]][[2]]) : 
    not enough 'y' observations 

我想我得到这个错误的原因是因为我在做吨。测试数据只有一个值(所以不会有一个意思或一个SD)与数据有20个值..有办法我可以解决这个问题..有没有办法我可以忽略数据,有足够的y观察?就像如果循环可能工作...请帮助

所以我的代码,不会的t.test是

t<- lapply(1:length(x), function(i) t.test(dat$Value,x[[i]][[2]])) 

其中x是在削减类似于

cut: [3:8) 
     Number Value 
3  15  8 
4  16  7 
5  17  6 
6  19  2.3 
this data goes on 
cut:[9:14) 
     Number Value 
7  21  15 
cut:[13:18) etc 
     Number Value 
8  22  49 
9  23  15 
10 24  13 

表单数据如何忽略只有1个值的“剪切”,就像上面的例子中'cut [9:14]'只有一个值...

回答

3

所有标准变体的t检验使用样本差异在他们的公式中,一个d你不能用一个观测值来计算这个观测值,因为你用n-1来划分,其中n是样本大小。

这很可能是最简单的修改,虽然我无法测试它,你没有提供的样本数据(你可以dput您的数据对您的问题):

t<- lapply(1:length(x), function(i){ 
    if(length(x[[i]][[2]])>1){ 
     t.test(dat$Value,x[[i]][[2]]) 
    } else "Only one observation in subset" #or NA or something else 
    }) 

另一种办法是修改索引它们在使用lapply

ind<-which(sapply(x,function(i) length(i[[2]])>1)) 
t<- lapply(ind, function(i) t.test(dat$Value,x[[i]][[2]])) 

这里的人工数据中的第一情况的示例:

x<-list(a=cbind(1:5,rnorm(5)),b=cbind(1,rnorm(1)),c=cbind(1:3,rnorm(3))) 
y<-rnorm(20) 

t<- lapply(1:length(x), function(i){ 
    if(length(x[[i]][,2])>1){ #note the indexing x[[i]][,2] 
     t.test(y,x[[i]][,2]) 
    } else "Only one observation in subset" 
    }) 

t 
[[1]] 

     Welch Two Sample t-test 

data: y and x[[i]][, 2] 
t = -0.4695, df = 16.019, p-value = 0.645 
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval: 
-1.2143180 0.7739393 
sample estimates: 
mean of x mean of y 
0.1863028 0.4064921 


[[2]] 
[1] "Only one observation in subset" 

[[3]] 

     Welch Two Sample t-test 

data: y and x[[i]][, 2] 
t = -0.6213, df = 3.081, p-value = 0.5774 
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval: 
-3.013287 2.016666 
sample estimates: 
mean of x mean of y 
0.1863028 0.6846135 


     Welch Two Sample t-test 

data: y and x[[i]][, 2] 
t = 5.2969, df = 10.261, p-value = 0.0003202 
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval: 
3.068071 7.496963 
sample estimates: 
mean of x mean of y 
5.5000000 0.2174829 
+0

我编辑我的问题,并添加了一些数据.....我试过你给了我,但我得到错误,如错误:意外的'{'在“t < - lapply(1:长度(x),函数(我){if(length(x [[i]] [[2]]> 1){“ – 2013-03-27 10:58:07

+0

糟糕,代码缺少一个括号,我更正了代码,应该立即工作。 – 2013-03-27 11:01:11

相关问题