【问题标题】:Resampling the sampled vector multiple times in R在R中多次重新采样采样向量
【发布时间】:2020-03-27 20:22:39
【问题描述】:

假设我有一个长度为 100 的向量 y。我想从 y 中抽取 95 个值而不进行替换,并取该样本的平均值。然后,从新向量中,我想采样 90 个值并取该向量的平均值。我想这样做20次。最后,我的结果应该是一个长度为 20 的向量,每个向量都是一个样本的平均值。

我认为 for 循环可以解决这个问题,但我似乎无法弄清楚。

【问题讨论】:

    标签: r vector resampling


    【解决方案1】:
    ss = seq(from = 95, to = 20, by = -5)
    r = length(ss)
    i = 1
    x = rnorm(100)
    for(sampSize in ss){
      x = sample(x, size = sampSize)
      r[i] = mean(x)
      i = i + 1
    }
    

    【讨论】:

      【解决方案2】:

      这是一个简单使用Reduce的版本:

      x  <- rnorm(100)
      ss <- seq(95, 5, -5)
      
      draws <- Reduce(sample, ss, x, accumulate=TRUE)
      means <- sapply(draws, mean)
      

      或单行:

      means <- sapply(Reduce(sample, seq(95, 5, -5), rnorm(100), accumulate=TRUE), mean)
      

      【讨论】:

      • 在 10 年的 R 编程中,直到现在我才知道 base R 有 ReduceMapFilter。谢谢你启发我。
      • 为什么length(draws[[1]]) == ss[1] 会产生FALSE
      【解决方案3】:

      您可以使用mapply

      set.seed(42)
      x <- rnorm(100); s <- c(19:1*5,1)
      
      (res <- mapply(function(x, ...) mean(sample(x, ...)), list(x), s))
      # [1]  0.07869899  0.04229915  0.03902743  0.01012935  0.05068990 -0.06091937
      # [7]  0.01686019  0.15820150  0.03205169 -0.08031480 -0.25243403 -0.14227557
      # [13] -0.08546622  0.09478834 -0.13369267  0.09708245  0.20528372  0.17391816
      # [19] -0.54889423 -0.36105730
      

      请注意:

      length(res) == length(s)
      # [1] TRUE
      

      【讨论】:

        猜你喜欢
        • 2012-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多