[R] acf Significance

David Scott d.scott at auckland.ac.nz
Thu Aug 6 10:48:16 CEST 2009


Steve Jones wrote:
> Thanks for the pointer to the str function - very handy!
> 
> The output for the acf object is below - it doesn't seem to contain
> anything that might tell me the significance level.
> 
> List of 6
>  $ acf   : num [1:27, 1, 1] 1 0.6309 0.2989 0.0612 -0.2105 ...
>  $ type  : chr "correlation"
>  $ n.used: int 27
>  $ lag   : num [1:27, 1, 1] 0 1 2 3 4 5 6 7 8 9 ...
>  $ series: chr "time_series"
>  $ snames: NULL
>  - attr(*, "class")= chr "acf"
> 
> Does anyone have any more ideas?

Well, yes, but it is kind of a long story.

I assume you want the limits as are plotted by the acf function, usually 
in blue. Possibly you actually want the p-value for a given observed 
autocorrelation, but you will have to get that yourself working from my 
answer.

The short answer to your question that the confidence limits are not 
returned by the acf function, they are in fact calculated in plot.acf. 
You have to look at the actual code. The beauty of R is that you can 
look at the code and hopefully in doing so, learn more about programming 
  in R.

First up, you decide to look in the obvious place, the code of acf.
Print that out and have a look at it and you find that the plotting is 
done by plot.acf. When you try to print out plot.acf you get
 > plot.acf
Error: object "plot.acf" not found
The reason is this function is non-visible as seen by
 > methods("plot")
  [1] plot.acf*           plot.data.frame*    plot.Date*
  [4] plot.decomposed.ts* plot.default        plot.dendrogram*
  [7] plot.density        plot.ecdf           plot.factor*
[10] plot.formula*       plot.hclust*        plot.histogram*
[13] plot.HoltWinters*   plot.isoreg*        plot.lm
[16] plot.medpolish*     plot.mlm            plot.POSIXct*
[19] plot.POSIXlt*       plot.ppr*           plot.prcomp*
[22] plot.princomp*      plot.profile.nls*   plot.spec
[25] plot.spec.coherency plot.spec.phase     plot.stepfun
[28] plot.stl*           plot.table*         plot.ts
[31] plot.tskernel*      plot.TukeyHSD

    Non-visible functions are asterisked
You can get it with getAnywhere(plot.acf) however.

So look through plot.acf. You will need to look at the help for it to 
see that it allows for two different types of limit, one assuming white 
noise and one assuming an MA model. Most likely you want the white noise 
case.

The place where the confidence limit is calculated is not too hard to 
find, it is
     clim0 <- if (with.ci)
         qnorm((1 + ci)/2)/sqrt(x$n.used)
This where x is an object of class acf with structure you saw when you 
used str.

So that is it. If your acf is called x, and your confidence level is ci, 
then the limit is given by
qnorm((1 + ci)/2)/sqrt(x$n.used)

The steps needed to access R code including underlying C and Fortran are 
explained in the article by Uwe Ligges:
Uwe Ligges. R Help Desk: Accessing the sources. R News, 6(4):43-45, 
October 2006.
This article is essential reading for anyone contemplating much use of R.

David Scott
-- 
_________________________________________________________________
David Scott	Department of Statistics
		The University of Auckland, PB 92019
		Auckland 1142,    NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email:	d.scott at auckland.ac.nz,  Fax: +64 9 373 7018

Director of Consulting, Department of Statistics




More information about the R-help mailing list