viking_pirate
对于你的问题,
第一个,使用的是何种方法:
The function first determines the trend component using a moving average (if filter is NULL, a symmetric window with equal weights is used), and removes it from the time series. Then, the seasonal figure is computed by averaging, for each time unit, over all periods. The seasonal figure is then centered. Finally, the error component is determined by removing trend and seasonal figure (recycled as needed) from the original time series.This only works well if x covers an integer number of complete periods. 更具体,请参照>M. Kendall and A. Stuart (1983) The Advanced Theory of Statistics, Vol.3, Griffin. pp. 410–414.
第二个,怎么查看原代码,我个人习惯,如果是下载后安装的package,请直接点开package,找到R文件夹,就可以看到。如果是internal function, 只要输入这个函数就可以,比如decompose.
function (x, type = c("additive", "multiplicative"), filter = NULL)
{
type <- match.arg(type)
l <- length(x)
f <- frequency(x)
if (f <= 1 || length(na.omit(x)) < 2 * f)
stop("time series has no or less than 2 periods")
if (is.null(filter))
filter <- if (!f%%2)
c(0.5, rep(1, f - 1), 0.5)/f
else rep(1, f)/f
trend <- filter(x, filter)
season <- if (type == "additive")
x - trend
else x/trend
periods <- l%/%f
index <- seq.int(1L, l, by = f) - 1L
figure <- numeric(f)
for (i in 1L:f) figure <- mean(season[index + i], na.rm = TRUE)
figure <- if (type == "additive")
figure - mean(figure)
else figure/mean(figure)
seasonal <- ts(rep(figure, periods + 1)[seq_len(l)], start = start(x),
frequency = f)
structure(list(x = x, seasonal = seasonal, trend = trend,
random = if (type == "additive") x - seasonal - trend else x/seasonal/trend,
figure = figure, type = type), class = "decomposed.ts")
}