【问题标题】:How to get all possible combinations of n number of data set?如何获得n个数据集的所有可能组合?
【发布时间】:2011-09-13 16:20:15
【问题描述】:

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

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

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

我想获取 9 个数据集的所有组合,并将函数 cpr2 应用于每个组合。 cpr2 获取一个数据集并制作一个 plsr 模型,预测 9 个测试集(单个集),并返回预测偏差。

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

根据建议:

我想做这样的事情

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

comb&lt;-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)          
}

任何解决此问题的见解将不胜感激。

【问题讨论】:

  • 单个数据集的行和列指的是什么?它们是实验结果吗(cols 是波长/质量,而行是样本?) 10 个数据集是 10 个设置组合?如果不是,什么代表组合?如果这个问题的答案是有 10 * 115 * 750 个条件并且你想评估它们的所有个组合,我希望你已经准备好等待漫长的等待!
  • “这些数据集的所有组合”是什么意思?如果您的每个数据框具有相同的列名,您可以使用rbind() 将它们组合成一个数据框:g &lt;- rbind(g11,g12,g13,g21,g22,g23,g31,g32,g33,g2)
  • 您必须向我们提供有关您的数据的更多信息。我知道你有 10 个矩阵,但我不明白你想如何组合这些矩阵。保存例如你组合g11g12,这个组合矩阵是什么样的?一个有 230 行的矩阵?
  • @Gavin 数据集是光谱测量,其中列是波长 (750),行是样本 (115)。十个数据集是指进行测量的十个不同温度。我想评估这十个条件的所有组合。所有数据集的列名(波长)都相同,因此 g11 和 g12 的组合将按照 Andrie 的建议进行。
  • @adamleerich rbind() 会给我一个单一的数据集,however i want to assess how individual conditions interact, eg. g11,g31 and g33 or g11, g21,g22 and g33。我一直在手动进行选择,但我希望有更简单的方法。

标签: r


【解决方案1】:

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

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, ....)

【讨论】:

  • @Gavin 谢谢。我能够重现 cmb,但是为了好玩,我想使用调用 cpr2(data) 的函数,如果我理解正确,g1 和 g2 是一个组合,我申请 cpr2。我不确定如何获取 g1 和 g2 作为函数的数据。任何建议将不胜感激。
  • 不,不是真的,g1g2cmb 矩阵 g11g12 的第一列。这就是get() 所做的;它检索具有给定名称的对象。 apply() 将函数FUN 应用于cmb 的每一列。在FUN 中,您想要做任何您想做的事情来评估single 组合。 apply() 将确保依次考虑每个成对(在这种情况下)组合。我的 FUN 只是设置了一个环境,其中当前组合的两个数据集可用 - 我有 ..... 你需要你的电话。
  • 进一步@DinoSingh 我看不出cpr2() 与这个问题的所有可能组合位有何关系。 cpr2() 采用单个数据对象,但在这里每个组合至少有 2 个数据集(如果您想一次组合 3 个数据集,则更多)。您最初的问题充满了歧义,因此我的回答将非常笼统。如果您可以更具体(不要求我阅读其他问题),我会尝试在我的回答中更具体。
  • 我为歧义道歉,我已经编辑了我的问题,希望现在更清楚。感谢您的帮助。
  • @Gavin 很抱歉再次打扰您,但我在尝试 assess &lt;- apply(cmb, 3, FUN = FUN, ....)cmb 中的 2 更改为 3 并添加 get g([3])rbind(g1,g2,g3)in FUN 后遇到了 Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed ,不知道我做错了什么。
【解决方案2】:

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

【讨论】:

  • 谢谢,我发现它适用于一个简单的列表,但我无法将我的数据集组合为不同的数据框。我尝试将数据帧放在像 g
猜你喜欢
  • 2013-05-09
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多