我的代码出现此错误
Error in t.test.default(dat$Value, x[[i]][[2]]) :
not enough 'y' observations
Run Code Online (Sandbox Code Playgroud)
我认为我收到此错误的原因是因为我正在对只有一个值的数据(因此不会有平均值或标准差)与具有 20 个值的数据进行 t.test ......有没有办法可以解决这个问题..有没有办法可以忽略没有足够 y 观察值的数据???就像 if 循环可能有效...请帮助
所以我执行 t.test 的代码是
t<- lapply(1:length(x), function(i) t.test(dat$Value,x[[i]][[2]]))
Run Code Online (Sandbox Code Playgroud)
其中 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
Run Code Online (Sandbox Code Playgroud)
我怎样才能忽略只有 1 个值的“剪切”,就像上面的例子,其中“剪切[9:14)”中只有一个值......
t 检验的所有标准变体都在其公式中使用样本方差,并且您无法通过除以 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
})
Run Code Online (Sandbox Code Playgroud)
另一种选择是修改以下中使用的索引lapply:
ind<-which(sapply(x,function(i) length(i[[2]])>1))
t<- lapply(ind, function(i) t.test(dat$Value,x[[i]][[2]]))
Run Code Online (Sandbox Code Playgroud)
这是第一种情况的人工数据示例:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33776 次 |
| 最近记录: |