NACITANIE PACKAGES

Na statisticke funkcie v R-ku

`%>%` <- magrittr::`%>%`  # pipe operator
library(ggplot2)
library(tsibble)
library(fabletools)
library(fpp3)
library(feasts)
library(fable)
library(urca)
library(forecast)
library(readxl)
library(rmarkdown)
library(knitr)

DATA

setwd("~/Desktop/Lukas")
knitr::opts_chunk$set(
  collapse = FALSE,
  out.width = '90%',
  fig.align = 'center',
  warning=F, 
  message=F
)

Cena_CR <- read_excel("Cena_CR.xlsx")

file <- "Cena_CR.xlsx"
dat <- sapply(readxl::excel_sheets(file),
              readxl::read_excel, 
              path = file, skip = 1, 
              col_names = c("čas", "cena"),
              simplify = F
) %>% 
  setNames(c("materiál", "práca")) %>%
  dplyr::bind_rows(.id = "artikel") %>% 
  dplyr::mutate(
    čas = tsibble::yearmonth(čas),
    vzorka = ifelse(čas < tsibble::yearmonth("2020-01-01"),
                    "trénovacia", "validačná")
  ) %>% 
  tsibble::as_tsibble(index = čas, 
                      key = artikel
  )
library(ggplot2)
dat %>% 
  ggplot() + aes(x = čas, y = cena, group = vzorka, color = vzorka) +
  geom_line() +
  facet_wrap(~ artikel, scales = "free_y") +
  theme(legend.position = "bottom")

Prieskumna Dekompozicia

Classical decomposition cena = tredn + seasonal + random

zložky <- dat %>% 
  dplyr::filter(vzorka == "trénovacia") %>% 
  fabletools::model(dk = feasts::classical_decomposition(cena)) %>% 
  generics::components()

zložky %>% 
  autoplot() +  theme(legend.position = "bottom")

Sezonna zlozka je velmi subtilna.

Prediktivne modely

Kontrola popisnej schopnosti

fit %>% 
  generics::glance() %>% 
  dplyr::select(artikel, .model, sigma2, BIC)
## # A tibble: 8 × 4
##   artikel  .model sigma2   BIC
##   <chr>    <chr>   <dbl> <dbl>
## 1 materiál reg    0.0740 -28.7
## 2 materiál arima  0.315   29.6
## 3 materiál ets    0.288   38.3
## 4 materiál naive  0.314   NA  
## 5 práca    reg    0.910   11.5
## 6 práca    arima  2.45    57.2
## 7 práca    ets    2.73    74.3
## 8 práca    naive  2.25    NA
fit %>% 
  fabletools::augment() %>%
  ggplot() + aes(čas) +
  geom_line(aes(y = cena)) +
  geom_line(aes(y = .fitted, color = .model)) + 
  facet_wrap(~artikel, scales = "free_y") +  
  theme(legend.position = "bottom")

Kontrola predpovednej schopnosti

Viacktokove predpovede

Toto je ta cast co zatial nefunguje

  multi <- fit

Jednokrokove predpovede

onestep <- fit %>% 
  fabletools::refit(new_data = dat, reestimate = TRUE) %>% 
  fabletools::augment() %>% 
  tsibble::filter_index("2020 jan" ~ .)

onestep %>% 
  tibble::as_tibble() %>% 
  dplyr::group_by(artikel, .model) %>% 
  dplyr::summarize(MSE = mean(.resid^2), .groups = "drop")
## # A tibble: 8 × 3
##   artikel  .model    MSE
##   <chr>    <chr>   <dbl>
## 1 materiál arima   1.35 
## 2 materiál ets     0.887
## 3 materiál naive   1.35 
## 4 materiál reg     2.21 
## 5 práca    arima   9.18 
## 6 práca    ets     6.62 
## 7 práca    naive   7.71 
## 8 práca    reg    22.2
onestep %>% 
  ggplot() + aes(čas) +
  geom_line(aes(y = cena, group = vzorka), data = dat) +
  geom_line(aes(y = .fitted, color = .model)) + 
  facet_wrap(~artikel, scales = "free_y") +  
  theme(legend.position = "bottom")

Ostra Predpoved

Exponencialne vyhladzovanie

predpovede <- dat %>% 
  fabletools::model(
    ets = fable::ETS(cena ~ trend("A") + season("A") + error("A"))
  ) %>% 
  fabletools::forecast(h = 24) 

predpovede %>% 
  autoplot() + autolayer(dat)

predpovede %>% 
  hilo() %>% 
  as.data.frame() 
##     artikel .model      čas                   cena    .mean
## 1  materiál    ets 2022 Mar N(128.2495, 0.6770903) 128.2495
## 2  materiál    ets 2022 Jun  N(130.7413, 2.264816) 130.7413
## 3  materiál    ets 2022 Sep  N(133.5342, 5.475927) 133.5342
## 4  materiál    ets 2022 Dec  N(135.6437, 10.87627) 135.6437
## 5  materiál    ets 2023 Mar  N(138.4464, 19.03217) 138.4464
## 6  materiál    ets 2023 Jun  N(140.9382, 30.50854) 140.9382
## 7  materiál    ets 2023 Sep  N(143.7311, 45.87169) 143.7311
## 8  materiál    ets 2023 Dec  N(145.8407, 65.68748) 145.8407
## 9  materiál    ets 2024 Mar  N(148.6433, 90.52257) 148.6433
## 10 materiál    ets 2024 Jun  N(151.1351, 120.9412) 151.1351
## 11 materiál    ets 2024 Sep     N(153.928, 157.51) 153.9280
## 12 materiál    ets 2024 Dec  N(156.0376, 200.7948) 156.0376
## 13 materiál    ets 2025 Mar  N(158.8402, 251.3627) 158.8402
## 14 materiál    ets 2025 Jun  N(161.3321, 309.7772) 161.3321
## 15 materiál    ets 2025 Sep  N(164.1249, 376.6052) 164.1249
## 16 materiál    ets 2025 Dec  N(166.2345, 452.4127) 166.2345
## 17 materiál    ets 2026 Mar   N(169.0371, 537.767) 169.0371
## 18 materiál    ets 2026 Jun    N(171.529, 633.231) 171.5290
## 19 materiál    ets 2026 Sep  N(174.3218, 739.3719) 174.3218
## 20 materiál    ets 2026 Dec  N(176.4314, 856.7556) 176.4314
## 21 materiál    ets 2027 Mar  N(179.2341, 985.9499) 179.2341
## 22 materiál    ets 2027 Jun  N(181.7259, 1127.517) 181.7259
## 23 materiál    ets 2027 Sep  N(184.5188, 1282.024) 184.5188
## 24 materiál    ets 2027 Dec  N(186.6283, 1450.038) 186.6283
## 25    práca    ets 2022 Mar   N(127.758, 5.066513) 127.7580
## 26    práca    ets 2022 Jun  N(131.2259, 13.81461) 131.2259
## 27    práca    ets 2022 Sep  N(132.7416, 27.24649) 132.7416
## 28    práca    ets 2022 Dec  N(133.8916, 46.36252) 133.8916
## 29    práca    ets 2023 Mar  N(136.3496, 72.16722) 136.3496
## 30    práca    ets 2023 Jun  N(139.8175, 105.6526) 139.8175
## 31    práca    ets 2023 Sep  N(141.3332, 147.8233) 141.3332
## 32    práca    ets 2023 Dec  N(142.4832, 199.6796) 142.4832
## 33    práca    ets 2024 Mar  N(144.9412, 262.2283) 144.9412
## 34    práca    ets 2024 Jun  N(148.4091, 336.4569) 148.4091
## 35    práca    ets 2024 Sep  N(149.9248, 423.3721) 149.9248
## 36    práca    ets 2024 Dec  N(151.0748, 523.9745) 151.0748
## 37    práca    ets 2025 Mar   N(153.5328, 639.273) 153.5328
## 38    práca    ets 2025 Jun  N(157.0007, 770.2506) 157.0007
## 39    práca    ets 2025 Sep  N(158.5164, 917.9163) 158.5164
## 40    práca    ets 2025 Dec   N(159.6664, 1083.27) 159.6664
## 41    práca    ets 2026 Mar  N(162.1244, 1267.325) 162.1244
## 42    práca    ets 2026 Jun  N(165.5923, 1471.057) 165.5923
## 43    práca    ets 2026 Sep   N(167.108, 1695.479) 167.1080
## 44    práca    ets 2026 Dec   N(168.258, 1941.591) 168.2580
## 45    práca    ets 2027 Mar   N(170.716, 2210.407) 170.7160
## 46    práca    ets 2027 Jun  N(174.1839, 2502.899) 174.1839
## 47    práca    ets 2027 Sep  N(175.6996, 2820.083) 175.6996
## 48    práca    ets 2027 Dec  N(176.8496, 3162.959) 176.8496
##                       80%                     95%
## 1  [127.1949, 129.3040]80 [126.63669, 129.8622]95
## 2  [128.8127, 132.6700]80 [127.79170, 133.6909]95
## 3  [130.5352, 136.5331]80 [128.94770, 138.1206]95
## 4  [131.4173, 139.8702]80 [129.17994, 142.1075]95
## 5  [132.8555, 144.0373]80 [129.89586, 146.9969]95
## 6  [133.8596, 148.0168]80 [130.11246, 151.7640]95
## 7  [135.0513, 152.4108]80 [130.45650, 157.0056]95
## 8  [135.4540, 156.2274]80 [129.95558, 161.7257]95
## 9  [136.4502, 160.8364]80 [129.99554, 167.2911]95
## 10 [137.0415, 165.2288]80 [129.58078, 172.6895]95
## 11 [137.8441, 170.0118]80 [129.32986, 178.5261]95
## 12 [137.8777, 174.1974]80 [128.26448, 183.8107]95
## 13 [138.5220, 179.1585]80 [127.76612, 189.9143]95
## 14 [138.7761, 183.8880]80 [126.83574, 195.8284]95
## 15 [139.2547, 188.9951]80 [126.08923, 202.1606]95
## 16 [138.9759, 193.4931]80 [124.54608, 207.9229]95
## 17 [139.3182, 198.7561]80 [123.58595, 214.4883]95
## 18 [139.2799, 203.7781]80 [122.20829, 220.8497]95
## 19 [139.4747, 209.1690]80 [121.02768, 227.6160]95
## 20 [138.9199, 213.9429]80 [119.06252, 233.8003]95
## 21 [138.9935, 219.4746]80 [117.69150, 240.7766]95
## 22 [138.6933, 224.7585]80 [115.91322, 247.5386]95
## 23 [138.6323, 230.4052]80 [114.34152, 254.6960]95
## 24 [137.8277, 235.4290]80 [111.99416, 261.2625]95
## 25 [124.8734, 130.6427]80 [123.34635, 132.1697]95
## 26 [126.4626, 135.9891]80 [123.94107, 138.5107]95
## 27 [126.0521, 139.4311]80 [122.51094, 142.9722]95
## 28 [125.1655, 142.6177]80 [120.54621, 147.2370]95
## 29 [125.4627, 147.2366]80 [119.69947, 152.9998]95
## 30 [126.6447, 152.9902]80 [119.67150, 159.9634]95
## 31 [125.7518, 156.9146]80 [117.50344, 165.1629]95
## 32 [124.3739, 160.5926]80 [114.78735, 170.1791]95
## 33 [124.1885, 165.6940]80 [113.20262, 176.6798]95
## 34 [124.9019, 171.9163]80 [112.45793, 184.3602]95
## 35 [123.5556, 176.2940]80 [109.59656, 190.2530]95
## 36 [121.7395, 180.4102]80 [106.21028, 195.9393]95
## 37 [121.1303, 185.9354]80 [103.97740, 203.0883]95
## 38 [121.4333, 192.5681]80 [102.60504, 211.3963]95
## 39 [119.6891, 197.3437]80 [ 99.13512, 217.8977]95
## 40 [117.4866, 201.8462]80 [ 95.15798, 224.1749]95
## 41 [116.5018, 207.7470]80 [ 92.35069, 231.8982]95
## 42 [116.4392, 214.7454]80 [ 90.41912, 240.7655]95
## 43 [114.3386, 219.8774]80 [ 86.40415, 247.8119]95
## 44 [111.7884, 224.7277]80 [ 81.89518, 254.6209]95
## 45 [110.4639, 230.9681]80 [ 78.56841, 262.8637]95
## 46 [110.0692, 238.2986]80 [ 76.12888, 272.2389]95
## 47 [107.6435, 243.7557]80 [ 71.61679, 279.7824]95
## 48 [104.7749, 248.9243]80 [ 66.62089, 287.0784]95
dat %>% 
  fabletools::model(
    ets = fable::ETS(cena ~ trend("A") + season("A") + error("A"))
  ) %>% 
  dplyr::filter(artikel == "materiál") %>% 
  fabletools::report()
## Series: cena 
## Model: ETS(A,A,A) 
##   Smoothing parameters:
##     alpha = 0.8848983 
##     beta  = 0.6464166 
##     gamma = 0.0001000139 
## 
##   Initial states:
##      l[0]      b[0]      s[0]     s[-1]       s[-2]      s[-3]
##  100.3301 0.5237986 -0.222261 0.2173842 -0.02625073 0.03112761
## 
##   sigma^2:  0.6771
## 
##      AIC     AICc      BIC 
## 75.18331 88.04046 85.78580

ARIMA

Tato cast tiez uplne dobre nefunguje - je tam asi problem v type dat, skusim to nejako zmenit aby to fungovalo

predpovede <- dat

Regresny model

predpovede <- dat %>% 
  fabletools::model(
    reg = fable::TSLM(
      cena ~ trend(knots = yearmonth(c("2017-12-01","2018-03-01", "2021-01-01"))) +
        season()
    )
  ) %>% 
  fabletools::forecast(h = 24) 

predpovede %>% 
  autoplot() + autolayer(dat)

predpovede %>% 
  as.data.frame()
##     artikel .model      čas                   cena    .mean
## 1  materiál    reg 2022 Mar N(127.6027, 0.7212832) 127.6027
## 2  materiál    reg 2022 Jun N(129.7928, 0.8883515) 129.7928
## 3  materiál    reg 2022 Sep  N(132.3495, 1.096798) 132.3495
## 4  materiál    reg 2022 Dec  N(134.3062, 1.346623) 134.3062
## 5  materiál    reg 2023 Mar  N(136.6655, 1.800566) 136.6655
## 6  materiál    reg 2023 Jun  N(138.8556, 2.140236) 138.8556
## 7  materiál    reg 2023 Sep  N(141.4123, 2.521285) 141.4123
## 8  materiál    reg 2023 Dec   N(143.369, 2.943712) 143.3690
## 9  materiál    reg 2024 Mar  N(145.7283, 3.647265) 145.7283
## 10 materiál    reg 2024 Jun  N(147.9184, 4.159538) 147.9184
## 11 materiál    reg 2024 Sep  N(150.4751, 4.713189) 150.4751
## 12 materiál    reg 2024 Dec  N(152.4318, 5.308219) 152.4318
## 13 materiál    reg 2025 Mar  N(154.7911, 6.261382) 154.7911
## 14 materiál    reg 2025 Jun  N(156.9812, 6.946257) 156.9812
## 15 materiál    reg 2025 Sep   N(159.5379, 7.67251) 159.5379
## 16 materiál    reg 2025 Dec  N(161.4946, 8.440141) 161.4946
## 17 materiál    reg 2026 Mar  N(163.8539, 9.642915) 163.8539
## 18 materiál    reg 2026 Jun   N(166.044, 10.50039) 166.0440
## 19 materiál    reg 2026 Sep  N(168.6007, 11.39925) 168.6007
## 20 materiál    reg 2026 Dec  N(170.5574, 12.33948) 170.5574
## 21 materiál    reg 2027 Mar  N(172.9167, 13.79186) 172.9167
## 22 materiál    reg 2027 Jun  N(175.1068, 14.82194) 175.1068
## 23 materiál    reg 2027 Sep   N(177.6635, 15.8934) 177.6635
## 24 materiál    reg 2027 Dec  N(179.6202, 17.00624) 179.6202
## 25    práca    reg 2022 Mar   N(132.9161, 5.77098) 132.9161
## 26    práca    reg 2022 Jun  N(138.6227, 7.107692) 138.6227
## 27    práca    reg 2022 Sep  N(142.3793, 8.775472) 142.3793
## 28    práca    reg 2022 Dec  N(145.8692, 10.77432) 145.8692
## 29    práca    reg 2023 Mar  N(150.7792, 14.40631) 150.7792
## 30    práca    reg 2023 Jun  N(156.4858, 17.12401) 156.4858
## 31    práca    reg 2023 Sep  N(160.2424, 20.17278) 160.2424
## 32    práca    reg 2023 Dec  N(163.7323, 23.55262) 163.7323
## 33    práca    reg 2024 Mar  N(168.6423, 29.18174) 168.6423
## 34    práca    reg 2024 Jun  N(174.3489, 33.28043) 174.3489
## 35    práca    reg 2024 Sep  N(178.1055, 37.71018) 178.1055
## 36    práca    reg 2024 Dec  N(181.5954, 42.47101) 181.5954
## 37    práca    reg 2025 Mar  N(186.5055, 50.09726) 186.5055
## 38    práca    reg 2025 Jun   N(192.212, 55.57693) 192.2120
## 39    práca    reg 2025 Sep  N(195.9686, 61.38768) 195.9686
## 40    práca    reg 2025 Dec  N(199.4585, 67.52949) 199.4585
## 41    práca    reg 2026 Mar  N(204.3686, 77.15287) 204.3686
## 42    práca    reg 2026 Jun  N(210.0751, 84.01354) 210.0751
## 43    práca    reg 2026 Sep  N(213.8317, 91.20527) 213.8317
## 44    práca    reg 2026 Dec  N(217.3216, 98.72807) 217.3216
## 45    práca    reg 2027 Mar  N(222.2317, 110.3486) 222.2317
## 46    práca    reg 2027 Jun  N(227.9383, 118.5902) 227.9383
## 47    práca    reg 2027 Sep   N(231.6948, 127.163) 231.6948
## 48    práca    reg 2027 Dec  N(235.1847, 136.0667) 235.1847