R calculate the standard error using bootstrap, Error in is.data.frame(x) :

1.1k views Asked by At

I used next program code to estimate the standard error using bootstrap for Libras Data movement set:

mydata<-read.table('C:/Users/Desktop/libra.txt', sep=',', header=TRUE)
head(data)
custom.boot <- function(times, data=mydata) {
  boots <- rep(NA, times)
  for (i in 1:times) {
    boots[i] <- sd(sample(data, length(data), replace=TRUE))/sqrt(length(data))  
  }
  boots
}
# Mean standard error
mean(custom.boot(times=1000))

But I got next error:

Error in is.data.frame(x) : 
  (list) object cannot be coerced to type 'double'

Could you help me to figure out the problem and give advice how it can be solved? Thanks in advance!

1

There are 1 answers

5
Stedy On

The mydata object is being read in as a 395 x 91 data.frame which custom.boot is struggling with. When I added changed to data = mydata[, 1] the function ran without an error. If you want to leave your function as is, I would either loop through every column or stack all the columns into one long column.

EDIT:

If you want to loop through all the columns of this data.frame, I would write a loop similar to what you have but one that slices by each column:

for(i in 1:ncol(mydata)){
  custom.boot(times=1000, data=mydata[, i])
  print(mean(custom.boot(times=1000)))
}