r语言 - 我收到错误"incorrect number of subscripts on matrix"



我正在尝试执行配对排列t检验。我得到一个错误";在t.star中[r,]<-res[[r]]:矩阵上下标的数目不正确;。下面是我写的代码。感谢任何指点。

library(RVAideMemoire)
library(boot)
library(xlsx)

mydata <- read.xlsx("C:/data_bootstrap.xlsx",1)
results = boot(mydata,statistic=sample_mean,R=500) 
print (results)
sample_mean <- function(mydata, indices) {
sam=mydata[indices,1]
sam1=mydata[indices,2]


bar = perm.t.test(sam,sam1,paired=TRUE,nperm=500)

return(bar)

}

原始数据,无需链接:

structure(list(Random = c(11L, 10L, 11L, 11L, 10L, 10L, 36L, 11L, 10L, 16L, 16L, 10L, 16L, 10L, 16L, 10L, 11L, 11L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 10L, 10L, 10L, 11L), Statement = c(11L, 10L, 11L, 10L, 10L, 16L, 16L, 10L, 10L, 16L, 11L, 11L, 10L, 10L, 16L, 11L, 10L, 11L, 16L, 10L, 11L, 10L, 16L, 10L, 10L, 10L, 11L, 10L, 10L)), class = "data.frame", row.names = c(NA, -29L))

boot()函数要求其statistic参数是一个返回向量的函数。sample_mean函数返回类"htest"的列表,因为这是perm.t.test()的输出。根据函数名称,我假设您想要该测试的差异平均值的估计值。

如果您将函数更改为如下所示,则代码可以工作。

sample_mean <- function(mydata, indices) {

sam=mydata[indices,1]

sam1=mydata[indices,2]


bar = perm.t.test(sam,sam1,paired=TRUE,nperm=500)

return(bar$estimate)
}

如果您想要不同于perm.t.test()的输出,则将$estimate交换为其他内容,如$statistic$p.value

以下是bootR=10的示例(便于管理(:

results = boot(mydata,statistic=sample_mean,R=10) 
print(results)
ORDINARY NONPARAMETRIC BOOTSTRAP

Call:
boot(data = mydata, statistic = sample_mean, R = 10)

Bootstrap Statistics :
original    bias    std. error
t1* 0.5172414 0.1206897    0.720067

相关内容

最新更新