我正在尝试回答
的问题系列包含1991年2月2日至2017年1月20日美国成品车用汽油产品供应量的每周数据。单位为“百万桶/天”。仅考虑截至 2004 年底的数据。us_gasoline
a.将调和回归与数据趋势进行拟合。尝试改变傅里叶项的数量。绘制观察到的汽油值和拟合值并评论您所看到的内容。
我的代码如下:
autoplot(gas2004, ylab = "Gas Supply (Weekly)")
fourier.gas1 <- tslm(gas2004 ~ trend + fourier(gas2004, K=7))
fourier.gas2 <- tslm(gas2004 ~ trend + fourier(gas2004, K=12))
fourier.gas3 <- tslm(gas2004 ~ trend + fourier(gas2004, K=20))
autoplot(gas2004, ylab = "Gas Supply (Weekly)",main= "Fourier Transformation") +
autolayer(fitted(fourier.gas1))+
autolayer(fitted(fourier.gas2))+
autolayer(fitted(fourier.gas3))
I am getting the following error from the first line of the code
> Error in `attr(x, "tsp") <- c(1, NROW(x), 1)`:
> ! invalid time series parameters specified
> Backtrace:
> 1. stats::window(us_gasoline, end = 2004)
> 2. stats:::window.default(us_gasoline, end = 2004)
> 3. stats::hasTsp(x)
您似乎正在尝试 https://otexts.com/fpp3/regression-exercises.html 中的练习 5。所以我认为这是家庭作业。
本书使用
fpp3
包,其中包含使用 fable
安装的 tsibble 对象和模型。然而您正在尝试使用为 ts
对象设计的函数。
按照 https://otexts.com/fpp3/useful-predictors.html#fourier-series 中的示例,了解如何将这些模型与 tsibble 对象相匹配。