R bootstrap加权平均值(按分组,带数据表)

R bootstrap加权平均值(按分组,带数据表),r,data.table,statistics-bootstrap,R,Data.table,Statistics Bootstrap,我试图结合两种方法: 与 以下是一些随机数据: ## Generate sample data # Function to randomly generate weights set.seed(7) rtnorm <- function(n, mean, sd, a = -Inf, b = Inf){ qnorm(runif(n, pnorm(a, mean, sd), pnorm(b, mean, sd)), mean, sd) } # Generate variables np

我试图结合两种方法:

  • 以下是一些随机数据:

    ## Generate sample data
    
    # Function to randomly generate weights
    set.seed(7)
    rtnorm <- function(n, mean, sd, a = -Inf, b = Inf){
    qnorm(runif(n, pnorm(a, mean, sd), pnorm(b, mean, sd)), mean, sd)
    }
    
    # Generate variables
    nps    <- round(runif(3500, min=-1, max=1), 0) # nps value which takes 1, 0 or -1
    group  <- sample(letters[1:11], 3500, TRUE) # groups
    weight <- rtnorm(n=3500, mean=1, sd=1, a=0.04, b=16) # weights between 0.04 and 16
    
    # Build data frame
    df = data.frame(group, nps, weight)
    
    # The following packages / libraries are required:
    require("data.table")
    require("boot")
    
    …将显示错误消息:

    Error in weighted.mean.default(d, w) : 
      'x' and 'w' must have the same length
    
    运行

    dt[, list(list(boot(.SD, samplewmean, R = 5000, j = dt[, 3 , drop = FALSE]))), by = group]$V1
    
    dt[, list(list(boot(dt[, 2 , drop = FALSE], samplewmean, R = 5000, j = dt[, 3 , drop = FALSE]))), by = group]$V1
    
    …引发了一个不同的错误:

    Error in weighted.mean.default(d, w) : 
      (list) object cannot be coerced to type 'double'
    
    我仍然无法理解data.table中的参数以及如何组合运行data.table的函数


    如果有任何帮助,我将不胜感激。

    这与
    数据有关。表
    在函数范围内的行为。d仍然是
    数据。表
    中的
    样本平均值
    即使在使用
    i
    进行子集设置后,但
    加权。平均值
    需要权重和值的数字向量。如果在调用
    weighted.mean
    之前
    unlist
    ,您将能够修复此错误

    加权平均值中的误差默认值(d,w): (列表)对象不能强制为“double”类型

    在进入加权前取消列出的代码。平均值

    samplewmean <- function(d, i, j) {
      d <- d[i, ]
      w <- j[i, ]
      return(weighted.mean(unlist(d), unlist(w)))   
    }
    
    dt[, list(list(boot(dt[, 2 , drop = FALSE], samplewmean, R = 5000, j = dt[, 3 , drop = FALSE]))), by = group]$V1
    
    或者另一种可能的语法是:(参见)

    samplewmean
    
    Error in weighted.mean.default(d, w) : 
      (list) object cannot be coerced to type 'double'
    
    samplewmean <- function(d, i, j) {
      d <- d[i, ]
      w <- j[i, ]
      return(weighted.mean(unlist(d), unlist(w)))   
    }
    
    dt[, list(list(boot(dt[, 2 , drop = FALSE], samplewmean, R = 5000, j = dt[, 3 , drop = FALSE]))), by = group]$V1
    
    #a variable named original is being passed in from somewhere and i am unable to figure out from where
    samplewmean <- function(d, valCol, wgtCol, original) {
        weighted.mean(unlist(d[, ..valCol]), unlist(d[, ..wgtCol]))
    }
    
    dt[, list(list(boot(.SD, statistic=samplewmean, R=1, valCol="nps", wgtCol="weight"))), by=group]$V1
    
    samplewmean <- function(d, valCol, wgtCol, original) {
        weighted.mean(unlist(d[, eval(substitute(valCol))]), unlist(d[, eval(substitute(wgtCol))]))
    }
    
    dt[, list(list(boot(.SD, statistic=samplewmean, R=1, valCol=nps, wgtCol=weight))), by=group]$V1