Dear R users, I'd like to compute X like below. X_{i,t} = 0.1*t + 2*X_{i,t-1} + W_{i,t} where W_{i,t} are from Uniform(0,2) and X_{i,0} = 1+5*W_{i,0} Of course, I can do this with "for" statement, but I don't think it's good idea because "i" and "t" are too big. So, my question is that Is there any better idea to avoid "for" statement for this problem? Thank you in advance. Kathie -- View this message in context: http://r.789695.n4.nabble.com/do-call-or-something-instead-of-for-tp4634421.html Sent from the R help mailing list archive at Nabble.com.
Hi: Use arima.sim because when you have recursive relationships like that, there's no way to not loop. If arima.sim doesn't allow for the trend piece (0.1*t ), then you can do that part seperately using cumsum(0.1*(1:t)) and then add it back in to what arima.sim gives you. I don't remember if arima.sim allows for a trend. It might. Mark On Mon, Jun 25, 2012 at 8:39 AM, Kathie <kathryn.lord2000@gmail.com> wrote:> Dear R users, > > I'd like to compute X like below. > > X_{i,t} = 0.1*t + 2*X_{i,t-1} + W_{i,t} > > where W_{i,t} are from Uniform(0,2) and X_{i,0} = 1+5*W_{i,0} > > Of course, I can do this with "for" statement, but I don't think it's good > idea because "i" and "t" are too big. > > So, my question is that > > Is there any better idea to avoid "for" statement for this problem? > > Thank you in advance. > > Kathie > > -- > View this message in context: > http://r.789695.n4.nabble.com/do-call-or-something-instead-of-for-tp4634421.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi Kathryn: I'm sorry because I didn't read your question carefully enough. arima.sim won't help because you don't have a normal error term. I think you have to loop unless someone else knows of a way that I'm not aware of. good luck. On Mon, Jun 25, 2012 at 8:39 AM, Kathie <kathryn.lord2000@gmail.com> wrote:> Dear R users, > > I'd like to compute X like below. > > X_{i,t} = 0.1*t + 2*X_{i,t-1} + W_{i,t} > > where W_{i,t} are from Uniform(0,2) and X_{i,0} = 1+5*W_{i,0} > > Of course, I can do this with "for" statement, but I don't think it's good > idea because "i" and "t" are too big. > > So, my question is that > > Is there any better idea to avoid "for" statement for this problem? > > Thank you in advance. > > Kathie > > -- > View this message in context: > http://r.789695.n4.nabble.com/do-call-or-something-instead-of-for-tp4634421.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On Mon, Jun 25, 2012 at 05:39:45AM -0700, Kathie wrote:> Dear R users, > > I'd like to compute X like below. > > X_{i,t} = 0.1*t + 2*X_{i,t-1} + W_{i,t} > > where W_{i,t} are from Uniform(0,2) and X_{i,0} = 1+5*W_{i,0} > > Of course, I can do this with "for" statement, but I don't think it's good > idea because "i" and "t" are too big. > > So, my question is that > > Is there any better idea to avoid "for" statement for this problem?Hi. The recurrence does not use index i. So, it is possible to vectorize over i. Try the following. m <- 10 # bound on i n <- 6 # bound on tt W <- matrix(runif(m*n, max=2), nrow=m, ncol=n) X <- matrix(nrow=m, ncol=n) X[, 1] <- 1 + 5*W[, 1] for (tt in seq.int(from=2, length=n-1)) { X[, tt] <- 0.1*(tt-1) + 2*X[, tt-1] + W[, tt] } The code uses tt instead of t to avoid confusion with the transpose function. If n is always at least 2, then seq.int(from=2, length=n-1) may be replaced by 2:n. Hope this helps. Petr Savicky.
On 26/06/12 00:39, Kathie wrote:> Dear R users, > > I'd like to compute X like below. > > X_{i,t} = 0.1*t + 2*X_{i,t-1} + W_{i,t} > > where W_{i,t} are from Uniform(0,2) and X_{i,0} = 1+5*W_{i,0} > > Of course, I can do this with "for" statement, but I don't think it's good > idea because "i" and "t" are too big. > > So, my question is that > > Is there any better idea to avoid "for" statement for this problem? > > Thank you in advance.You could use "filter()", as follows: foo <- function (n) { w0 <- 1+5*runif(1,0,2) w <- c(w0,runif(n-1,0,2)) y <- filter(w,filter=2,method="recursive") mu <- sapply(0:(n-1),function(k){if(k==0) return(0);sum((1:k)*2^((k-1):0))*0.1}) mu + y } (This of course just does univariate time series and ignores the subscript i. To get multivariate time series, just use "foo()" to generate as many components as you want; they are independent.) Check: set.seed(42) x <- foo(10) x Time Series: Start = 1 End = 10 Frequency = 1 [1] 10.14806 22.27027 45.31282 92.58654 186.85657 375.25133 [7] 752.57585 1506.12103 3014.35603 6031.02220 set.seed(42) w0 <- 1+5*runif(1,0,1) w <- c(w0,runif(9,0,2) 0.1+2*x[1]+w[2] [1] 22.27027 # Bingo. 0.2+2*x[2]+w[3] [1] 45.31282 # Bingo. etc. Notice that the generated series explodes rather rapidly. cheers, Rolf Turner