Search code examples
rt-test

I get error "incorrect number of subscripts on matrix"


I am trying to perform a paired permutation t-test. I get an error " in t.star[r, ] <- res[[r]] : incorrect number of subscripts on matrix". Below is the code I wrote. Any pointers appreciated.

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)
 
}

Raw data, no need for the 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))

Solution

  • The boot() function requires its statistic argument to be a function that returns a vector. Your sample_mean function returns a list of class "htest" because that is the output of perm.t.test(). Based on the function name, I assume you want the estimate of the means of the differences from that test.

    If you change your function to look as follows, the code works.

    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)
    }
    

    If you want a different output from perm.t.test(), then swap $estimate for something else like $statistic or $p.value.

    Here is an example of boot with R=10 (to be manageable):

    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