[R] R help: Correlograms

Pfaff, Bernhard Bernhard.Pfaff at drkw.com
Fri Jun 6 17:15:59 CEST 2003



Hello,

 
I have time series and need to draw simple and partial correlograms with
associated Q-statistics (the same as in EViews). Can I do it in R? Thanks


Hello Shutnik,

see ?Box.test in the ts-package for Q-statistics.

To my knowledge a plot function as in EViews (left-hand panel are ACFs and
PACFs as bars and right-hand panel are acfs, pacfs as values with Q-stats
and p-values) is not existent, hence you have to set up the plot by
yourself. In order, to do so, may be the following function might be of use
to you ("Ljung-Box"):

qstat <- function(x, order=12)
{
  if (order >= (length(x)-1))
    {
      print("Vector length falls short of autocorrelations to compute.")
    }
  else
    {
      x <- as.ts(na.omit(x))
      Total <- length(x)
      factor <- Total*(Total+2)
      xacf <- acf(x, lag=(order), plot=FALSE)
      denom <- rep(Total, order)-seq(1, order)
      Lag <- seq(1, order)
      tausq <- xacf$acf[2:length(xacf$acf)]^2/denom
      Q <- 0[1:order]
      temp <- 0[1:order]
      for(i in 1:order)
        {
          temp[i] <- sum(tausq[1:i])
        }
      Qstat <- factor*temp
      pval <- 1 - pchisq(Qstat,i)
      Ljung.Box <- cbind(Lag, Qstat, pval)
      Ljung.Box
    }
}
#
y <- 1:100
qstat(y, order=10)


HTH,
Bernhard



----------------------------------------------------------------------
If you have received this e-mail in error or wish to read our e-mail 
disclaimer statement and monitoring policy, please refer to 
http://www.drkw.com/disc/email/ or contact the sender.




More information about the R-help mailing list