0

I have a list in R which looks something like this

b0=5;b1=2
f <- function(x) b0 + b1*x
Nsim <- 100
my.list <- vector("list", Nsim)
for(i in 1:Nsim){
   x <- rep(0,1000)
   y <- x
   y[1] <- f(x[1])
   for(j in 2:1000){
    x[j] <- x[j-1] + rnorm(1,0,0.1)
    y[j] < f(x[j])
   }

   my.list[[i]]$x <- x
   my.list[[i]]$y <- y
}

In reality, f is the result of an optimisation routine and x tracks the input value over time and y is the function values which are generated. So in essence, I have Nsim time series. I want to plot metrics of these time series over time by averaging over the index i. For instance, the average performance of the algorithm over time.

At the moment I'm doing this with a bespoke function for each metric I want to calculate (e.g. mean squared error of x from the true value of x, another for generating error bars and so on). I want to use something like lapply to average over i so I can visualise how x and y evolve over time but that doesn't do the right thing.

Is what I want to output is a pointwise summary of the results. As an analogy, if my.list[[i]]$x was instead stored as a matrix, I could take colMeans() to see the average value of x over "time".

Is there a function/package which is good for working with lists of lists?

jcken
  • 435
  • 3
  • 9
  • This question might be interesting but it lacks an expected output. What do you want to extract from `my.list` exactly? – Dan Chaltiel Mar 03 '21 at 11:20
  • @DanChaltiel updated question to clarify output – jcken Mar 03 '21 at 12:03
  • Sorry but this still not quite enough. You give an example of a nested list and you want to perform something on it. What should the function/package output? Please give some code representation of this output. Please understand that not being precise enough can lead someone to waste time giving an answer that will not help you at all. Asking good questions can be surprisingly challenging, this might help you: https://stackoverflow.com/q/5963269/3888000 – Dan Chaltiel Mar 03 '21 at 12:08

1 Answers1

0

At least for what has been presented there is no real reason to use a list of lists. The x's are all the same and equal to 1, 2, 3, ... so this could be represented by a matrix with the x component being implicit or represented by row names or we could represent this as a ts object or zoo object. In the last two cases if X is the object time(X) is the common x.

mat <- sapply(my.list, "[[", "y")

ts(mat)

library(zoo); zoo(mat)

Alternately, get rid of my.list and construct one of these directly in the code.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341