如何获得n个数据集的所有可能组合?

时间:2011-09-12 04:51:57

标签: r

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

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

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

我想获得9个数据集的所有组合,并为每个组合应用函数cpr2cpr2获取数据集并生成plsr模型,预测9个测试集(各个集合),并返回预测偏差。

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

基于建议:

我正在做这样的事情

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是

   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)          
}

任何有关解决此问题的见解都将受到赞赏。

2 个答案:

答案 0 :(得分:7)

你没有说 你想要如何评估矩阵对,但是如果你根据你用这些名字显示的代码有你的矩阵,那么

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

给出:

> 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"

是您的矩阵组合,一次取2个。

然后迭代进行评估的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, ....)

答案 1 :(得分:4)

你尝试过梳理吗?例如,如果您想要从一组10个元素中抽取3个组合,则可以使用combn(10, 3)

相关问题