计算(和可视化)Arima(1,1,1)模型的AR和MA贡献

时间:2018-05-24 18:07:12

标签: r statistics forecasting arima

我想正确地想象和理解Arima(1,1,1)模型的组件。

我如何能够量化AR和MA-Term为系列序列的每个拟合值提供的贡献?

我认为我已经基本上能够为(1,0,1)模型做到这一点,请参阅下面的示例


library(forecast)
library(tidyverse)
library(ggfortify)

arima_101 <- Arima(AirPassengers, c(1, 0, 1), include.mean = F)  # only works without mean
autoplot(arima_101)

# get coefficients
ar1_coef <- coef(arima_101)["ar1"]
ma1_coef <- coef(arima_101)["ma1"]

# try to compute contributions by components 
ar1_part <- AirPassengers %>% 
  as.numeric() %>% 
  dplyr::lag(1) %>% 
  `*`(ar1_coef)

ma1_part <- resid(arima_101) %>% 
  as.numeric() %>% 
  dplyr::lag(1) %>% 
  `*`(ma1_coef)

fitted_values <- fitted(arima_101) %>% as.numeric()


# inspect results
df <- tibble(idx = 1:144, ar1_part, ma1_part, fitted_values) %>% 
  mutate(sum_ar1_ma1 = ar1_part + ma1_part)

df %>% 
  gather("component", "contribution", -idx, -fitted_values, -sum_ar1_ma1) %>% 
  #  %>% 
  ggplot(aes(idx, contribution)) +
  geom_area(aes(fill = component)) +
  geom_line(aes(idx, value, linetype = result),
            alpha = 0.3,
            data = gather(df, "result", "value", -idx, -ar1_part, -ma1_part))
#> Warning: Removed 2 rows containing missing values (position_stack).
#> Warning: Removed 1 rows containing missing values (geom_path).

fitted_values == ar1_part + ma1_part
#>   [1]    NA FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
#>  [23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [56]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [67]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [78]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#>  [89]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [100]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [111]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [122]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [133]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
#> [144]  TRUE

1 个答案:

答案 0 :(得分:1)

到目前为止,这是我对arima(1,1,1)的最佳拍摄


# ARIMA 111
library(forecast)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
#> ✔ tibble  1.4.2     ✔ dplyr   0.7.4
#> ✔ tidyr   0.8.0     ✔ stringr 1.3.1
#> ✔ readr   1.1.1     ✔ forcats 0.2.0
#> ── Conflicts ────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
library(ggfortify)

arima_111 <- Arima(AirPassengers, c(1, 1, 1))  # possibly dissallow the mean
autoplot(arima_111)

diff_airpass <- diff(AirPassengers)
diff_arima101 <- Arima(diff_airpass, c(1,0,1), include.mean = F)

diff_baseline <- as.numeric(AirPassengers) - c(NA, diff_airpass)

autoplot(diff_arima101)

# get coefficients
ar1_coef <- coef(arima_111)["ar1"]
ma1_coef <- coef(arima_111)["ma1"]

# airpass_num <- as.numeric()
# plot(airpass_num, type = "b")

# try to compute contributions by components 
ar1_part <- diff_airpass %>% 
  # diff() %>% 
  as.numeric() %>% 
  dplyr::lag(1) %>% 
  `*`(ar1_coef) %>% 
  c(NA, .)  # add na for first value

ma1_part <- resid(diff_arima101) %>% 
  as.numeric() %>% 
  dplyr::lag(1) %>% 
  `*`(ma1_coef) %>% 
  c(NA, .)

fitted_values <- fitted(diff_arima101) %>%
  as.numeric() %>% 
  c(NA, .)


# inspect results
df <- tibble(idx = 1:144, ar1_part, ma1_part, z_baseline = diff_baseline, fitted_values) %>% 
  mutate(sum_ar1_ma1 = ar1_part + ma1_part)

df %>% 
  gather("component", "contribution", -idx, -fitted_values, -sum_ar1_ma1) %>% 
  ggplot(aes(idx, contribution)) +
  geom_area(aes(fill = component)) 
#> Warning: Removed 5 rows containing missing values (position_stack).