Using 'boot' to calculate bootstrapped mean in R

Mitchell Zufelt

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?

Allan Cameron

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate bootstrapped confidence interval using the mean_CI_boot used in ggplot2?

From Dev

R - Calculate mean of rasterbrick

From Dev

Using numpy to calculate mean?

From Dev

R: Calculate mean by column in a list of dataframes using pipes %>% in dplyr

From Dev

How can I calculate confidence interval for a mean in R not using confint

From Dev

How to calculate mean by row for multiple groups using dplyr in R?

From Dev

Calculate mean days in R using dplyr, filter, group_by and summarise?

From Dev

R: calculate the row mean in a matrix

From Dev

Calculate a lag or lead mean in r

From Dev

Calculate min, maximum and mean in R

From Dev

Using Protractor Test with Bootstrapped AngularJS

From Dev

R: Efficiently Calculate Deviations from the Mean Using Row Operations on a DF (Without Using a For Loop)

From Dev

R: Bootstrapped binary mixed-model logistic regression using bootMer() of the new lme4 package

From Dev

How to iterate over dataframe using character vector and calculate the mean for matching items in R

From Dev

Calculate a mean, by a condition, within a factor [r]

From Dev

in R, how to calculate mean of all column, by group?

From Dev

How to calculate mean of two timestamp columns in R?

From Dev

for loop in R to calculate mean from list

From Dev

Calculate Mean of Multiply Columns with Condition in R

From Dev

calculate logarithm and mean, add the result to dataset in R

From Dev

Calculate mean based on marker in a second column in R

From Dev

r search along a vector and calculate the mean

From Dev

Calculate the mean value of symmetry data in a matrix with R

From Dev

calculate grand mean from means in r

From Dev

How to calculate mean points for elements in a List in R

From Dev

R program to calculate the mean of the results of the same experiments

From Dev

Nested loops in R to calculate a mean day

From Dev

Calculate daily mean of data frame in r

From Dev

Calculate mean of dataset in R that satisfies two conditions

Related Related

  1. 1

    How to calculate bootstrapped confidence interval using the mean_CI_boot used in ggplot2?

  2. 2

    R - Calculate mean of rasterbrick

  3. 3

    Using numpy to calculate mean?

  4. 4

    R: Calculate mean by column in a list of dataframes using pipes %>% in dplyr

  5. 5

    How can I calculate confidence interval for a mean in R not using confint

  6. 6

    How to calculate mean by row for multiple groups using dplyr in R?

  7. 7

    Calculate mean days in R using dplyr, filter, group_by and summarise?

  8. 8

    R: calculate the row mean in a matrix

  9. 9

    Calculate a lag or lead mean in r

  10. 10

    Calculate min, maximum and mean in R

  11. 11

    Using Protractor Test with Bootstrapped AngularJS

  12. 12

    R: Efficiently Calculate Deviations from the Mean Using Row Operations on a DF (Without Using a For Loop)

  13. 13

    R: Bootstrapped binary mixed-model logistic regression using bootMer() of the new lme4 package

  14. 14

    How to iterate over dataframe using character vector and calculate the mean for matching items in R

  15. 15

    Calculate a mean, by a condition, within a factor [r]

  16. 16

    in R, how to calculate mean of all column, by group?

  17. 17

    How to calculate mean of two timestamp columns in R?

  18. 18

    for loop in R to calculate mean from list

  19. 19

    Calculate Mean of Multiply Columns with Condition in R

  20. 20

    calculate logarithm and mean, add the result to dataset in R

  21. 21

    Calculate mean based on marker in a second column in R

  22. 22

    r search along a vector and calculate the mean

  23. 23

    Calculate the mean value of symmetry data in a matrix with R

  24. 24

    calculate grand mean from means in r

  25. 25

    How to calculate mean points for elements in a List in R

  26. 26

    R program to calculate the mean of the results of the same experiments

  27. 27

    Nested loops in R to calculate a mean day

  28. 28

    Calculate daily mean of data frame in r

  29. 29

    Calculate mean of dataset in R that satisfies two conditions

HotTag

Archive