'Creating a function to characterise repeated simulations

I want to create a function which helps characterise the results to some simulations. For the purposes of this post let the simulation function be:

example_sim <- function(time=100, npops=5){
  result <- data.frame(matrix(NA, nrow = time, ncol = npops))
  colnames(result) <- LETTERS[1:npops]
  for(i in 1:npops){
    sim <- sample.int(time, time)
    result[,i] <- sim
    result[,i] <- result[,i]*i
  }
  return(result)
}

This creates a data frame with varying length and width based on the number of populations (npops) and the time simulated.

I want to create a function which uses the output of such simulations and characterises the mean, variance for each population over an n amount of simulations (nsims).

So far I have managed to get it working for two populations with the following code:

library("matrixStats")
library("reshape2")

ensembles <- function(nsims=10, time = 100, npops = 2){
  result_N.A <- data.frame(matrix(NA, nrow = time, ncol = nsims))
  result_N.B <- data.frame(matrix(NA, nrow = time, ncol = nsims))
  for( i in 1:(nsims)){
    simulation_with_2pops <- example_sim(time=100,npops=2)
    result_N.A[,i] <- simulation_with_2pops[,1]
    result_N.B[,i] <- simulation_with_2pops[,2]
  }
  output <- simulation_with_2pops
  for( j in 1:params$ntime){
    output$meanA[j] <- rowMeans(result_N.A[j,])
  }
  for( j in 1:params$ntime){
    output$meanB[j] <- rowMeans(result_N.B[j,])
  }
  for( j in 1:params$ntime){
    output$varA[j] <- rowVars(as.matrix(result_N.A[j,]))
  }
  for( j in 1:params$ntime){
    output$varB[j] <- rowVars(as.matrix(result_N.B[j,]))
  }
  return(output)
} 
ensembles_output<- ensembles(nsims = 10)
ensembles_output

To fully implement the function for any number of populations I would need to create another for loop where I create and update the result_N.A object. (Presumably called something like result[i].) I have also thought about creating a 3 dimensional object (time, npops, nsims) and taking a slice of it to calculate the mean and variance but i havent had much success yet. I am not married for this route and am very open to other recommendations.

Eventually I would like to create a code where the covariance and correlation are also calculated by giving highlighting two populations in the parameters. (for instance population A and population E). If you have any ideas on the implementation i would be very grateful to hear them.

Thank you for considering this problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source