![](/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.