在R中使用t.test()时出错-没有足够的'y'观测值 [英] Error using t.test() in R - not enough 'y' observations

查看:1110
本文介绍了在R中使用t.test()时出错-没有足够的'y'观测值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码出现此错误

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

我认为我收到此错误的原因是因为我正在对仅具有一个值的数据进行t.test(因此不会有平均值或标准差)与具有20个值的数据..有没有办法可以解决这..有一种方法可以忽略没有足够y观测值的数据???像if循环可能会起作用... pls帮助

I think the reason I got this error is because I'm doing a t.test for data that only has one value (so there wouldnt be a mean or an sd) vs data that has 20 values..is there a way I can get around this.. is there a way I can ignore the data that doesn't have enough y observations??? like an if loop might work...pls help

所以执行t.test的代码是

So my code that does the t.test is

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

其中x是类似于以下削减形式的数据

where x is data in the form of cuts similar to

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)中只有一个值...。

How can I ignore 'cuts' that have only 1 value in them like the example above where in 'cut[9:14)' theres only one value....

推荐答案

t检验的所有标准变体在其公式中均使用样本方差,并且无法用一个观察值进行计算,因为用n-除1,其中n是样本大小。

All standard variants of t-test use sample variances in their formulas, and you cannot compute that from one observation as you are dividing with n-1, where n is sample size.

这可能是最简单的修改,尽管由于您未提供样本数据,所以我无法对其进行测试(您可以 dput 您的数据到您的问题):

This would probably be the easiest modification, although I cannot test it as you did not provide sample data (you could dput your data to your question):

 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 中使用的索引:

Another option would be to modify the indices which are used in lapply:

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

这是第一个使用人工数据的情况的示例:

Here's an example of the first case with artificial data:

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 

这篇关于在R中使用t.test()时出错-没有足够的'y'观测值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆