Skip to content
Prev 145341 / 398500 Next

n Realizations of a Stochastic Process assigned to dynamically generated variable names?

You can do things like this without explicit loops and if you store the individual realizations in a list or matrix then it is a lot cleaner than creating multiple objects.  Here is some example code to try:

tsnfun <- function(n, init=10, mean=0, sd=1){
 x <- numeric(n)
 x[1] <- init
 x[-1] <- rnorm(n-1, mean, sd)
 cumsum(x)
}

tsnmat <- replicate( 10, tsnfun( 250, mean=0.01, sd=0.025) )
tsnlist <- lapply( c(50, 100, 150, 200, 250), tsnfun, mean=0.01, sd=0.025 )

matplot(1:250, tsnmat, type='l', lwd=2, col='maroon')

colMeans(tsnmat)
apply(tsnmat, 2, acf)

op <- par(ask=TRUE)
lapply( tsnlist, function(x) plot.ts(x, lwd=2, col='maroon') )
par(op)

sapply( tsnlist, mean )
sapply( tsnlist, '[', 1:2 )
acf(tsnlist[[2]])


Hope this helps,