问题描述:

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
```

您可能感兴趣的文章：

- php class function -> call the function according to a passing parameter
- networking - Sending WCF messages being delayed under load
- How do I url unencode in Python?
- c# - Absolute URLs not working with Virtual Directories?
- sql server 2008 - Returning table-type by function
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**