# [R] acf and ccf

arun smartpink111 at yahoo.com
Tue Jul 30 23:15:15 CEST 2013

```I use R 3.0.1.

#I do have that line after  ` lag[lower.tri(lag)] <- -1`

acf <- .Call(C_acf, x, lag.max, type == "correlation")
lag <- outer(0:lag.max, lag/x.freq)
acf.out <- structure(list(acf = acf, type = type, n.used = sampleT,
lag = lag, series = series, snames = colnames(x)), class = "acf")

http://stackoverflow.com/questions/14035506/how-to-see-the-source-code-of-r-internal-or-primitive-function

A.K.

________________________________
From: Cathy Lee Gierke <leegi001 at umn.edu>
To: arun <smartpink111 at yahoo.com>
Sent: Tuesday, July 30, 2013 5:04 PM
Subject: Re: [R] acf and ccf

You must have a different version of R?  But you do have a line:      acf <- .Call(C_acf, x, lag.max, type == "correlation")
That is quite similar.  It is a call to a function that does the actual math.

Here is what I get:

function (x, lag.max = NULL, type = c("correlation", "covariance",
"partial"), plot = TRUE, na.action = na.fail, demean = TRUE,
...)
{
type <- match.arg(type)
if (type == "partial") {
m <- match.call()
m[[1L]] <- as.name("pacf")
m\$type <- NULL
return(eval(m, parent.frame()))
}
series <- deparse(substitute(x))
x <- na.action(as.ts(x))
x.freq <- frequency(x)
x <- as.matrix(x)
if (!is.numeric(x))
stop("'x' must be numeric")
sampleT <- as.integer(nrow(x))
nser <- as.integer(ncol(x))
if (is.na(sampleT) || is.na(nser))
stop("sampleT and nser must be ints", domain = NA)
if (is.null(lag.max))
lag.max <- floor(10 * (log10(sampleT) - log10(nser)))
lag.max <- as.integer(min(lag.max, sampleT - 1L))
if (is.na(lag.max) || lag.max < 0)
stop("'lag.max' must be at least 0")
if (demean)
x <- sweep(x, 2, colMeans(x, na.rm = TRUE), check.margin = FALSE)
lag <- matrix(1, nser, nser)
lag[lower.tri(lag)] <- -1
acf <- array(.C(C_acf, as.double(x), sampleT, nser, lag.max,
as.integer(type == "correlation"), acf = double((lag.max +
1L) * nser * nser), NAOK = TRUE)\$acf, c(lag.max +
1L, nser, nser))
lag <- outer(0:lag.max, lag/x.freq)
acf.out <- structure(.Data = list(acf = acf, type = type,
n.used = sampleT, lag = lag, series = series, snames = colnames(x)),
class = "acf")
if (plot) {
plot.acf(acf.out, ...)
return(invisible(acf.out))
}
else return(acf.out)
}
<bytecode: 0x7f8e04f34ea8>
<environment: namespace:stats>

Cathy Lee Gierke

On Tue, Jul 30, 2013 at 3:57 PM, arun <smartpink111 at yahoo.com> wrote:

Hi,
>When I type 'acf', I get this:
>
>
>function (x, lag.max = NULL, type = c("correlation", "covariance",
>    "partial"), plot = TRUE, na.action = na.fail, demean = TRUE,
>    ...)
>{
>    type <- match.arg(type)
>    if (type == "partial") {
>        m <- match.call()
>        m[[1L]] <- as.name("pacf")
>        m\$type <- NULL
>        return(eval(m, parent.frame()))
>    }
>    series <- deparse(substitute(x))
>    x <- na.action(as.ts(x))
>    x.freq <- frequency(x)
>    x <- as.matrix(x)
>    if (!is.numeric(x))
>        stop("'x' must be numeric")
>    sampleT <- as.integer(nrow(x))
>    nser <- as.integer(ncol(x))
>    if (is.na(sampleT) || is.na(nser))
>        stop("'sampleT' and 'nser' must be integer")
>    if (is.null(lag.max))
>        lag.max <- floor(10 * (log10(sampleT) - log10(nser)))
>    lag.max <- as.integer(min(lag.max, sampleT - 1L))
>    if (is.na(lag.max) || lag.max < 0)
>        stop("'lag.max' must be at least 0")
>    if (demean)
>        x <- sweep(x, 2, colMeans(x, na.rm = TRUE), check.margin = FALSE)
>    lag <- matrix(1, nser, nser)
>    lag[lower.tri(lag)] <- -1
>    acf <- .Call(C_acf, x, lag.max, type == "correlation")
>    lag <- outer(0:lag.max, lag/x.freq)
>    acf.out <- structure(list(acf = acf, type = type, n.used = sampleT,
>        lag = lag, series = series, snames = colnames(x)), class = "acf")
>    if (plot) {
>        plot.acf(acf.out, ...)
>        invisible(acf.out)
>    }
>    else acf.out
>}
>
>
>
>I couldn't get:
>
>array(.C(C_acf, as.double(x), sampleT, nser, lag.max,
>        as.integer(type == "correlation"), acf = double((lag.max +
>            1L) * nser * nser), NAOK = TRUE)\$acf, c(lag.max +
>        1L, nser, nser))
>
>
>________________________________
>
>From: Cathy Lee Gierke <leegi001 at umn.edu>
>To: arun <smartpink111 at yahoo.com>
>Sent: Tuesday, July 30, 2013 4:49 PM
>
>Subject: Re: [R] acf and ccf
>
>
>
>
>Thank you.  Yes, I supposed that does give you the function.  However, most of the meaningful code is inside another compiled function:
>
> acf <- array(.C(C_acf, as.double(x), sampleT, nser, lag.max,
>        as.integer(type == "correlation"), acf = double((lag.max +
>            1L) * nser * nser), NAOK = TRUE)\$acf, c(lag.max +
>        1L, nser, nser))
>
>Any idea how to see that?
>
>
>Cathy Lee Gierke
>
>
>On Tue, Jul 30, 2013 at 3:28 PM, arun <smartpink111 at yahoo.com> wrote:
>
>Just type
>>acf
>>ccf
>>on R prompt
>>A.K.
>>
>>
>>
>>
>>
>>
>>----- Original Message -----
>>From: Cathy Lee Gierke <leegi001 at umn.edu>
>>To: r-help at r-project.org; r-core-owner at r-project.org
>>Cc:
>>Sent: Tuesday, July 30, 2013 4:02 PM
>>Subject: [R] acf and ccf
>>
>>Greetings,
>>
>>Is it possible to see the source code for the acf and ccf functions?  I
>>want to understand the exact formula used.
>>
>>It may be in this book, but I am hoping I can find it elsewhere, as the
>>book is quite expensive.
>>Venables, W. N. and Ripley, B. D. (2002) *Modern Applied Statistics with S*.
>>
>>Fourth Edition. Springer-Verlag.
>>
>>
>>Sincere thanks,
>>Cathy Lee Gierke
>>
>>    [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help