简体   繁体   中英

Computing correlation coefficients for an AR(2) process in R

I have a time series problem that I could easily work out manually, only it would take kind of a long time since I have 4 different AR(2) processes and want to calculate at least 20 lags for each.

What I want to do is use the Yule Walker equation for rho as follows:

I have an auto regressive process of second order, AR(2) . Phi(1) is 0.6 and Phi(2) is 0.4.

I want to calculate the correlation coefficients rho(k) for all lags up to k = 20 .

So rho(0) would naturally be 1 and rho(-1) = rho(1) . Therefore

rho(1) = phi(1) + phi(2)*rho(1)
rho(k) = phi(1)*rho(k-1) + phi(2)*rho(k-2)

Now I want to solve this in R, but I have no idea how to start, can anyone help me out here?

You can try my program in R languages,

In R Script:

AR2 <- function(Zt,tetha0,phi1,phi2,nlag)
{
 n <- length(Zt)
 Zbar <- mean(Zt)
 Zt1 <- rep(Zbar,n)
 for(i in 2:n){Zt1[i] <- Zt[i-1]}
 Zt2 <- rep(Zbar,n)
 for(i in 3:n){Zt1[i] <- Zt[i-2]}
 Zhat <- tetha0+phi1*Zt1+phi2*Zt2
 error <- Zt-Zhat
 ACF(error,nlag)
}

ACF <- function(error,nlag)
{
 n <- length(error)
 rho <- rep(0,nlag)
 for(k in 1:nlag)
 {
  a <- 0
  b <- 0
  for(t in 1:(n-k)){a <- a+(error[t]*error[t+k])}
  for(t in 1:n){b <- b+(error[t]^2)}
  rho[k] <- a/b
 }  
 return(rho)
}

In R console:

Let you have a Zt series, tetha(0) = 0, phi(1) = 0.6, phi(2) = 0.4, and number of lag = 20

AR2(Zt,0,0.6,0.4,20)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM