248
248
#| par: true
249
249
250
250
air_passengers_df <- data.frame(y = as.vector(AirPassengers), t = 1:144)
251
- fit_lm1 <- lm(y ~ t + sin(t / 12 * 2 * pi) + cos(t / 12 * 2 * pi), data = air_passengers_df)
251
+ fit_lm1 <- lm(log(y) ~ t + sin(t / 12 * 2 * pi) + cos(t / 12 * 2 * pi), data = air_passengers_df)
252
252
fit_lm2 <- update(fit_lm1, . ~ . +
253
253
sin(t / 12 * 2 * 2 * pi) + cos(t / 12 * 2 * 2 * pi), data = air_passengers_df
254
254
)
255
255
fit_lm3 <- update(fit_lm2, . ~ . +
256
256
sin(t / 12 * 3 * 2 * pi) + cos(t / 12 * 3 * 2 * pi), data = air_passengers_df
257
257
)
258
258
plot(y ~ t, air_passengers_df, type = "l")
259
- lines(x = air_passengers_df$t, y = fit_lm1$fitted.values, col = "red")
260
- lines(x = air_passengers_df$t, y = fit_lm2$fitted.values, col = "green")
261
- lines(x = air_passengers_df$t, y = fit_lm3$fitted.values, col = "orange")
259
+ lines(x = air_passengers_df$t, y = exp( fit_lm1$fitted.values) , col = "red")
260
+ lines(x = air_passengers_df$t, y = exp( fit_lm2$fitted.values) , col = "green")
261
+ lines(x = air_passengers_df$t, y = exp( fit_lm3$fitted.values) , col = "orange")
262
262
```
263
263
264
264
模型 1 已经很好地捕捉到趋势和周期信息,当添加小周期后,略有改善,继续添加更多的小周期,不再有明显改善。实际上,小周期对应的回归系数也将不再显著。所以,这类模型的优化空间见顶了,需要进一步观察和利用残差的规律,使用更加复杂的模型。
@@ -267,10 +267,6 @@ lines(x = air_passengers_df$t, y = fit_lm3$fitted.values, col = "orange")
267
267
268
268
非线性趋势、多季节性(多个周期混合)、特殊节假日、突发热点事件、残差成分(平稳),能同时应对这五种情况的建模方法是贝叶斯可加模型和神经网络模型,比如基于 Stan 实现的 prophet 包和 tensorflow 框架。
269
269
270
- ::: callout-tip
271
- prophet 包是如何同时处理这些情况,是否可以在 cmdstanr 包中实现,是否可以在 mgcv 和 INLA 中实现?
272
- :::
273
-
274
270
``` {r}
275
271
library(cmdstanr)
276
272
```
@@ -390,7 +386,7 @@ air_passengers_tbl <- data.frame(
390
386
year = rep(1949:1960, each = 12),
391
387
month = rep(1:12, times = 12)
392
388
)
393
- mod1 <- gam(y ~ s(year) + s(month, bs = "cr", k = 12 ),
389
+ mod1 <- gam(log(y) ~ s(year) + s(month, bs = "cr"),
394
390
data = air_passengers_tbl, family = gaussian
395
391
)
396
392
summary(mod1)
@@ -420,15 +416,15 @@ plot(mod1, shade = TRUE)
420
416
#| fig-height: 4
421
417
#| par: true
422
418
423
- air_passengers_ts <- ts(mod1$fitted.values, start = c(1949, 1), frequency = 12)
419
+ air_passengers_ts <- ts(exp( mod1$fitted.values) , start = c(1949, 1), frequency = 12)
424
420
plot(AirPassengers)
425
421
lines(air_passengers_ts, col = "red")
426
422
```
427
423
428
424
整体上,乘客数逐年呈线性增长,每年不同月份呈现波动,淡季和旺季出行的流量有很大差异,近年来,这种差异的波动在扩大。为了刻画这种情况,考虑年度趋势和月度波动的交互作用。
429
425
430
426
``` {r}
431
- mod2 <- gam(y ~ s(year, month), data = air_passengers_tbl, family = gaussian)
427
+ mod2 <- gam(log(y) ~ s(year, month), data = air_passengers_tbl, family = gaussian)
432
428
summary(mod2)
433
429
```
434
430
@@ -470,7 +466,7 @@ on.exit(par(op), add = TRUE)
470
466
#| fig-height: 4
471
467
#| par: true
472
468
473
- air_passengers_ts <- ts(mod2$fitted.values, start = c(1949, 1), frequency = 12)
469
+ air_passengers_ts <- ts(exp( mod2$fitted.values) , start = c(1949, 1), frequency = 12)
474
470
plot(AirPassengers)
475
471
lines(air_passengers_ts, col = "red")
476
472
```
0 commit comments