问题
I have an XTS time series object which shows a value on the first of each month (representing an aggregated sum for the whole month) during four years.
When I run the stats::acf()
function on it, I get a plot with lag (x axis) units in the hundreds of thousands. How can that be if I only have 48 values in my time series? If it is a time unit, then which one, and how can I change it?
Example code:
library(dplyr)
library(lubridate)
library(xts)
set.seed(100)
test <- data.frame(y = c(rep(2012, 12), rep(2013, 12), rep(2014, 12), rep(2015, 12)),
m = rep(seq(1, 12, 1), 4), d = rep(1, 48), value = runif(48, 0, 100))
test <- test %>%
mutate(date = ymd(paste(y, m, d, sep = "-"))) %>%
select(date, value)
test <- xts(test$value, test$date)
acf(test)
回答1:
From the source code we see that we can calculate the lags like this:
sampleT <- as.integer(nrow(test))
nser <- as.integer(ncol(test))
lag.max <- floor(10 * (log10(sampleT) - log10(nser)))
x.freq <- frequency(test)
lag <- outer(0:lag.max, 1/x.freq)
# [,1]
# [1,] 0
# [2,] 86400
# [3,] 172800
# [4,] 259200
# [5,] 345600
# [6,] 432000
# [7,] 518400
# [8,] 604800
# [9,] 691200
#[10,] 777600
#[11,] 864000
#[12,] 950400
#[13,] 1036800
#[14,] 1123200
#[15,] 1209600
#[16,] 1296000
#[17,] 1382400
The time unit is the reciprocal of the frequency unit. To understand how that value is calculated you need to dive into the source code of frequency.zoo
, which does something I find difficult to understand at a first glance.
来源:https://stackoverflow.com/questions/35705001/r-time-series-whats-the-lag-unit-for-autocorrelation-function-acf