![](/img/trans.png)
[英]How to calculate bootstrapped confidence interval using the mean_CI_boot used in ggplot2?
[英]Using 'boot' to calculate bootstrapped mean in R
我想使用 R 命令“boot”来计算自举平均值。 但是,这不起作用,并且总是返回错误消息:
Error in mean.default(data, original, ...) :
'trim' must be numeric of length one
这是我正在使用的代码示例:
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)
返回上述错误。
为什么这个命令不起作用? 我该怎么做才能解决这个问题?
您的statistic
应该是一个 function,它需要两个 arguments:第一个应该是要采样的数据,第二个应该是boot
将用来对数据进行子集化的索引。 由于您直接将mean
传递给statistic
参数,因此索引向量将传递给mean.default
的第二个参数,即trim
。 此参数需要一个数字,并在传递向量时抛出错误。
您需要创建一个小包装器boot
来显示您希望它如何对数据进行子集化以进行采样:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.