我收到错误 "incorrect number of subscripts on matrix"

I get error "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)
 
}

原始数据,不需要 link:

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 参数是 returns 向量的函数。您的 sample_mean 函数 returns 是 class "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