【问题标题】:Using 'boot' to calculate bootstrapped mean in RUsing \'boot\' to calculate bootstrapped mean in R
【发布时间】:2022-12-02 01:26:11
【问题描述】:

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?

【问题讨论】:

    标签: r statistics-bootstrap


    【解决方案1】:

    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
    

    【讨论】:

      猜你喜欢
      • 2022-12-15
      • 2022-12-02
      • 2022-12-27
      • 2022-12-27
      • 2022-12-01
      • 2022-12-02
      • 1970-01-01
      • 2022-12-26
      • 2022-12-28
      相关资源
      最近更新 更多