当前位置: 动力学知识库 > 问答 > 编程问答 >

Time series decomposition in R

问题描述:

I've recently started dabbling with some time-series analysis in R, and I'm a little confused as to how the filter function in the stats library works. Essentially, I've seen it asserted that, for a daily time series, that filter could be used to decompose the series into yearly, seasonal and weekly components using something like the following:

x.year <- filter(x, rep(1/365, 365))

x.season <- filter(x, rep(1/90, 90))

x.weekly <- filter(x, rep(1/7, 7))

While I can figure out that the rep(1/period, period) gives you a component of length period, I'm not sure why, and am trying to avoid cargo cult analysis. Consulting the documentation, that bit is apparently, "a vector of filter coefficients in reverse time order" - just not sure what that means.

Anyone care to point me in the right direction?

网友答案:

I would suggest you first look at what a Convolution is. When you understand it well, you should easily see that using filter to compute the convolution of your signal x with rep(1/period, period) is nothing more than computing the "moving average" or "rolling mean" of your signal, see for yourself:

x <- runif(10)
filter(x, rep(1/5, 5))
# Time Series:
# Start = 1 
# End = 10 
# Frequency = 1 
#  [1]        NA        NA 0.4400744 0.3643682 0.2677056 0.3703566 0.3449967
#  [8] 0.4975061        NA        NA

library(zoo)
rollmean(x, 5, na.pad = TRUE)
#  [1]        NA        NA 0.4400744 0.3643682 0.2677056 0.3703566 0.3449967
#  [8] 0.4975061        NA        NA
分享给朋友:
您可能感兴趣的文章:
随机阅读: