1)线性回归假设
- 输入数据框
dd 在最后的注释中重复显示(4 个点还不够,但我们使用现有的)
- 按照问题中的说明使用线性回归
- 问题中的y、yc、yt和e分别是产量(产量)、平均产量、趋势效应和年产量线性回归的残差
我们使用lm 运行回归,然后使用proj 进行分解。没有使用任何包。
fm <- lm(production ~ year, dd)
p <- proj(fm)
# check that components sum to yield
all.equal(dd$production, rowSums(p), check.attributes = FALSE)
## [1] TRUE
tt <- ts(cbind(dd$production, p), start = dd$year[1])
colnames(tt) <- c("y", "yc", "yt", "e")
tt
## Time Series:
## Start = 1979
## End = 1982
## Frequency = 1
## y yc yt e
## 1979 1061 1460.5 -23.7 -375.8
## 1980 1900 1460.5 -7.9 447.4
## 1981 1701 1460.5 7.9 232.6
## 1982 1180 1460.5 23.7 -304.2
# plot
plot(tt, main = "yield and components")
(续图)
2) HP 过滤器 另一种方法是将 yc 定义为上述平均产量,但使用 Hodrick 和 Prescott 过滤器输出来定义趋势。
(还有其他可能性,例如对线性回归的残差运行 HP 过滤器,然后将 HP 趋势定义为 yc,给出四个分量:均值、yt、yc 和 e,或者可能将均值与其他组件;但是,在没有对实际需要的具体定义的情况下,我们不会追求许多可能性。)
library(mFilter)
y <- with(dd, ts(production, start = year[1]))
yc <- mean(y)
yt <- hpfilter(y - yc)$trend
e <- y - yc - yt
tt2 <- cbind(y, yc, yt, e); tt2
## Time Series:
## Start = 1979
## End = 1982
## Frequency = 1
## y yc yt e
## 1979 1061 1460.5 -50.440731 -349.0593
## 1980 1900 1460.5 20.014502 419.4855
## 1981 1701 1460.5 32.293190 208.2068
## 1982 1180 1460.5 -1.866961 -278.6330
plot(tt2, main = "yield and HP components")
注意
dd <- structure(list(year = 1979:1982, production = c(1061L, 1900L,
1701L, 1180L)), class = "data.frame", row.names = c(NA, -4L))
更新
进行了一些改进并添加了第二种方法。