Re: [R] n Realizations of a Stochastic Process assigned to dynamically generated variable names?

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Mon, 26 May 2008 11:47:05 -0600

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.

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. Received on Mon 26 May 2008 - 18:19:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 26 May 2008 - 19:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive