Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

r - How to get all possible combinations of n number of data set?

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.

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

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

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.

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

for reference cpr2 is

   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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...