I want to use the R command 'boot' to calculate a bootstrapped mean. This doesn't work, though, and always returns the error message:
Error in mean.default(data, original, ...) :
'trim' must be numeric of length one
Here is an example of the code I am using:
df <- data.frame(
v1 <- c(0,159028,236220,9127,0,0),
v2 <- c(13, 42, 56, 9, 77, 34)
)
boot(df$v1, statistic = mean, R = 100)
Which returns the aforementioned error.
Why won't this command work? What can I do to resolve the issue?
CodePudding user response:
Your statistic
should be a function that takes two arguments: the first should be the data to be sampled, and the second should be the indices that boot
will use to subset your data. Since you are directly passing mean
to the statistic
argument, a vector of indices is being passed to the second argument of mean.default
, which is trim
. This argument expects a single number, and throws an error when passed a vector.
You need to create a little wrapper function that shows boot
how you want it to subset your data for sampling:
boot(df$v1, statistic = function(x, inds) mean(x[inds]), R = 100)
#>
#> ORDINARY NONPARAMETRIC BOOTSTRAP
#>
#>
#> Call:
#> boot(data = df$v1, statistic = function(x, inds) mean(x[inds]),
#> R = 100)
#>
#>
#> Bootstrap Statistics :
#> original bias std. error
#> t1* 67395.83 839.4583 39734.55