r - 为什么 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 索引的总和?它没有给出平均比率的值。
解决方案
假设我们有一个例子,这个和样本是独立的,
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)
推荐阅读
- angular - 如何运行加载程序,直到从 Ionic 4 中的 API 获取响应
- google-cloud-platform - 如何将访问日志从每小时更改为每天或每周?
- php - 如果未定义参数,则显示对象数据
- python-3.x - 使用drawContours()厚度=-1填充轮廓不起作用
- sql - 返回相反的交易价值行
- javascript - 使用 PolyLines 将拖放形状与 FabricJS 连接起来
- java - ViewPager 没有从服务器获取图像
- javascript - 每个脚本文件只能有一个匿名定义调用
- dart - 无法在颤动中模拟单例类
- c - 二进制两个连续的 1 位