有序值行中的排名组合

Vid*_*ach 5 combinations r permutation

我需要生成一个代码(最好在 R 中),该代码生成如何在 3 个独立组(组大小为 2、3、4)之间划分 9 个等级的所有相同可能性。我试图提出一些想法,但似乎无济于事。有人可以帮助解决这个问题吗?

我没有给出任何具体的值。所以应该有 9!/(2!3!4!)=1260 种不同的可能性。

我的想法都不起作用

jbl*_*d94 6

x <- RcppAlgos::permuteGeneral(3, 9, TRUE, 2:4)

dim(x)
#> [1] 1260    9

head(x)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> [1,]    1    1    2    2    2    3    3    3    3
#> [2,]    1    1    2    2    3    2    3    3    3
#> [3,]    1    1    2    2    3    3    2    3    3
#> [4,]    1    1    2    2    3    3    3    2    3
#> [5,]    1    1    2    2    3    3    3    3    2
#> [6,]    1    1    2    3    2    2    3    3    3
Run Code Online (Sandbox Code Playgroud)

的列x对应于 9 个等级。的行x对应于将 9 个等级分为 3 个组的唯一分配。的元素x对应于为每个分配分配排名的组。

标杆管理

library(RcppAlgos)

microbenchmark::microbenchmark(
  permuteGeneral = permuteGeneral(3, 9, TRUE, 2:4),
  comboGroups = comboGroups(9, 3, 2:4),
  Thomas = f(1:9, 2:4),
  Allan = {unlist(lapply(asplit(combn(9, 2), 2), \(x) {
    lapply(asplit(combn(setdiff(1:9, x), 3), 2), 
           \(y) list(first = x, second = y, third = setdiff(1:9, c(x, y))))
  }), recursive = FALSE)}
)
#> Unit: microseconds
#>            expr     min      lq      mean   median       uq     max neval
#>  permuteGeneral    19.2    26.4    38.751    37.65    46.35    91.5   100
#>     comboGroups    34.2    45.1    60.819    61.20    70.80   152.7   100
#>          Thomas 10258.9 10986.4 12912.926 12099.50 14028.30 21670.0   100
#>           Allan 13314.6 14256.7 17343.145 15978.95 18749.60 35892.4   100
Run Code Online (Sandbox Code Playgroud)

一个更大的问题:

microbenchmark::microbenchmark(
  permuteGeneral = permuteGeneral(5, 15, TRUE, 1:5),
  comboGroups = comboGroups(15, 5, 1:5),
  times = 10
)
#> Unit: milliseconds
#>            expr       min       lq      mean    median       uq      max neval
#>  permuteGeneral  848.4244  853.683  962.7133  971.9733 1050.486 1127.023    10
#>     comboGroups 1279.6234 1391.748 1501.5075 1495.7773 1588.031 1713.698    10
Run Code Online (Sandbox Code Playgroud)

毫不奇怪,RcppAlgos比基本选项快几个数量级。然而,有趣的是,permuteGeneral它比comboGroups. 当然,使用哪个可能更多地取决于您希望如何格式化结果(沿列排名或沿列分组)。