0

I have the following script:

randomdiv <- function(ncells, ndivs, size, accuracy) { sz <- matrix(nrow = ncells, ncol = ndivs)

for (j in 1:ncells) {
total_subunits <- size
for (i in 1:ndivs)
{

accurate_subunits <- (size * accuracy)
random_subunits <- round(size - accurate_subunits)

random_inh <- rbinom(1, random_subunits, 0.5)
accurate_inh <- (accurate_subunits / 2)

total_inh <- 2 * (random_inh + accurate_inh)

sz[j,i] <- total_inh
total_subunits <- total_inh

}
}
return (do.call(rbind, replicate(100, sz, simplify = FALSE)))
}

Such that I thought randomdiv(5, 20, 10, 0) would return a matrix with 500 rows, where the original sz matrix had been replicated 100 times. In fact, this is the case. However, the replicates are identical rather than each replicate being a fresh generation of data, which is what I need.

Any ideas how I can make sure that each replicate is a new matrix, not literally a replicate of the first one to be generated?

Jordan
  • 131
  • 8
  • Just because `sz` appears inside `randomdiv` does not mean that `randomdiv` will be called when you `replicate` `sz`. It will just be whatever its value is in the `globalenv()`. No call to that function will happen. You should return `sz` from `randomdiv` and call `randomdiv` from `replicate`. – IRTFM Oct 18 '15 at 15:29
  • I understand your point, but I was specifically trying to write this kind of function into the script so that when I hit `randomdiv()` it would return a replicate of the matrix and from that I can get straight on with statistical analysis. The point is that I have to alter `size` and `accuracy` for analysis, and so if I can't get the script to return a replicated matrix, I have to vectorise every combination of the matrices and then use those. Which means it's still not being randomly generated and adds a massive amount of extra lines to the code. – Jordan Oct 18 '15 at 15:36

0 Answers0