0

我有一个落后的行业。我相信可以预测 y 的变量 x。

首先,我安装了一个 tslm 模型,发现残差可以建模为一个 arima 过程。-> 所以我将残差建模为 ARIMAX (1,1,0)。我的麻烦是理解为什么预测(适合验证数据)不像 arima 模型。AR(1) 过程正在对拟合值进行处理。但它看起来非常接近验证数据中的简单 tslm-model。

是因为 AR 过程是从 y_fc_1 处的第一个计算值推断出来的,并且该值乘以 AR 系数吗?而不是像拟合线上那样提供新鲜的 y_t-1 数据?

下面的例子和情节!

library(tsibble)
library(fable)
library(tidyverse)
library(gridExtra)

# Example dataset. I transformed x to x_lag here, instead of in the model, for transparency.
tibble <- tibble(
  index = 1:46,
  x_lag = c(NA, 9, 32, 43, 46, 50, 48, 51, 46, 44, 40, 42, 40, 38, 
               38, 43, 50 ,49, 42, 39, 36, 32, 33, 34, 34, 33, 36, 39, 
               38, 34, 36, 39, 43, 47, 53, 69, 87, 100, 93, 67, 71, 74,
               92, 79, 73, 73),
  y = c(99, 101, 89, 76.1, 78, 75.9, 77.7, 77.1, 86.7, 80.7, 85, 82.7, 
        88.4, 98.7, 95, 99.9, 88.8, 101, 102, 102, 102, 106, 106, 100,
        97.3, 96, 99.4, 95.3, 92.4, 93.6, 95.7, 93.7, 90.6, 89.4, 92,
        92.7, 81.4, 77, 74.4, 83.1, 79.7, 77.4, 79.6, 67.1, 66.1, 67.1)
)

# Nr of validation weeks to test the model with
val_weeks <- 15

# Classify for training and validation and convert to Tsibble for Fable
tibble <- tibble %>%
  mutate(type = if_else(index > max(index) - val_weeks,
                        "validation", "training")) %>%
  as_tsibble(., index = index)

# Save the training data
tibble_train <- tibble %>%
  filter(type == "training")

# Save the validation data
tibble_val <- tibble %>%
  filter(type == "validation")

# Fit the 2 models, and forecast.
fit_tslm <- tibble_train %>%
  model(TSLM(y ~ x_lag))
fc_tslm <- fit_tslm %>%
  forecast(new_data = tibble_val)

fit_arimax <- tibble_train %>%
  model(ARIMA(y ~ x_lag + pdq(1, 1, 0)))
fc_arimax <- fit_arimax %>%
  forecast(new_data = tibble_val)

tslm_plot <- 
  tibble %>%
  ggplot(aes(x = index, y = y)) +
  autolayer(fc_tslm, alpha = 0.2) +
  geom_line(aes(color = type), alpha = 0.8) +
  geom_line(aes(y = .fitted, color = "Fitted"), data = augment(fit_tslm))

arimax_plot <- 
  tibble %>%
  ggplot(aes(x = index, y = y)) +
  autolayer(fc_arimax, alpha = 0.2) + 
  geom_line(aes(color = type), alpha = 0.8) +
  geom_line(aes(y = .fitted, color = "Fitted"), data = augment(fit_arimax))

grid.arrange(tslm_plot, arimax_plot, nrow = 2)

tslm vs arimax

4

0 回答 0