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?