如何得到n个数的所有可能组合?

时间:2022-02-21 21:38:32

I have 9 data sets, each having 115 rows and 742 columns and each data set contains results from a spectrometer taken under specific conditions.

我有9个数据集,每个数据集有115行和742列,每个数据集包含在特定条件下的光谱仪得到的结果。

I would like to analyze all combinations of these 9 data sets to determine the best conditions.

我想分析这9个数据集的所有组合,以确定最佳的条件。

Edit:
The data are spectral measurements(rows= samples,columns =wavelengths) taken at 10 different temperatures.

编辑:数据是在10种不同温度下采集的光谱测量数据(行=样本,列=波长)。

I would like to get all combinations of the 9 data sets and apply a function cpr2 to each combination. cpr2 takes a data set and makes a plsr model,predicts 9 test sets(the individual sets),and returns bias of prediction.

我希望得到9个数据集的所有组合,并对每个组合应用一个函数cpr2。cpr2获取一个数据集并建立一个plsr模型,预测9个测试集(单个集),并返回预测偏差。

My intention is to find which combination gave the smallest prediction biases i.e how many temperature conditions are need to give acceptable bias.

我的目的是找出哪个组合给出的预测偏差最小。需要多少温度条件才能给出可接受的偏差。

Based on suggestion:

基于建议:

I'm looking to do something like this

我想做这样的事情。

g<-c("g11","g12","g13,g21","g22","g23","g31","g32","g33") 
cbn<-combn(g,3) # making combinations of 3 

comb<-lapply(cbn,cpr2(cbn))

cpr2梳子< -lapply(立方氮化硼(cbn))

for reference cpr2 is

cpr2是供参考

   cpr2<-function(data){ 
      data.pls<-plsr(protein~.,8,data=data,validation="LOO") #make plsr model       
      gag11p.pred<-predict(data.pls,8,newdata=gag11p)  #predict each test set 
      gag12p.pred<-predict(data.pls,8,newdata=gag12p)
      gag13p.pred<-predict(data.pls,8,newdata=gag13p)
      gag21p.pred<-predict(data.pls,8,newdata=gag21p)
      gag22p.pred<-predict(data.pls,8,newdata=gag22p)            
      gag23p.pred<-predict(data.pls,8,newdata=gag23p)
      gag31p.pred<-predict(data.pls,8,newdata=gag31p)
      gag32p.pred<-predict(data.pls,8,newdata=gag32p)
      gag33p.pred<-predict(data.pls,8,newdata=gag33p)                        
      pred.bias1<-mean(gag11p.pred-gag11p[742])     #calculate prediction bias      
      pred.bias2<-mean(gag12p.pred-gag12p[742])
      pred.bias3<-mean(gag13p.pred-gag13p[742])         
      pred.bias4<-mean(gag21p.pred-gag21p[742])
      pred.bias5<-mean(gag22p.pred-gag22p[742])
      pred.bias6<-mean(gag23p.pred-gag23p[742])
      pred.bias7<-mean(gag31p.pred-gag31p[742])
      pred.bias8<-mean(gag32p.pred-gag32p[742])
      pred.bias9<-mean(gag33p.pred-gag33p[742])            
    r<-signif(c(pred.bias1,pred.bias2,pred.bias3,pred.bias4,pred.bias5,
          pred.bias6,pred.bias7,pred.bias8,pred.bias9),2)            
  out<-c(R2(data.pls,"train",ncomp=8),RMSEP(data.pls,"train",ncomp=8),r)
 return(out)          
}

Any insights into solving this will be appreciated.

对于解决这一问题的任何见解都将受到赞赏。

2 个解决方案

#1


7  

You don't say how you want to assess the pairs of matrices, but if you have your matrices as per the code you showed with those names, then

你没有说你想如何评估矩阵对,但是如果你有你的矩阵,就像你用这些名字展示的代码一样,那么

g <- c("g11", "g12", "g13", "g21", "g22", "g23", "g31", "g32", "g33", "g2")
cmb <- combn(g, 2)

which gives:

这使:

> cmb
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] [,11] [,12]
[1,] "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g12" "g12" "g12"
[2,] "g12" "g13" "g21" "g22" "g23" "g31" "g32" "g33" "g2"  "g13" "g21" "g22"
     [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] "g12" "g12" "g12" "g12" "g12" "g13" "g13" "g13" "g13" "g13" "g13" "g13"
[2,] "g23" "g31" "g32" "g33" "g2"  "g21" "g22" "g23" "g31" "g32" "g33" "g2" 
     [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
[1,] "g21" "g21" "g21" "g21" "g21" "g21" "g22" "g22" "g22" "g22" "g22" "g23"
[2,] "g22" "g23" "g31" "g32" "g33" "g2"  "g23" "g31" "g32" "g33" "g2"  "g31"
     [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
[1,] "g23" "g23" "g23" "g31" "g31" "g31" "g32" "g32" "g33"
[2,] "g32" "g33" "g2"  "g32" "g33" "g2"  "g33" "g2"  "g2"

are the set of combinations of your matrices taken 2 at a time.

是你每次取2个矩阵的组合的集合。

Then iterate over the columns of cmb doing your assessment, e.g.:

然后遍历cmb的列进行评估,例如:

FUN <- function(g, ...) {
    ## get the objects for the current pair
    g1 <- get(g[1])
    g2 <- get(g[2])
    ## bind together
    dat <- rbind(g1, g2)
    ## something here to assess this combination
    cpr2(dat)
}

assess <- apply(cmb, 2, FUN = FUN, ....)

#2


4  

Did you try combn? For example, if you want combinations of 3 drawn from a group of 10 elements you can use combn(10, 3)

你试过combn吗?例如,如果你想从一组10个元素中选择3个元素的组合,你可以使用combn(10,3)

#1


7  

You don't say how you want to assess the pairs of matrices, but if you have your matrices as per the code you showed with those names, then

你没有说你想如何评估矩阵对,但是如果你有你的矩阵,就像你用这些名字展示的代码一样,那么

g <- c("g11", "g12", "g13", "g21", "g22", "g23", "g31", "g32", "g33", "g2")
cmb <- combn(g, 2)

which gives:

这使:

> cmb
     [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9]  [,10] [,11] [,12]
[1,] "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g11" "g12" "g12" "g12"
[2,] "g12" "g13" "g21" "g22" "g23" "g31" "g32" "g33" "g2"  "g13" "g21" "g22"
     [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
[1,] "g12" "g12" "g12" "g12" "g12" "g13" "g13" "g13" "g13" "g13" "g13" "g13"
[2,] "g23" "g31" "g32" "g33" "g2"  "g21" "g22" "g23" "g31" "g32" "g33" "g2" 
     [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
[1,] "g21" "g21" "g21" "g21" "g21" "g21" "g22" "g22" "g22" "g22" "g22" "g23"
[2,] "g22" "g23" "g31" "g32" "g33" "g2"  "g23" "g31" "g32" "g33" "g2"  "g31"
     [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
[1,] "g23" "g23" "g23" "g31" "g31" "g31" "g32" "g32" "g33"
[2,] "g32" "g33" "g2"  "g32" "g33" "g2"  "g33" "g2"  "g2"

are the set of combinations of your matrices taken 2 at a time.

是你每次取2个矩阵的组合的集合。

Then iterate over the columns of cmb doing your assessment, e.g.:

然后遍历cmb的列进行评估,例如:

FUN <- function(g, ...) {
    ## get the objects for the current pair
    g1 <- get(g[1])
    g2 <- get(g[2])
    ## bind together
    dat <- rbind(g1, g2)
    ## something here to assess this combination
    cpr2(dat)
}

assess <- apply(cmb, 2, FUN = FUN, ....)

#2


4  

Did you try combn? For example, if you want combinations of 3 drawn from a group of 10 elements you can use combn(10, 3)

你试过combn吗?例如,如果你想从一组10个元素中选择3个元素的组合,你可以使用combn(10,3)