R中的自举加权均值 [英] bootstrap weighted mean in R

查看:149
本文介绍了R中的自举加权均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何引导向量的均值:

I know how to bootstrap the mean of a vector:

library(boot)
samplemean <- function(x, d) {
  return(mean(x[d]))
}
results_qsec <- boot(data=mtcars$qsec, statistic = samplemean, R=1000)

但是我如何引导加权平均值,例如考虑值在mtcars$qsec中,而这些值的权重在mtcars$wt中?

but how do I bootstrap the weighted mean, considering for instance values are in mtcars$qsec and weights on these values are in mtcars$wt?

推荐答案

诀窍是指定weighted.mean的权重,作为boot参数的...参数的一部分.在这里,我使用j作为权重,并将其作为数据帧传递,以匹配data =参数.

The trick is to specify the weights for weighted.mean as part of the ... argument to boot. Here I use j for the weights, and pass it through as a data frame, to match the data = argument.

您在这里:

samplewmean <- function(d, i, j) {
    d <- d[i, ]
    w <- j[i, ]
    return(weighted.mean(d, w))   
  }

results_qsec <- boot(data= mtcars[, 7, drop = FALSE], 
                     statistic = samplewmean, 
                     R=10000, 
                     j = mtcars[, 6 , drop = FALSE])

返回:

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars[, 7, drop = FALSE], statistic = samplewmean, 
    R = 10000, j = mtcars[, 6, drop = FALSE])


Bootstrap Statistics :
    original       bias    std. error
t1* 17.75677 0.0006948823   0.3046888

比较:

weighted.mean(mtcars[,7], mtcars[,6])
[1] 17.75677

这篇关于R中的自举加权均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆