<br />
decompose <- function (x, type = c("additive", "multiplicative"), filter = NULL) <br />
{<br />
type <- match.arg(type)<br />
l <- length(x)<br />
f <- frequency(x)<br />
if (f <= 1 || length(na.omit(x)) < 3 * f) <br />
stop("time series has no or less than 3 periods")<br />
if (is.null(filter)) <br />
filter <- if (!f%%2) <br />
c(0.5, rep(1, f - 1), 0.5)/f<br />
else rep(1, f)/f<br />
trend <- filter(x, filter)<br />
season <- if (type == "additive") x - trend else x/trend<br />
season <- window(season, start(x) + c(1, 0), c(end(x)[1] - 1, f))<br />
periods <- l%/%f<br />
index <- c(0, cumsum(rep(f, periods - 3)))<br />
figure <- numeric(f)<br />
for (i in 1:f) figure[i] <- mean(season[index + i])<br />
figure <- if (type == "additive") figure - mean(figure) else figure/mean(figure)<br />
seasonal <- ts(rep(figure, periods), start = start(x), frequency = f)<br />
structure(list(seasonal = seasonal, trend = trend, <br />
random = x - if (type == "additive") seasonal + trend else seasonal * trend, <br />
figure = figure, type = type), class = "decomposed.ts")<br />
}<br />
粗斜体部分,为什么采用这种处理,如果说是怕最前和最后两个周期内数据有不齐的,那么周期也应该从1开始,到k-1结束阿,为什么感觉是从0开始,到k-2结束。还是有什么特别的含义?