R 因子抽样方法

R a factor sampling methode

我想使用以下对象生成直接采样:

v <- c("piment","aubergine","carotte","oignon","chou","pommeDeTerre","Na")
n <- 12

## TEST 1 : crach R
tmp <- data.frame(matrix(rep(v,n), ncol = n))
expand.grid(tmp)

但这会产生一个过大的矩阵(2.176782336e+9种可能性)。所以我必须研究抽样,但我不知道如何让它代表我的人口。

如果您的研究样本由 v 组成,则从 v 重新采样 length(v) 个值(即 7)将为您提供有效的 bootstrap 个样本。重复此重采样 B 次,您 (non-parametrically) bootstrap 以适当的方式对您的研究样本进行了处理。

确保始终固定随机数生成器,以便可以在任何情况下重现您的重采样过程。

set.seed(1) # fix random number generator
B <- 1e4L

boot_fun <- function(data, B, n_sample) {
  boot <- replicate(
    B,
    data[sample(seq_along(data), n_sample, replace = TRUE)],
    simplify = FALSE
  )
  return(boot)
}
out <- boot_fun(data = v, B = B, n_sample = 12L)

#> out[[1]]
#[1] "chou"         "pommeDeTerre" "chou"         "chou"         "chou"         "pommeDeTerre" "carotte"      "chou"        
#[9] "piment"       "carotte"      "piment"       "oignon"