代码之家  ›  专栏  ›  技术社区  ›  banan3'14

取一个具有特定平均值的样本

  •  3
  • banan3'14  · 技术社区  · 6 年前

    我试着使用 sample 函数,使用自定义概率向量,但不起作用:

    population <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
    mean(population)
    minimum <- min(population)
    maximum <- max(population)
    amplitude <- maximum - minimum 
    expected <- 6
    n <- length(population)
    prob.vector = rep(expected, each=n)
    for(i in seq(1, n)) {
      if(expected > population[i]) {
        prob.vector[i] <- (i - minimum) / (expected - minimum)
      } else {
        prob.vector[i] <- (maximum - i) / (maximum - expected)
      }
    }
    sample.size <- 5
    sample <- sample(population, sample.size, prob = prob.vector)
    mean(sample)
    

    一个好的例子是:

    • {3,5,6,8,9},平均值=6.2
    • {2,3,4,8,9},平均值=5.6

    问题不同于 sample integer values in R with specific mean

    概率向量图: plot

    1 回复  |  直到 6 年前
        1
  •  2
  •   Onyambu    6 年前

    您可以尝试以下方法:

    m = local({b=combn(1:23,5);
               d = colMeans(b);
               e = b[,d>5.5 &d<6.5];
               function()sample(e[,sample(ncol(e),1)])})
    m()
    [1] 8 5 6 9 3
    m()
    [1]  6  4  5  3 13
    

    b=combn(1:23,5) # combine the numbers into 5
    d = colMeans(b) # find all the means
    e = b[,d>5.5 &d<6.5] # select only the means that are within a 0.5 range of 6
    sample(e[,sample(ncol(e),1)]) # sample the values the you need