0

I am executing a function (computeModules{bipartite}) which yields different results every time is run. I want to iterate the execution 100 times. The function's output consists of a list with several slots. I have to extract a matrix from one of these slots (@modules) and then subset it to obtain the rows I'm interested in. I am later using these rows to select positions of a vector. In the end, I want to obtain 100 lists with as many sublists as rows subsetted in each iteration.

Simulated @modules:

n1 <- structure(c(1, 1, 1, 2, 2, 2, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 4, 0, 0, 0, 0, 4, 0, 5, 0, 0, 0, 0, 5, 0, 6, 0, 0, 0, 0, 6, 0, 7, 0, 0, 0, 0, 7, 0, 8, 0, 0, 0, 0, 8, 0, 9, 0, 0, 9, 0, 0, 0, 10, 0, 0, 10, 0, 0, 0, 11, 0, 0, 0, 11, 0, 12, 0, 0, 12, 0, 0, 0, 0, 13, 0, 0, 0, 0, 13, 14, 0, 0, 14, 0, 0, 0, 0, 15, 0, 0, 0, 0, 15, 16, 0, 0, 16, 0, 0, 0, 17, 0, 0, 17, 0, 0, 0, 0, 18, 0, 0, 0, 0, 18, 0, 19, 0, 0, 0, 0, 19, 20, 0, 0, 20, 0, 0, 0, 0, 21, 0, 0, 21, 0, 0, 0, 22, 0, 0, 22, 0, 0, 0, 23, 0, 0, 0, 23, 0, 0, 24, 0, 0, 24, 0, 0, 0, 25, 0, 0, 25, 0, 0, 0, 26, 0, 0, 0, 26, 0, 0, 27, 0, 0, 27, 0, 0, 0, 28, 0, 0, 0, 28, 0), .Dim = c(7L, 30L))


n2 <- structure(c(1, 1, 2, 2, 2, 2, 2, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 3, 0, 0, 3, 0, 0, 0, 4, 0, 0, 4, 0, 0, 0, 5, 0, 0, 5, 0, 0, 0, 6, 0, 0, 6, 0, 0, 0, 7, 0, 0, 7, 0, 0, 0, 8, 0, 0, 8, 0, 0, 0, 9, 0, 0, 0, 0, 9, 0, 10, 0, 0, 0, 0, 10, 0, 11, 0, 0, 0, 11, 0, 12, 0, 12, 0, 0, 0, 0, 0, 13, 0, 0, 13, 0, 0, 14, 0, 14, 0, 0, 0, 0, 0, 15, 0, 0, 15, 0, 0, 16, 0, 16, 0, 0, 0, 0, 17, 0, 17, 0, 0, 0, 0, 0, 18, 0, 0, 18, 0, 0, 0, 19, 0, 0, 19, 0, 0, 20, 0, 20, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 21, 0, 22, 0, 0, 0, 0, 22, 0, 23, 0, 0, 0, 23, 0, 0, 24, 0, 0, 0, 0, 24, 0, 25, 0, 0, 0, 0, 25, 0, 26, 0, 0, 0, 26, 0, 0, 27, 0, 0, 0, 0, 27, 0, 28, 0, 0, 0, 28, 0), .Dim = c(7L, 30L))

My code:

e <- 1:30
nr=2
n=list(n1, n2); comp2=list(); u=list(); m=list()
for(i in 1:nr){
m[[i]] <- n[[i]][,-c(1,2, (ncol(n[[i]])-5):ncol(n[[i]]))] # select columns of interest
comp2[[i]] <- which(n[[i]][,1]==2) # subset by values of column 1 (total 5)
for(j in 1:length(comp2[[i]])){ # here I want to create a 2xlength(comp2[[i]]) list
u[[i]] <- e[unlist(m[[i]][comp2[[i]][j],])] # create list u selecting values of e
}
}

I would like to obtain:

> u
[1]]
> e[unlist(m[[1]][comp2[[1]][1],])]
[1] 12 14 16 17 20
> e[unlist(m[[1]][comp2[[1]][2],])]
[1]  2  9 10 21 22
> e[unlist(m[[1]][comp2[[1]][3],])]
[1] 11
> e[unlist(m[[1]][comp2[[1]][4],])]
[1]  1  3  4  5  6  7  8 13 15 18 19
[[2]]
> e[unlist(m[[2]][comp2[[2]][1],])]
[1] 12 14 16 17 20
> e[unlist(m[[2]][comp2[[2]][2],])]
[1] integer(0)
> e[unlist(m[[2]][comp2[[2]][3],])]
[1]  1  3  4  5  6  7  8 13 15 18 19
> e[unlist(m[[2]][comp2[[2]][4],])]
[1] 11
> e[unlist(m[[2]][comp2[[2]][5],])]
[1]  2  9 10 21 22

But my code overwrites the output of each iteration and only keeps the last one for each list:

> u
[[1]]
[1]  1  3  4  5  6  7  8 13 15 18 19
[[2]]
[1]  2  9 10 21 22

How can I get the desired list of lists?

EDIT: real data example

data <- structure(list(MC8 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), MC9 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L,     1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GC9 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GC8 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GC7 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), GC6 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), P8 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), P9 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), P10 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("MC8", "MC9", "GC9", "GC8", "GC7", "GC6", "GC5", "GC4", "GC3", "GC2", "GC1", "CC", "CP1", "CP2", "CP3", "CP4", "CP5", "CP6", "CP7", "CP8", "CP9", "S9", "S8", "S7", "S6", "S5", "S4", "S3", "S2", "S1", "P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"), class = "data.frame", row.names = c(NA, -40L))

Adapted code:

pl <- 40 # number of columns to be deleted in tmp@modules
eox <- unlist(dimnames(data)) # names of rows and columns
L <- vector("list", 2L) # lists
for (i in 1:length(L)) {
tmp <- computeModules(data)
tmp <- tmp@modules
ss1 <- which(tmp[,1]==1) # subsetting module 1
tmp <- tmp[,-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))] # delete columns of no interest
for (j in 1:length(ss1)) {   
L[[i]] <- eox[tmp[ss1[j],]]
# rows of tmp that meet subsetting conditions; use each row to select position in eox. Then, create as many sublists as rows in tmp3 and assign to L[[i]]
}

Everything works fine but the last line of code does not yield the desired output and the last loop overwrites the previous ones. Perhaps the desired output could be obtained more directly?

Santi XGR
  • 185
  • 2
  • 13
  • There doesn't appear to be a `computeModules` in `vegan`. There is one in `bipartite`. Please clarify. – Bryan Hanson Dec 16 '14 at 13:52
  • OK, we are getting close. A few more questions based upon your updated example. First, don't include things you don't need for the example, make it as lean as possible. Now, `pl` is equal to `ncol(tmp)` so it doesn't not need to be specified in advance, and `-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))` evaluates to `-c(1, 2, 1:40)` because `ncol(tmp)-ncol(tmp)+1` is just 1:40. So that is probably not what you want, because it says to remove all the columns. Try putting `str(tmp)` after that step. Maybe this is all the problem, but please check and edit your code and then I'll look at it again. – Bryan Hanson Dec 17 '14 at 15:12
  • On making the example lean: `nr` is not used, so drop it. Same with `ss2`. `pl` appears to be redundant as mentioned. All of the arguments to `computeModules` are the defaults but one, so all you need in the example is `tmp <- computeModules(data, deep = TRUE)`. Even `deep` is not necessary, your problem is not with that but with indexing later. – Bryan Hanson Dec 17 '14 at 15:14
  • I deleted those pieces of my simplified code inherited from the original that are not used in this example: `nr`, `ss2` and the default arguments in `computeModules`. However, `pl=40` should remain. If you execute the code line by line you'll see that `ncol(tmp@modules)`= 82, when you remove `-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))` you still have 40 columns. – Santi XGR Dec 17 '14 at 16:42

2 Answers2

0

Perhaps something like this pseudocode?

L <- vector("list", 100L)
for (i in 1:length(L)) {
    tmp <- computeModules(...) # your specific arguments
    tmp <- tmp@modules # just the slot you want
    tmp <- subset(tmp, ...) # your subsetting requirements
    L[[i]] <- tmp # put just the piece/answer you want into the list
}

res <- unlist(L)

Or maybe you need to loop over L again and do different stuff. Alternatively, if you did this: L[[i]] <- computeModules(...) that is your list of lists.

EDIT: Small version with data

library('bipartite')
data(small1976)
L <- vector("list", 1L)
for (i in 1:length(L)) {
    tmp <- computeModules(small1976) # your specific arguments
    tmp <- tmp@modules # just the slot you want
    tmp <- subset(tmp, ...) # your subsetting requirements
    L[[i]] <- tmp # put just the piece/answer you want into the list
}

Without subsetting, tmp is a numeric matrix. What do you need to do with it?

EDIT 2: If this is right, you were overthinking the subsetting process.

library("bipartite")
eox <- unlist(dimnames(data))
pl <- 40
L <- vector("list", 1L)
for (i in 1:length(L)) {
    tmp <- computeModules(data, deep = TRUE)
    tmp <- tmp@modules # just the slot you want
    tmp <- tmp[,-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))] # delete columns of no interest
    ss1 <- which(tmp[,1]==1) # subsetting module 1
    L[[i]] <- eox[ss1] # put just the piece/answer you want into the list
    }

Gives, for L:

[[1]]
[1] "1" "2" "4"

But there must be some random fitting going on, I get different answers on different runs.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • Your code is cleaner and faster but the problem in the end remains unsolved: it does not create a list within each L[[i]] even when I loop over L like this: for (j in 1:length(tmp1)) { L[[i]][[j]] <- eoxo[unlist(matrix(tmp3[tmp1[j],]))]}} – Santi XGR Dec 16 '14 at 23:05
  • I'm going to update my answer with some data so we have something more specific to troubleshoot. Maybe you could post your updated code as an edit to your original question too. – Bryan Hanson Dec 17 '14 at 00:31
  • `ss1` yields the rows of modules of order 1 (as `which(tmp[,1]==1)` would yield rows of modules of order 2). The elements within those rows are the key to extract the elements of the original community matrix within each module. Every time you execute `computeModule` the algorithm may or may not yield different outputs (quite similar in any case). I am interested in calculating the probability of finding each element in each module. I figured out a way to overcome the stated problem, but one has to renounce to get separate lists. However, there are a daunting obstacle ahead – Santi XGR Dec 17 '14 at 17:36
0

I found a way around to get the output. However, this output is combined in a data frame not divided in separate lists:

L <- vector("list", 2L)
for (i in 1:length(L)) {
tmp <- computeModules(data, deep = T, deleteOriginalFiles = T, steps = 1000000, tolerance = 1e-10, experimental = F)
tmp <- tmp@modules
s1 <- which(tmp[,1]==1) # subsetting module 1
tmp <- tmp[,-c(1,2,(ncol(tmp)-pl+1):ncol(tmp))] # delete columns of no interest
L[[i]] <- data.frame(tmp[s1,])
}
Santi XGR
  • 185
  • 2
  • 13
  • Sorry I couldn't be of more help. Don't forget you can put `str(some_object)` at strategic places in your code to get it to tell you what something is, and then you can choose the subsetting/indexing approach that best applies. Good Luck! – Bryan Hanson Dec 17 '14 at 17:52
  • Thank you very much Bryan! Actually, you made think of another approach, and this works fine for me. So, your effort has helped me – Santi XGR Dec 17 '14 at 18:52
  • by the way, `dplyr::str()` gives a nicer display of the data structure than `utils::str()` – Santi XGR Dec 18 '14 at 11:03
  • Where can I find that alternative `str`? I don't see one in `dplyr` but I'd like to try one out. Thanks. – Bryan Hanson Dec 18 '14 at 11:37
  • Sorry! The equivalent function was `dplyr::glimpse()` – Santi XGR Dec 18 '14 at 11:56