rtime-seriesautocorrelation

How to use "a set of lags (time differences) to retain" in acf() routing in R


There is a couple of lines in R documentation of acf function.

i a set of lags (time differences) to retain.

j a set of series (names or numbers) to retain.

What do they mean and how to use them? I see no relevant example in docs.

(I though that it should be as simple as acf(time_series, i=c(1,2,4,7)) but it throws warning messages and doesn't affect the output.)

example:

time_series = rnorm(100)
acf(time_series, i=c(1,2,4,7))

# There were 12 warnings (...)
# In plot.window(...) : "i" is not a graphical parameter
# ... 

Solution

  • There are 3 methods suggested for acf (Extract, plot and print)

    methods(class = acf)
    #[1] [     plot  print
    

    The S3 method for the Extraction ([) source code returns and it is a starred one

    grep("acf", methods("["), value = TRUE)
    #[1] "[.acf"
    
    getAnywhere('[.acf')
    function (x, i, j) 
    {
        if (missing(j)) 
            j <- seq_len(ncol(x$lag))
        ii <- if (missing(i)) 
            seq_len(nrow(x$lag))
        else match(i, x$lag[, 1, 1], nomatch = NA_integer_)
        x$acf <- x$acf[ii, j, j, drop = FALSE]
        x$lag <- x$lag[ii, j, j, drop = FALSE]
        x
    }
    

    Thus, the i, and j is based on the Extraction