首页 > 解决方案 > 我收到错误“矩阵上的下标数量不正确”

问题描述

我正在尝试执行配对排列 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))

标签: rt-test

解决方案


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其他东西,例如$statisticor $p.value

这是bootwith的示例R=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

推荐阅读