Search code examples
rstandard-errorstatistics-bootstrap

R calculate the standard error using bootstrap


I have this array of values:

> df
[1] 2 0 0 2 2 0 0 1 0 1 2 1 0 1 3 0 0 1 1 0 0 0 2 1 2 1 3 1 0 0 0 1 1 2 0 1 3
[38] 1 0 2 1 1 2 2 1 2 2 2 1 1 1 2 1 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0
[75] 0 0 0 0 0 1 1 0 1 1 1 1 3 1 3 0 1 2 2 1 2 3 1 0 0 1

I want to use package boot to calculate the standard error of the data. http://www.ats.ucla.edu/stat/r/faq/boot.htm

So, I used this command to pursue:

library(boot)
boot(df, mean, R=10)

and I got this error:

Error in mean.default(data, original, ...) : 
'trim' must be numeric of length one

Can someone help me figure out the problem? Thanks


Solution

  • If you are bootstrapping the mean you can do as follows:

    set.seed(1)
    library(boot)
    x<-rnorm(100)
    meanFunc <- function(x,i){mean(x[i])}
    bootMean <- boot(x,meanFunc,100)
    >bootMean
    
    ORDINARY NONPARAMETRIC BOOTSTRAP
    
    
    Call:
    boot(data = x, statistic = meanFunc, R = 100)
    
    
    Bootstrap Statistics :
         original      bias    std. error
    t1* 0.1088874 0.002614105  0.07902184
    

    If you just input the mean as an argument you will get the error like the one you got:

    bootMean <- boot(x,mean,100)
    Error in mean.default(data, original, ...) : 
      'trim' must be numeric of length one