为什么R引导函数使用指数来计算平均比率?

为什么R引导函数使用指数来计算平均比率?,r,function,indexing,mean,statistics-bootstrap,R,Function,Indexing,Mean,Statistics Bootstrap,我正在寻找使用引导为一个样本,我有一个平均值。我一直在研究R中的bootstrap包应用程序,发现了一些让我非常困惑的东西。在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中的bootstrap包应用程序,发现了一些让我非常困惑的东西。在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")
#使用城市数据的平均值比率的常规引导

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

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)

由于设置了
stype=“w”
,因此
boot()
函数将权重传递给
statistics
函数作为第二个参数,而不是索引。所以
w
是权重,而
ratio
函数正在计算均值的加权比率。@MrFlick谢谢!但我如何防止这种情况?在我的应用程序中,我有两个样本,我想看看它们的平均值是否相同。所以我计划做的是得到样本A的所有平均值-得到置信区间,样本B也是如此。然而,它分配的权重并不相等-它们与指数相关。