Vishal Belsare
2008-May-25 19:02 UTC
[R] n Realizations of a Stochastic Process assigned to dynamically generated variable names?
I am interested in creating multiple (say 1000) time series, from a given stochastic process, of length 250. I want to refer to each realization with its own variable name, of the format say, tsn, where n is the n'th simulation. i.e. ts1, ts2, ts3, ts4, .... , ts1000 The way I am thinking of doing this is placing the following code within another loop, and the 'tsn' assignment should happen as ts1 and so on, the n being taken from the iteration of the outer loop. However, crucially, I don't know how/whether I can create variable names dynamically and assign values to them. #begin tsn <- (10); for (i in 1:250) { tsn <- c(tsn, tsn[i] + rnorm(1, mean=0.01, sd=0.025)) }; plot.ts(tsn, lwd=2, col="maroon"); #end Thanks in anticipation. Best Regards Vishal Belsare
Greg Snow
2008-May-26 17:47 UTC
[R] 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, ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Vishal Belsare [shoot.spam at gmail.com] Sent: Sunday, May 25, 2008 1:02 PM To: r-help at r-project.org Subject: [R] n Realizations of a Stochastic Process assigned to dynamically generated variable names? I am interested in creating multiple (say 1000) time series, from a given stochastic process, of length 250. I want to refer to each realization with its own variable name, of the format say, tsn, where n is the n'th simulation. i.e. ts1, ts2, ts3, ts4, .... , ts1000 The way I am thinking of doing this is placing the following code within another loop, and the 'tsn' assignment should happen as ts1 and so on, the n being taken from the iteration of the outer loop. However, crucially, I don't know how/whether I can create variable names dynamically and assign values to them. #begin tsn <- (10); for (i in 1:250) { tsn <- c(tsn, tsn[i] + rnorm(1, mean=0.01, sd=0.025)) }; plot.ts(tsn, lwd=2, col="maroon"); #end Thanks in anticipation. Best Regards Vishal Belsare ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.