首页 > 解决方案 > 为什么 R bootstrap 函数使用索引来计算平均比率?

问题描述

我正在寻找使用自举来获取我拥有的样本的平均值。我一直在研究 R 中的引导程序包应用程序,我发现了一些让我非常困惑的东西。在 CRAN 上,这是为引导功能给出的官方示例:

# Usual bootstrap of the ratio of means using the city data
ratio <- function(d, w) sum(d$x * w)/sum(d$u * w)
boot(city, ratio, R = 999, stype = "w")

它在 R 中使用城市数据

为什么在函数中计算 x TIMES 索引的总和?它没有给出平均比率的值。

标签: rfunctionindexingmeanstatistics-bootstrap

解决方案


假设我们有一个例子,这个和样本是独立的,

library(boot)
set.seed(100)
x=rpois(100,3)
y=rpois(100,5)

您只需在 mean 函数中添加更多内容即可进行引导:

boot_x = boot(x,function(i,d)mean(i[d]),R=999)
boot.ci(boot_x,type="perc")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 999 bootstrap replicates

CALL : 
boot.ci(boot.out = boot_x, type = "perc")

Intervals : 
Level     Percentile     
95%   ( 2.79,  3.39 )  

boot_y = boot(y,function(i,d)mean(i[d]),R=999)

等等...

如果观察结果是成对的,并且您对差异感兴趣,则应该将它们放在 data.frame 中,然后执行以下操作:

x=rpois(100,3)
y= x+ rnorm(100,2,1)
df = data.frame(x,y)
boot_df = boot(df,function(i,d)mean(i[d,1] - i[d,2]),R=999)

推荐阅读