Ashwini Patil
2016-Oct-08 20:42 UTC
[R] simulate AR1 process with uniform distribution and different y0 values
Hello I need to plot an ar1 graph for process yk=0.75y (k-1) + ek, for y0=1 and another graph for y0=10. assume ek is uniformly distributed on interval [-0.5,0.5]. i have the following code but i am not sure how to control y0. #----------#Start#---------# rm(list=ls()) library(tseries) #library(zoo) set.seed(0) y<-arima.sim(model=list(ar=.75), n=100, innov = runif(100, 0, 1)) y.1<-y-0.5 ts.plot(y.1) [[alternative HTML version deleted]]
Rolf Turner
2016-Oct-08 23:44 UTC
[R] [FORGED] simulate AR1 process with uniform distribution and different y0 values
On 09/10/16 09:42, Ashwini Patil wrote:> Hello > > I need to plot an ar1 graph for process yk=0.75y (k-1) + ek, for y0=1 and > another graph for y0=10. > > assume ek is uniformly distributed on interval [-0.5,0.5]. > > i have the following code but i am not sure how to control y0. > > #----------#Start#---------# > rm(list=ls()) > library(tseries) > #library(zoo) > set.seed(0) > y<-arima.sim(model=list(ar=.75), n=100, innov = runif(100, 0, 1)) > y.1<-y-0.5 > ts.plot(y.1)I don't understand the last three lines; shouldn't "innov" be equal to runif(100, -0.5, 0.5), and subtracting 0.5 be skipped? I think that the following does what you want: set.seed(42) y<- arima.sim(model=list(ar=0.75),n.start=1,start.innov=10,n=100, innov=runif(100,-0.5,0.5)) Or you could simply do: set.seed(42) e <- runif(100,-0.5,0.5) yy <- numeric(101) yy[1] <- 10 for(i in 1:100) yy[i+1] <- 0.75*yy[i] + e[i] yy <- as.ts(yy[-1]) Same-same. (Or you could apply filter() to c(10,e) with method="recursive".) cheers, Rolf Turner P.S. I am not convinced that what you want to do makes much sense, but the foregoing shows how to do it if you must. R. T. -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276