5
Consider the stochastic process AR (1). Generate a sequence of 500 random variables and assume c = 3 and Phi = {0.5, 0.95, 1}. Make autocorrelations from 1st to 3rd order (CR). How to generate these correlations in R?
T=500
e=rnorm(T)
phi1=.5
phi2=.9
phi3=1
c=3
y1=matrix(0,T,1)
y1[1]=e[1]
for(i in 2:T){y1[i]=c+phi1*y1[i-1]+e[i]}
y2=matrix(0,T,1)
y2[1]=e[1]
for(i in 2:T){y2[i]=c+phi2*y2[i-1]+e[i]}
y3=matrix(0,T,1)
y3[1]=e[1]
for(i in 2:T){y3[i]=c+phi3*y3[i-1]+e[i]}
y1[i-1]=lag(y1[i],-1)
y2[i-1]=lag(y2[i],-1)
y3[i-1]=lag(y3[i],-1)
y88=cbind(y1[i],y2[i],y3[i])
y88
lm(y88[,1]~y88[,2])
cor(y1[i],y1[i-1])
When I do this the correction shows NA result. Someone would have some hint?
I know it’s not the focus, but you can use the function
filter
to generate the AR processes (among others). I made a summary of this function in this link.– Wilson Freitas