【问题标题】:bootstrap weighted mean in RR中的引导加权平均值
【发布时间】:2018-02-24 03:43:59
【问题描述】:

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

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

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

【问题讨论】:

    标签: r statistics-bootstrap


    【解决方案1】:

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

    给你:

    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
    

    【讨论】:

      【解决方案2】:

      方法如下:

      samplewmean <- function(data, d) {
        return(weighted.mean(x=data[d,1], w=data[d,2]))
      }
      
      results_qsec <- boot(data=mtcars[,c(7,6)], statistic = samplewmean, R=1000)
      

      【讨论】:

        猜你喜欢
        • 2018-08-08
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多