【问题标题】:Why R bootstrap function uses indices to calculate mean ratio?为什么 R bootstrap 函数使用索引来计算平均比率?
【发布时间】:2020-06-10 13:34:56
【问题描述】:

我希望使用 bootstrapping 来获取我拥有的样本的平均值。我一直在研究 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 索引的总和?它没有给出平均比率的值。

【问题讨论】:

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

标签: r function indexing mean statistics-bootstrap


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 1970-01-01
    • 2017-12-12
    • 2022-11-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多