-1

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))
user1174152
  • 47
  • 1
  • 7
  • Please `dput(mydata)` or if the data is large, `dput(head(mydata,n = 20))` and paste that into your question. Without a reproducible sample of your data, we cannot diagnose your problem and propose solutions. – Ben Norris Sep 17 '20 at 16:45
  • I added the link to the data. – user1174152 Sep 17 '20 at 17:00
  • user1174152, when (not if) links go stale, the question becomes unreproducible. Your data was really small, so it was trivial to include it (using `dput`, as @BenNorris suggested). If you don't like my edit, feel free to roll it back. (Additionally, in general, some people are hesitant to click on arbitrary links in questions, so links often slow down potential answers.) Now that the data is here, you can [edit] your question to remove the now-unnecessary link to google drive, if you'd like. – r2evans Sep 17 '20 at 17:22

1 Answers1

1

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
Ben Norris
  • 5,639
  • 2
  • 6
  • 15