[R] acf.plot() question
DeBarr, Dave
debarr at mitre.org
Mon Oct 10 03:43:40 CEST 2005
When I run the "acf()" function using the "acf(ts.union(mdeaths,
fdeaths))" example, the "acf()" function calls the "acf.plot()"
function to generate this plot...
http://members.cox.net/ddebarr/images/acf_example.png
The plot in the lower right-hand corner is labeled "fdeaths & mdeaths",
but the negative lags appear to belong to "mdeaths & fdeaths" [which
correspond to the positive lags of "fdeaths & mdeaths"].
Am I missing something, or should the plot in the lower right-hand
corner be labeled "mdeaths & fdeaths" (instead of "fdeaths & mdeaths")?
Note: The unit of measure for the lags is years.
> autocorrelation <- function(x, y, lags) {
+ n <- length(x)
+ x.bar <- mean(x)
+ y.bar <- mean(y)
+ c <- array(0, length(lags))
+ i <- 1
+ for (t in lags) {
+ s <- seq(max(1, 1 - t), min(n - t, n))
+ c[i] <- sum((x[s + t] - x.bar) * (y[s] - y.bar)) / n
+ i <- i + 1
+ }
+ x.sd <- sqrt(sum((x - x.bar) ^ 2) / n)
+ y.sd <- sqrt(sum((y - y.bar) ^ 2) / n)
+ return(c / (x.sd * y.sd))
+ }
> autocorrelation(mdeaths, fdeaths, -15:15)
[1] 0.015054983 0.365626026 0.615427121 0.708206289 0.621895801
[6] 0.340005447 -0.024534195 -0.381671430 -0.611793479 -0.677803477
[11] -0.604031174 -0.349468396 0.019759425 0.405200639 0.744309322
[16] 0.976241251 0.735668532 0.364241839 -0.010675725 -0.382920620
[21] -0.622386979 -0.688538519 -0.610583980 -0.383338305 -0.018112073
[26] 0.391983088 0.656592111 0.721397236 0.639104375 0.361352626
[31] -0.003385423
> autocorrelation(fdeaths, mdeaths, -15:15)
[1] -0.003385423 0.361352626 0.639104375 0.721397236 0.656592111
[6] 0.391983088 -0.018112073 -0.383338305 -0.610583980 -0.688538519
[11] -0.622386979 -0.382920620 -0.010675725 0.364241839 0.735668532
[16] 0.976241251 0.744309322 0.405200639 0.019759425 -0.349468396
[21] -0.604031174 -0.677803477 -0.611793479 -0.381671430 -0.024534195
[26] 0.340005447 0.621895801 0.708206289 0.615427121 0.365626026
[31] 0.015054983
More information about the R-help
mailing list