Prediction intervals for multivariate time series (simulation-based)

ahead is a package for univariate and multivariate time series forecasting, with uncertainty quantification (R and Python).

The model used in this demo is the one presented in this paper, and has actually been significantly improved since 2018, as you’ll see here.

Currently for this model (as of 2023-08-28), for uncertainty quantification, I have:

  • Gaussian distribution
  • Independent bootstrap simulation
  • Multivariate circular block bootstrap simulation
  • Multivariate moving block bootstrap simulation
  • Copula simulation
  • More options to come in the future.

Please remember that in real life, this model’s hyperparameters will have to be tuned.

Contents:

  • 0 - Install ahead
  • 1 - Prediction intervals based on Gaussian distribution (meh, but quick)
  • 2 - Prediction intervals based on independent bootstrap
  • 3 - Prediction intervals based on block bootstrap
  • 4 - Prediction intervals based on moving block bootstrap
  • 5 - Prediction intervals based on R-Vine copula simulation

0 - Install ahead

Here’s how to install the R version of the package:

  • 1st method: from R-universe

    In R console:

    options(repos = c(
        techtonique = 'https://techtonique.r-universe.dev',
        CRAN = 'https://cloud.r-project.org'))
    
    install.packages("ahead")
  • 2nd method: from Github

    In R console:

    devtools::install_github("Techtonique/ahead")

    Or

    remotes::install_github("Techtonique/ahead")

And here are the packages that will be used in this vignette:

library(ahead)
library(fpp)

1 - Prediction intervals based on Gaussian distribution (meh, but quick)

1 - 1 With default parameters

h <- 10L # forecasting horizon

(res1 <- ahead::ridge2f(fpp::insurance, h = h))
#> Installing package into '/tmp/Rtmp5pHZmF/Rinst189394aba9f'
#> (as 'lib' is unspecified)
#> also installing the dependency 'rngWELL'
#> Installing package into '/tmp/Rtmp5pHZmF/Rinst189394aba9f'
#> (as 'lib' is unspecified)
#> also installing the dependencies 'mvtnorm', 'ADGofTest'
#> $mean
#>            Quotes TV.advert
#> May 2005 14.76393  8.947782
#> Jun 2005 14.61841  8.862613
#> Jul 2005 14.56234  8.841193
#> Aug 2005 14.49305  8.809091
#> Sep 2005 14.43100  8.781208
#> Oct 2005 14.37179  8.754418
#> Nov 2005 14.31606  8.729243
#> Dec 2005 14.26346  8.705474
#> Jan 2006 14.21385  8.683054
#> Feb 2006 14.16704  8.661902
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 11.96618  7.011446
#> Jun 2005 11.82066  6.926276
#> Jul 2005 11.76459  6.904857
#> Aug 2005 11.69530  6.872755
#> Sep 2005 11.63325  6.844872
#> Oct 2005 11.57404  6.818082
#> Nov 2005 11.51831  6.792907
#> Dec 2005 11.46571  6.769138
#> Jan 2006 11.41610  6.746718
#> Feb 2006 11.36929  6.725566
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.56168  10.88412
#> Jun 2005 17.41616  10.79895
#> Jul 2005 17.36009  10.77753
#> Aug 2005 17.29080  10.74543
#> Sep 2005 17.22875  10.71754
#> Oct 2005 17.16954  10.69075
#> Nov 2005 17.11381  10.66558
#> Dec 2005 17.06121  10.64181
#> Jan 2006 17.01160  10.61939
#> Feb 2006 16.96479  10.59824
#> 
#> $sims
#> NULL
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.40368643  1.33052782
#> 2003 -1.08056990 -1.14582835
#> 2004 -1.23343504 -1.15469695
#> 2005  1.40368643  1.33052782
#> 2006 -2.58725990 -2.26486335
#> 2007 -3.07477874 -1.76651025
#> 2008 -1.31677412 -1.06553020
#> 2009  0.65681006  0.07019039
#> 2010  0.33076863 -0.40143292
#> 2011  0.51993723  0.22434076
#> 2012  0.10053432 -0.17249791
#> 2013  0.95770684  0.35761548
#> 2014  0.26825909 -0.01668861
#> 2015 -0.12839403 -0.42852687
#> 2016 -1.17137522 -0.76232506
#> 2017  0.19770654  0.96971721
#> 2018  1.62259434  1.31981494
#> 2019 -0.14704277  0.17362676
#> 2020 -0.07045343 -0.05730098
#> 2021 -1.17072203 -0.66882907
#> 2022 -2.89887258 -1.53863637
#> 2023 -0.62736155 -0.06504866
#> 2024  0.96560231  0.19518092
#> 2025 -0.09354593 -0.06951135
#> 2026  0.15915097  1.22257588
#> 2027  0.29875209  0.25719228
#> 2028 -1.31329880 -0.69076345
#> 2029  2.48926902  1.54309187
#> 2030  1.91175656  0.91165371
#> 2031  2.04130891  1.23352769
#> 2032 -0.29759583 -0.05351566
#> 2033 -1.22025180 -0.56162404
#> 2034 -0.95856611 -0.54609232
#> 2035  0.20717781  0.29095534
#> 2036 -0.70462371 -1.15899926
#> 2037  2.26007518  1.40841386
#> 2038  2.95319223  1.69957104
#> 2039  0.89317411  0.77051186
#> 2040 -1.54622760 -0.71981400
#> 
#> $coefficients
#>        Quotes   TV.advert
#> x1  0.4358443  0.18606264
#> x2  0.1361999  0.08192973
#> h1  0.2311472 -0.09036149
#> h2 -1.2318730 -0.60953989
#> h3 -0.1129694 -0.08956715
#> h4  0.4033300  0.15098381
#> h5 -0.4866789 -0.25350343
#> 
#> $loocv
#> [1] 1.924173
#> 
#> $weighted_loocv
#> [1] -2.913168e-17
#> 
#> $loocv_per_series
#>    Quotes TV.advert 
#>  2.601976  1.246370 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res1$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-3.0748   Min.   :-2.26486  
#>  1st Qu.:-1.1256   1st Qu.:-0.67980  
#>  Median : 0.1005   Median :-0.05352  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9254   3rd Qu.: 0.84108  
#>  Max.   : 2.9532   Max.   : 1.69957

print(apply(res1$residuals, 2, function (x) Box.test(x)$p.value)) # stationarity is overrated?:)
#>     Quotes  TV.advert 
#> 0.06458106 0.32251812

print(apply(res1$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.7471878 0.3924569

plot(res1, "Quotes")

plot(res1, "TV.advert")

1 - 2 With external regressors

xreg <- as.numeric(time(fpp::insurance))
(res2 <- ahead::ridge2f(fpp::insurance, xreg = xreg, h = h))
#> $mean
#>            Quotes TV.advert
#> May 2005 15.45580  9.403836
#> Jun 2005 15.50296  9.542891
#> Jul 2005 15.24251  9.457418
#> Aug 2005 15.02165  9.351020
#> Sep 2005 14.95493  9.307973
#> Oct 2005 15.02539  9.335216
#> Nov 2005 15.17341  9.408039
#> Dec 2005 15.34645  9.499447
#> Jan 2006 15.51500  9.592048
#> Feb 2006 15.66959  9.678782
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 12.76518  7.579582
#> Jun 2005 12.81234  7.718637
#> Jul 2005 12.55189  7.633164
#> Aug 2005 12.33103  7.526766
#> Sep 2005 12.26431  7.483719
#> Oct 2005 12.33477  7.510962
#> Nov 2005 12.48279  7.583785
#> Dec 2005 12.65583  7.675193
#> Jan 2006 12.82438  7.767794
#> Feb 2006 12.97897  7.854528
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 18.14642  11.22809
#> Jun 2005 18.19359  11.36714
#> Jul 2005 17.93313  11.28167
#> Aug 2005 17.71227  11.17527
#> Sep 2005 17.64555  11.13223
#> Oct 2005 17.71601  11.15947
#> Nov 2005 17.86403  11.23229
#> Dec 2005 18.03707  11.32370
#> Jan 2006 18.20562  11.41630
#> Feb 2006 18.36021  11.50304
#> 
#> $sims
#> NULL
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes    TV.advert
#> 2002  1.67535261  1.600318363
#> 2003 -0.09454667  0.059245589
#> 2004 -0.61710349 -0.617562341
#> 2005  1.55432930  1.431543276
#> 2006 -1.65830176 -1.201288646
#> 2007 -3.27721670 -1.999680352
#> 2008 -0.83986146 -0.746117612
#> 2009  1.04485279  0.291750330
#> 2010  0.75352722 -0.155143365
#> 2011  0.26745241  0.004965096
#> 2012  0.43157027  0.102861987
#> 2013  1.13693270  0.494399368
#> 2014  0.49053241  0.135721074
#> 2015 -0.10253286 -0.465058629
#> 2016 -1.85353900 -1.312388835
#> 2017 -0.33001834  0.475005003
#> 2018  2.16635830  1.749815332
#> 2019  0.17739319  0.443529161
#> 2020  0.27361734  0.111136601
#> 2021 -0.58111949 -0.379519019
#> 2022 -2.30767070 -1.246635836
#> 2023 -0.19353452  0.176564889
#> 2024  1.00563207  0.187437814
#> 2025  0.41917089  0.235645632
#> 2026  0.52295932  1.364524163
#> 2027 -0.95933919 -0.673442252
#> 2028 -1.56353647 -0.843576594
#> 2029  2.26526358  1.468847972
#> 2030  1.69164304  0.697529740
#> 2031  2.01008148  1.118934668
#> 2032 -0.38235687 -0.261094013
#> 2033 -1.39361339 -0.785256869
#> 2034 -1.33394033 -0.789489620
#> 2035 -0.37279705 -0.008190995
#> 2036 -1.43004502 -1.625855697
#> 2037  0.16351013  0.242165460
#> 2038  2.64510868  1.356646561
#> 2039  0.59781674  0.490409659
#> 2040 -2.00203118 -1.128697062
#> 
#> $coefficients
#>        Quotes  TV.advert
#> x1  1.5520048  0.6627858
#> x2 -1.6051784 -0.5784973
#> x3 -1.7090968 -0.5873351
#> h1  1.7017413  0.7132521
#> h2 -1.1790106 -0.5946307
#> h3 -0.7179868 -0.3862358
#> h4  0.6670119  0.1636983
#> h5 -0.3598980 -0.1534834
#> 
#> $loocv
#> [1] 1.243869
#> 
#> $weighted_loocv
#> [1] 2.484213e-17
#> 
#> $loocv_per_series
#>       Quotes    TV.advert       xreg_1 
#> 2.556419e+00 1.175162e+00 2.765868e-05 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res2$residuals))
#>      Quotes          TV.advert      
#>  Min.   :-3.2772   Min.   :-1.9997  
#>  1st Qu.:-0.8996   1st Qu.:-0.7098  
#>  Median : 0.1635   Median : 0.1029  
#>  Mean   : 0.0000   Mean   : 0.0000  
#>  3rd Qu.: 0.8796   3rd Qu.: 0.4827  
#>  Max.   : 2.6451   Max.   : 1.7498

print(apply(res2$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.1553145 0.4247378

print(apply(res2$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9498845 0.5141136

plot(res2, "Quotes")

plot(res2, "TV.advert")

1 - 3 With external regressors and K-Means clustering

centers <- 2L
(res3 <- ahead::ridge2f(fpp::insurance, xreg = xreg, centers = centers, h = h))
#> $mean
#>            Quotes TV.advert
#> May 2005 16.85208 10.372047
#> Jun 2005 15.06174  9.371331
#> Jul 2005 14.71415  9.244101
#> Aug 2005 14.17693  8.942588
#> Sep 2005 14.15692  8.919681
#> Oct 2005 14.18453  8.928931
#> Nov 2005 14.41917  9.073567
#> Dec 2005 14.58104  9.188771
#> Jan 2006 14.75620  9.318539
#> Feb 2006 14.85389  9.404282
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 14.35980  8.635699
#> Jun 2005 12.56946  7.634984
#> Jul 2005 12.22187  7.507753
#> Aug 2005 11.68465  7.206241
#> Sep 2005 11.66464  7.183334
#> Oct 2005 11.69224  7.192584
#> Nov 2005 11.92689  7.337220
#> Dec 2005 12.08876  7.452424
#> Jan 2006 12.26392  7.582192
#> Feb 2006 12.36161  7.667934
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 19.34436  12.10839
#> Jun 2005 17.55402  11.10768
#> Jul 2005 17.20643  10.98045
#> Aug 2005 16.66921  10.67894
#> Sep 2005 16.64920  10.65603
#> Oct 2005 16.67681  10.66528
#> Nov 2005 16.91145  10.80991
#> Dec 2005 17.07332  10.92512
#> Jan 2006 17.24848  11.05489
#> Feb 2006 17.34617  11.14063
#> 
#> $sims
#> NULL
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.11874618  1.19921616
#> 2003  0.09390745  0.01901986
#> 2004 -1.43434322 -1.16781389
#> 2005  1.27193197  1.24470742
#> 2006 -1.49065784 -1.21723396
#> 2007 -2.34261676 -1.51417390
#> 2008 -0.46298299 -0.60250287
#> 2009  1.63235318  0.57215494
#> 2010  0.40411221 -0.32801633
#> 2011  0.48277892  0.18055867
#> 2012  0.57087239  0.21240486
#> 2013  1.17734665  0.57331263
#> 2014  0.11531808  0.03121107
#> 2015 -0.42672324 -0.52697371
#> 2016 -1.85435793 -1.23203209
#> 2017 -0.03066633  0.70089111
#> 2018  2.03550258  1.71783757
#> 2019  0.10090940  0.43690274
#> 2020 -0.06200294  0.01106144
#> 2021  0.13620873  0.06072585
#> 2022 -1.89082466 -0.95683806
#> 2023 -0.59474544  0.01552906
#> 2024  0.86784469  0.14757423
#> 2025  0.64438935  0.39749239
#> 2026  1.21235947  1.79794968
#> 2027 -0.72834160 -0.52926182
#> 2028 -2.25083381 -1.18159761
#> 2029  1.33778080  0.97343623
#> 2030  1.97229999  0.89921107
#> 2031  0.98424529  0.52904493
#> 2032 -0.10842601 -0.11619745
#> 2033 -2.24369150 -1.28978017
#> 2034 -0.89142004 -0.54783127
#> 2035 -0.04783668  0.13304638
#> 2036 -1.11899454 -1.45036144
#> 2037  0.60160639  0.35230619
#> 2038  2.54250721  1.21587964
#> 2039  0.43166441  0.33684394
#> 2040 -1.75521983 -1.09770352
#> 
#> $coefficients
#>        Quotes  TV.advert
#> x1  3.4186614  1.5473408
#> x2 -3.1731701 -1.3443203
#> x3  0.1632802  0.3192611
#> x4  2.0400926  1.0944911
#> x5 -2.0400926 -1.0944911
#> h1  1.3316541  0.5511770
#> h2 -0.8883466 -0.2904272
#> h3  2.1231054  1.3697223
#> h4  1.4885414  0.7223003
#> h5 -0.4781963 -0.2255774
#> 
#> $loocv
#> [1] 0.7972857
#> 
#> $weighted_loocv
#> [1] 3.031318e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert        xreg_1 xreg_cluster1 xreg_cluster2 
#>  2.444624e+00  1.186564e+00  2.824183e-05  1.776061e-01  1.776061e-01 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res3$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-2.3426   Min.   :-1.51417  
#>  1st Qu.:-0.8099   1st Qu.:-0.57517  
#>  Median : 0.1009   Median : 0.06073  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9260   3rd Qu.: 0.55060  
#>  Max.   : 2.5425   Max.   : 1.79795

print(apply(res3$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.2913532 0.6500526

print(apply(res3$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.4150247 0.2507454

plot(res3, "Quotes")

plot(res3, "TV.advert")

1 - 4 With K-Means clustering

centers <- 2L
(res10 <- ahead::ridge2f(fpp::insurance, centers = centers, h = h))
#> $mean
#>            Quotes TV.advert
#> May 2005 14.10717  8.489682
#> Jun 2005 14.38158  8.175616
#> Jul 2005 15.45634  8.470898
#> Aug 2005 16.77134  9.147753
#> Sep 2005 17.34214  9.699675
#> Oct 2005 16.90309  9.754963
#> Nov 2005 15.99418  9.391872
#> Dec 2005 15.35446  8.958560
#> Jan 2006 15.36658  8.772022
#> Feb 2006 15.87293  8.905313
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 11.17305  6.463342
#> Jun 2005 11.44747  6.149277
#> Jul 2005 12.52222  6.444558
#> Aug 2005 13.83723  7.121414
#> Sep 2005 14.40802  7.673336
#> Oct 2005 13.96898  7.728624
#> Nov 2005 13.06007  7.365533
#> Dec 2005 12.42034  6.932220
#> Jan 2006 12.43246  6.745682
#> Feb 2006 12.93881  6.878974
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.04128  10.51602
#> Jun 2005 17.31569  10.20196
#> Jul 2005 18.39045  10.49724
#> Aug 2005 19.70546  11.17409
#> Sep 2005 20.27625  11.72601
#> Oct 2005 19.83721  11.78130
#> Nov 2005 18.92830  11.41821
#> Dec 2005 18.28857  10.98490
#> Jan 2006 18.30069  10.79836
#> Feb 2006 18.80704  10.93165
#> 
#> $sims
#> NULL
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $coefficients
#>        Quotes   TV.advert
#> x1  2.4575845  0.91704166
#> x2 -2.0640894 -0.95963434
#> x3 -0.9277974 -0.39680734
#> x4  0.9277974  0.39680734
#> h1  0.8772790  0.54857210
#> h2 -1.6557144 -0.50781090
#> h3 -0.4923290 -0.01455017
#> h4  0.8215335  0.53984616
#> h5 -0.4400446 -0.52582355
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res10$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res10$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

print(apply(res10$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

plot(res10, "Quotes")

plot(res10, "TV.advert")

2 - Prediction intervals based on independent bootstrap

2 - 1 With default parameters

B <- 10L # number of bootstrap replications -- increase this
(res4 <- ahead::ridge2f(fpp::insurance, 
                        type_pi = "bootstrap", B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.95424  8.917110
#> Jun 2005 14.49974  8.636097
#> Jul 2005 14.89943  9.021361
#> Aug 2005 13.81663  8.379904
#> Sep 2005 13.21467  7.907439
#> Oct 2005 12.43278  7.492033
#> Nov 2005 13.26207  8.025966
#> Dec 2005 13.83344  8.416299
#> Jan 2006 14.05378  8.520859
#> Feb 2006 14.27316  8.593758
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 12.084705  7.338993
#> Jun 2005 12.334471  6.899060
#> Jul 2005 10.169184  6.394872
#> Aug 2005 11.009906  6.891660
#> Sep 2005 10.892449  5.821847
#> Oct 2005  9.512025  5.810005
#> Nov 2005 11.136196  7.072952
#> Dec 2005  9.352377  6.033064
#> Jan 2006 10.779190  6.889282
#> Feb 2006 10.410663  6.496912
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 15.53240  9.620957
#> Jul 2005 17.01236 10.447844
#> Aug 2005 16.25077 10.429662
#> Sep 2005 15.17868  9.484860
#> Oct 2005 13.87775  8.808728
#> Nov 2005 15.02885  9.384877
#> Dec 2005 16.01964  9.714979
#> Jan 2006 16.78336 10.055364
#> Feb 2006 17.73885 10.720315
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 15.09470  8.546350
#> Jun 2005 15.26574  8.957984
#> Jul 2005 17.14212 10.521963
#> Aug 2005 14.47419  8.777585
#> Sep 2005 13.80475  7.662264
#> Oct 2005 13.29210  7.242733
#> Nov 2005 12.93090  7.530277
#> Dec 2005 14.38592  8.007453
#> Jan 2006 13.75002  8.075816
#> Feb 2006 13.91423  7.624468
#> 
#> $sims[[2]]
#>             Quotes TV.advert
#> May 2005 14.693479  8.890481
#> Jun 2005 12.071239  6.621006
#> Jul 2005 16.321333  9.408312
#> Aug 2005 16.301909 10.599011
#> Sep 2005 10.751088  6.451488
#> Oct 2005 12.825250  8.975899
#> Nov 2005 10.917488  7.055409
#> Dec 2005  8.509706  5.597015
#> Jan 2006 12.748927  8.157222
#> Feb 2006 15.261147  9.472318
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 15.51548  9.305770
#> Jul 2005 15.51882  9.127883
#> Aug 2005 13.05521  7.008770
#> Sep 2005 11.37936  5.649254
#> Oct 2005 13.04494  7.183429
#> Nov 2005 14.34255  7.673654
#> Dec 2005 15.68812  9.097144
#> Jan 2006 15.08182  8.093210
#> Feb 2006 15.28630  8.804900
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 11.68915  7.181272
#> Jun 2005 15.16186  9.360902
#> Jul 2005 16.50681 10.087084
#> Aug 2005 13.47757  8.178792
#> Sep 2005 14.49698  8.965963
#> Oct 2005 12.82710  7.912189
#> Nov 2005 12.26039  7.194058
#> Dec 2005 16.11588  9.746326
#> Jan 2006 17.15008 10.279865
#> Feb 2006 16.07570  9.332013
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 14.67039  8.878271
#> Jun 2005 15.53732  9.650251
#> Jul 2005 10.79614  6.687064
#> Aug 2005 12.45865  7.752375
#> Sep 2005 15.36642  9.557945
#> Oct 2005 12.55254  7.850653
#> Nov 2005 15.04672  9.338306
#> Dec 2005 13.15837  8.368249
#> Jan 2006 14.75788  9.282082
#> Feb 2006 10.12754  6.388148
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 15.09470  8.546350
#> Jun 2005 14.32990  7.856803
#> Jul 2005 16.26642  9.830921
#> Aug 2005 16.07463  9.846349
#> Sep 2005 13.61470  8.234302
#> Oct 2005 13.38812  8.232918
#> Nov 2005 13.58878  8.044008
#> Dec 2005 15.48621  9.136085
#> Jan 2006 15.52022  9.209342
#> Feb 2006 17.23132 10.632457
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 15.14628  8.991276
#> Jul 2005 15.61068 10.192546
#> Aug 2005 10.58930  6.857661
#> Sep 2005 13.47287  8.511551
#> Oct 2005 10.19723  5.739297
#> Nov 2005 11.88952  7.133378
#> Dec 2005 12.25491  7.535011
#> Jan 2006 10.44075  6.714824
#> Feb 2006 13.16995  8.647643
#> 
#> $sims[[8]]
#>             Quotes TV.advert
#> May 2005 13.447159  7.882252
#> Jun 2005 13.241160  7.983379
#> Jul 2005 16.565430 10.160963
#> Aug 2005 14.500822  8.968994
#> Sep 2005 13.156521  8.392164
#> Oct 2005  9.313095  6.053557
#> Nov 2005 12.581349  8.323640
#> Dec 2005 14.074785  8.890812
#> Jan 2006 11.944916  7.490191
#> Feb 2006 11.385856  6.871544
#> 
#> $sims[[9]]
#>             Quotes TV.advert
#> May 2005 14.616890  9.121409
#> Jun 2005 13.634215  9.520057
#> Jul 2005  9.987166  6.310043
#> Aug 2005 13.703893  8.339510
#> Sep 2005 11.571965  6.416334
#> Oct 2005 14.019898  7.954921
#> Nov 2005 14.095724  8.568528
#> Dec 2005 13.091223  8.177888
#> Jan 2006 14.042929  8.960467
#> Feb 2006 12.393313  7.418268
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 15.72954  9.142963
#> Jun 2005 15.09424  8.113548
#> Jul 2005 14.27943  7.886835
#> Aug 2005 13.53009  7.469997
#> Sep 2005 14.53202  9.233122
#> Oct 2005 12.86756  7.774732
#> Nov 2005 14.96732  9.398397
#> Dec 2005 15.56929  9.607004
#> Jan 2006 15.10023  8.945575
#> Feb 2006 17.88620 10.745822
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.40368643  1.33052782
#> 2003 -1.08056990 -1.14582835
#> 2004 -1.23343504 -1.15469695
#> 2005  1.40368643  1.33052782
#> 2006 -2.58725990 -2.26486335
#> 2007 -3.07477874 -1.76651025
#> 2008 -1.31677412 -1.06553020
#> 2009  0.65681006  0.07019039
#> 2010  0.33076863 -0.40143292
#> 2011  0.51993723  0.22434076
#> 2012  0.10053432 -0.17249791
#> 2013  0.95770684  0.35761548
#> 2014  0.26825909 -0.01668861
#> 2015 -0.12839403 -0.42852687
#> 2016 -1.17137522 -0.76232506
#> 2017  0.19770654  0.96971721
#> 2018  1.62259434  1.31981494
#> 2019 -0.14704277  0.17362676
#> 2020 -0.07045343 -0.05730098
#> 2021 -1.17072203 -0.66882907
#> 2022 -2.89887258 -1.53863637
#> 2023 -0.62736155 -0.06504866
#> 2024  0.96560231  0.19518092
#> 2025 -0.09354593 -0.06951135
#> 2026  0.15915097  1.22257588
#> 2027  0.29875209  0.25719228
#> 2028 -1.31329880 -0.69076345
#> 2029  2.48926902  1.54309187
#> 2030  1.91175656  0.91165371
#> 2031  2.04130891  1.23352769
#> 2032 -0.29759583 -0.05351566
#> 2033 -1.22025180 -0.56162404
#> 2034 -0.95856611 -0.54609232
#> 2035  0.20717781  0.29095534
#> 2036 -0.70462371 -1.15899926
#> 2037  2.26007518  1.40841386
#> 2038  2.95319223  1.69957104
#> 2039  0.89317411  0.77051186
#> 2040 -1.54622760 -0.71981400
#> 
#> $loocv
#> [1] 1.924173
#> 
#> $weighted_loocv
#> [1] -2.913168e-17
#> 
#> $loocv_per_series
#>    Quotes TV.advert 
#>  2.601976  1.246370 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res4$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-3.0748   Min.   :-2.26486  
#>  1st Qu.:-1.1256   1st Qu.:-0.67980  
#>  Median : 0.1005   Median :-0.05352  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9254   3rd Qu.: 0.84108  
#>  Max.   : 2.9532   Max.   : 1.69957

print(apply(res4$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.06458106 0.32251812

print(apply(res4$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.7471878 0.3924569

plot(res4, "Quotes")

plot(res4, "TV.advert")

2 - 2 With external regressors

(res5 <- ahead::ridge2f(fpp::insurance, xreg = xreg, 
                        type_pi = "bootstrap", B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 15.83543  9.489772
#> Jun 2005 15.85604  9.556819
#> Jul 2005 16.47363 10.091468
#> Aug 2005 15.49717  9.499224
#> Sep 2005 14.92088  9.093326
#> Oct 2005 14.80031  9.044508
#> Nov 2005 16.07283  9.721118
#> Dec 2005 16.55953 10.012005
#> Jan 2006 16.38269  9.838773
#> Feb 2006 17.24196 10.332083
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 12.72699  7.686207
#> Jun 2005 14.27121  8.487002
#> Jul 2005 11.92056  7.522689
#> Aug 2005 12.63396  8.135428
#> Sep 2005 13.39517  8.189226
#> Oct 2005 10.62521  6.861347
#> Nov 2005 14.30810  8.911368
#> Dec 2005 11.71644  7.398021
#> Jan 2006 13.99457  8.665999
#> Feb 2006 11.97363  7.533064
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.72106  10.87268
#> Jun 2005 17.22584  10.42581
#> Jul 2005 19.31335  11.94216
#> Aug 2005 17.91034  11.35180
#> Sep 2005 17.67846  10.95107
#> Oct 2005 17.38254  10.35569
#> Nov 2005 17.67314  10.58502
#> Dec 2005 19.49927  11.56067
#> Jan 2006 19.08364  11.30186
#> Feb 2006 20.55522  12.37432
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 16.20933  9.248692
#> Jun 2005 17.27866 10.181992
#> Jul 2005 19.31439 11.954437
#> Aug 2005 15.71288  9.435132
#> Sep 2005 14.78888  8.245459
#> Oct 2005 17.46036  9.863086
#> Nov 2005 17.49723 10.226270
#> Dec 2005 18.65309 10.458171
#> Jan 2006 18.40507 10.598431
#> Feb 2006 17.45471  9.403776
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 15.72942  9.514972
#> Jun 2005 14.09346  8.458399
#> Jul 2005 18.25350 10.789118
#> Aug 2005 17.97007 11.672310
#> Sep 2005 13.23870  8.172901
#> Oct 2005 15.38918 10.437795
#> Nov 2005 14.13069  9.070793
#> Dec 2005 10.83028  6.903871
#> Jan 2006 14.01583  8.671829
#> Feb 2006 17.93777 10.714967
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 17.72106 10.872683
#> Jun 2005 14.88347  8.941699
#> Jul 2005 16.30249  9.738921
#> Aug 2005 14.81684  8.780448
#> Sep 2005 14.68504  8.613253
#> Oct 2005 15.71413  9.260283
#> Nov 2005 17.72422 10.022207
#> Dec 2005 19.66011 11.586327
#> Jan 2006 17.02992  9.157579
#> Feb 2006 20.63057 12.127529
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 12.17858  7.404155
#> Jun 2005 16.69438  9.967782
#> Jul 2005 18.79987 11.439357
#> Aug 2005 15.02971  9.083331
#> Sep 2005 14.87959  8.973301
#> Oct 2005 14.62332  8.894779
#> Nov 2005 15.10973  8.957506
#> Dec 2005 18.94525 11.472298
#> Jan 2006 19.23483 11.408549
#> Feb 2006 18.62945 10.815449
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 15.87497  9.639481
#> Jun 2005 16.27445 10.112087
#> Jul 2005 11.86508  7.401519
#> Aug 2005 14.58401  8.866675
#> Sep 2005 15.71663  9.728297
#> Oct 2005 13.97101  8.768716
#> Nov 2005 16.67898 10.212294
#> Dec 2005 15.91397 10.063626
#> Jan 2006 15.12939  9.691796
#> Feb 2006 11.21637  7.185258
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 16.20933  9.248692
#> Jun 2005 17.04391  9.825523
#> Jul 2005 19.30979 11.899881
#> Aug 2005 16.84484 10.247832
#> Sep 2005 14.78831  9.037350
#> Oct 2005 14.18791  8.715208
#> Nov 2005 15.20932  8.897974
#> Dec 2005 18.19603 10.650764
#> Jan 2006 18.56289 10.934366
#> Feb 2006 20.29570 12.445974
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 17.72106 10.872683
#> Jun 2005 16.11642  9.726278
#> Jul 2005 15.68071 10.246019
#> Aug 2005 12.06781  7.948164
#> Sep 2005 14.92835  9.304370
#> Oct 2005 13.30382  8.105049
#> Nov 2005 14.91919  8.993319
#> Dec 2005 15.52932  9.381070
#> Jan 2006 13.98840  8.664306
#> Feb 2006 17.44368 11.101247
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.61594  8.657718
#> Jun 2005 15.46367  9.272312
#> Jul 2005 16.19348  9.995896
#> Aug 2005 15.58329  9.977242
#> Sep 2005 14.00118  9.245992
#> Oct 2005  9.84755  6.500272
#> Nov 2005 15.00607  9.664711
#> Dec 2005 16.81473 10.389499
#> Jan 2006 14.59121  9.035259
#> Feb 2006 14.58196  8.731063
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 15.63319  9.847365
#> Jun 2005 15.00880 10.496599
#> Jul 2005 12.11165  7.940050
#> Aug 2005 14.65761  8.898576
#> Sep 2005 13.93412  8.306272
#> Oct 2005 17.11447 10.072885
#> Nov 2005 17.23442 10.579489
#> Dec 2005 14.76880  9.100092
#> Jan 2006 16.11455 10.015358
#> Feb 2006 15.53897  9.273544
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 16.46143  9.591273
#> Jun 2005 15.70312  8.585523
#> Jul 2005 16.90533  9.509479
#> Aug 2005 17.70462 10.082533
#> Sep 2005 18.24802 11.306063
#> Oct 2005 16.39132  9.827007
#> Nov 2005 17.21848 10.586622
#> Dec 2005 16.28371 10.114332
#> Jan 2006 16.75483 10.210257
#> Feb 2006 18.69041 11.522023
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes    TV.advert
#> 2002  1.67535261  1.600318363
#> 2003 -0.09454667  0.059245589
#> 2004 -0.61710349 -0.617562341
#> 2005  1.55432930  1.431543276
#> 2006 -1.65830176 -1.201288646
#> 2007 -3.27721670 -1.999680352
#> 2008 -0.83986146 -0.746117612
#> 2009  1.04485279  0.291750330
#> 2010  0.75352722 -0.155143365
#> 2011  0.26745241  0.004965096
#> 2012  0.43157027  0.102861987
#> 2013  1.13693270  0.494399368
#> 2014  0.49053241  0.135721074
#> 2015 -0.10253286 -0.465058629
#> 2016 -1.85353900 -1.312388835
#> 2017 -0.33001834  0.475005003
#> 2018  2.16635830  1.749815332
#> 2019  0.17739319  0.443529161
#> 2020  0.27361734  0.111136601
#> 2021 -0.58111949 -0.379519019
#> 2022 -2.30767070 -1.246635836
#> 2023 -0.19353452  0.176564889
#> 2024  1.00563207  0.187437814
#> 2025  0.41917089  0.235645632
#> 2026  0.52295932  1.364524163
#> 2027 -0.95933919 -0.673442252
#> 2028 -1.56353647 -0.843576594
#> 2029  2.26526358  1.468847972
#> 2030  1.69164304  0.697529740
#> 2031  2.01008148  1.118934668
#> 2032 -0.38235687 -0.261094013
#> 2033 -1.39361339 -0.785256869
#> 2034 -1.33394033 -0.789489620
#> 2035 -0.37279705 -0.008190995
#> 2036 -1.43004502 -1.625855697
#> 2037  0.16351013  0.242165460
#> 2038  2.64510868  1.356646561
#> 2039  0.59781674  0.490409659
#> 2040 -2.00203118 -1.128697062
#> 
#> $loocv
#> [1] 1.243869
#> 
#> $weighted_loocv
#> [1] 2.484213e-17
#> 
#> $loocv_per_series
#>       Quotes    TV.advert       xreg_1 
#> 2.556419e+00 1.175162e+00 2.765868e-05 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res5$residuals))
#>      Quotes          TV.advert      
#>  Min.   :-3.2772   Min.   :-1.9997  
#>  1st Qu.:-0.8996   1st Qu.:-0.7098  
#>  Median : 0.1635   Median : 0.1029  
#>  Mean   : 0.0000   Mean   : 0.0000  
#>  3rd Qu.: 0.8796   3rd Qu.: 0.4827  
#>  Max.   : 2.6451   Max.   : 1.7498

print(apply(res5$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.1553145 0.4247378

print(apply(res5$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9498845 0.5141136

plot(res5, "Quotes")

plot(res5, "TV.advert")

2 - 3 With external regressors and K-Means clustering

(res6 <- ahead::ridge2f(fpp::insurance, xreg = xreg, 
                        centers = centers, 
                        type_pi = "bootstrap", B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 17.07501 10.388766
#> Jun 2005 15.43253  9.426450
#> Jul 2005 16.06612 10.032875
#> Aug 2005 15.55843  9.781757
#> Sep 2005 15.38803  9.561312
#> Oct 2005 14.29577  8.922294
#> Nov 2005 15.14111  9.437209
#> Dec 2005 15.62042  9.786917
#> Jan 2006 15.07962  9.427875
#> Feb 2006 15.75082  9.817742
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 14.93238  9.062999
#> Jun 2005 12.69504  7.635103
#> Jul 2005 12.02952  7.593689
#> Aug 2005 10.84778  7.172965
#> Sep 2005 11.62026  7.728688
#> Oct 2005 10.99568  6.889067
#> Nov 2005 11.91647  7.224795
#> Dec 2005 12.52659  8.282338
#> Jan 2006 12.09162  7.762180
#> Feb 2006 12.45194  7.679870
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 18.18986  11.34548
#> Jun 2005 18.44062  11.17688
#> Jul 2005 20.85434  13.21411
#> Aug 2005 20.12044  12.47538
#> Sep 2005 18.78197  11.33696
#> Oct 2005 17.49535  10.54290
#> Nov 2005 17.84692  10.68494
#> Dec 2005 19.52352  12.08598
#> Jan 2006 18.63094  11.31156
#> Feb 2006 18.26613  11.54401
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 17.25619 10.044030
#> Jun 2005 18.73895 11.413493
#> Jul 2005 21.59912 13.829240
#> Aug 2005 20.67822 12.672639
#> Sep 2005 19.07756 10.732739
#> Oct 2005 17.87501 10.108171
#> Nov 2005 16.07176  9.649668
#> Dec 2005 15.56584  9.027154
#> Jan 2006 18.72426 11.502984
#> Feb 2006 14.86814  8.268011
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 16.79008 10.383108
#> Jun 2005 12.31255  7.295241
#> Jul 2005 18.28898 10.708656
#> Aug 2005 18.07230 11.795922
#> Sep 2005 17.08844 10.800062
#> Oct 2005 15.26409 10.651944
#> Nov 2005 15.74207 10.320349
#> Dec 2005 12.63185  8.281412
#> Jan 2006 13.12904  8.348276
#> Feb 2006 18.09217 10.991725
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 18.18986 11.345483
#> Jun 2005 15.29909  9.330493
#> Jul 2005 16.43677 10.181040
#> Aug 2005 13.17118  8.044474
#> Sep 2005 13.86599  8.221628
#> Oct 2005 14.73454  8.849249
#> Nov 2005 18.29295 10.693085
#> Dec 2005 20.34044 12.591561
#> Jan 2006 18.30950 10.435445
#> Feb 2006 18.31664 11.315339
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 14.50946  8.857873
#> Jun 2005 14.82960  9.079181
#> Jul 2005 17.61633 11.095309
#> Aug 2005 12.92689  8.308982
#> Sep 2005 15.69561  9.844969
#> Oct 2005 13.68268  8.697224
#> Nov 2005 11.38714  6.919177
#> Dec 2005 16.45313 10.221813
#> Jan 2006 14.81343  9.285194
#> Feb 2006 17.37824 10.916374
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 17.49647 10.769539
#> Jun 2005 14.85714  9.099847
#> Jul 2005 11.78886  7.254220
#> Aug 2005 15.23442  9.316137
#> Sep 2005 13.77734  8.539092
#> Oct 2005 14.95766  9.428613
#> Nov 2005 13.89406  8.729591
#> Dec 2005 15.64772 10.212206
#> Jan 2006 14.16236  9.215672
#> Feb 2006 13.14627  8.432472
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 17.25619 10.044030
#> Jun 2005 17.41304 10.361876
#> Jul 2005 17.29974 10.956613
#> Aug 2005 17.04012 10.969462
#> Sep 2005 12.72317  8.123417
#> Oct 2005 12.03883  7.574503
#> Nov 2005 13.73972  8.277479
#> Dec 2005 16.65257 10.040701
#> Jan 2006 16.93919 10.522729
#> Feb 2006 16.41777 10.798443
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 18.18986 11.345483
#> Jun 2005 15.96543  9.870816
#> Jul 2005 12.85845  8.762974
#> Aug 2005 10.24417  6.919946
#> Sep 2005 15.60678 10.013383
#> Oct 2005 10.94730  6.819761
#> Nov 2005 14.70745  8.977825
#> Dec 2005 16.70968 10.344554
#> Jan 2006 12.14156  7.877396
#> Feb 2006 17.71229 11.610400
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 16.38910  9.769544
#> Jun 2005 15.07803  9.235066
#> Jul 2005 14.90282  9.239625
#> Aug 2005 16.56806 10.758096
#> Sep 2005 11.30006  7.614089
#> Oct 2005 11.16230  7.127788
#> Nov 2005 16.31061 10.656895
#> Dec 2005 16.03564 10.097145
#> Jan 2006 12.07712  7.728730
#> Feb 2006 13.35216  8.037539
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 16.95299 10.808949
#> Jun 2005 14.01249  9.772747
#> Jul 2005 14.16247  9.128365
#> Aug 2005 13.44972  8.252735
#> Sep 2005 16.98154 10.230916
#> Oct 2005 16.18762  9.798373
#> Nov 2005 16.14857 10.428086
#> Dec 2005 12.49604  8.285530
#> Jan 2006 13.42923  8.710094
#> Feb 2006 12.25036  7.576030
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 17.71992 10.519621
#> Jun 2005 15.81894  8.805736
#> Jul 2005 15.70763  9.172703
#> Aug 2005 18.19918 10.779171
#> Sep 2005 17.76383 11.492828
#> Oct 2005 16.10768 10.167317
#> Nov 2005 15.11675  9.719931
#> Dec 2005 13.67129  8.767092
#> Jan 2006 17.07047 10.652228
#> Feb 2006 15.97413 10.231085
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.11874618  1.19921616
#> 2003  0.09390745  0.01901986
#> 2004 -1.43434322 -1.16781389
#> 2005  1.27193197  1.24470742
#> 2006 -1.49065784 -1.21723396
#> 2007 -2.34261676 -1.51417390
#> 2008 -0.46298299 -0.60250287
#> 2009  1.63235318  0.57215494
#> 2010  0.40411221 -0.32801633
#> 2011  0.48277892  0.18055867
#> 2012  0.57087239  0.21240486
#> 2013  1.17734665  0.57331263
#> 2014  0.11531808  0.03121107
#> 2015 -0.42672324 -0.52697371
#> 2016 -1.85435793 -1.23203209
#> 2017 -0.03066633  0.70089111
#> 2018  2.03550258  1.71783757
#> 2019  0.10090940  0.43690274
#> 2020 -0.06200294  0.01106144
#> 2021  0.13620873  0.06072585
#> 2022 -1.89082466 -0.95683806
#> 2023 -0.59474544  0.01552906
#> 2024  0.86784469  0.14757423
#> 2025  0.64438935  0.39749239
#> 2026  1.21235947  1.79794968
#> 2027 -0.72834160 -0.52926182
#> 2028 -2.25083381 -1.18159761
#> 2029  1.33778080  0.97343623
#> 2030  1.97229999  0.89921107
#> 2031  0.98424529  0.52904493
#> 2032 -0.10842601 -0.11619745
#> 2033 -2.24369150 -1.28978017
#> 2034 -0.89142004 -0.54783127
#> 2035 -0.04783668  0.13304638
#> 2036 -1.11899454 -1.45036144
#> 2037  0.60160639  0.35230619
#> 2038  2.54250721  1.21587964
#> 2039  0.43166441  0.33684394
#> 2040 -1.75521983 -1.09770352
#> 
#> $loocv
#> [1] 0.7972857
#> 
#> $weighted_loocv
#> [1] 3.031318e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert        xreg_1 xreg_cluster1 xreg_cluster2 
#>  2.444624e+00  1.186564e+00  2.824183e-05  1.776061e-01  1.776061e-01 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res6$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-2.3426   Min.   :-1.51417  
#>  1st Qu.:-0.8099   1st Qu.:-0.57517  
#>  Median : 0.1009   Median : 0.06073  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9260   3rd Qu.: 0.55060  
#>  Max.   : 2.5425   Max.   : 1.79795

print(apply(res6$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.2913532 0.6500526

print(apply(res6$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.4150247 0.2507454

plot(res6, "Quotes")

plot(res6, "TV.advert")

2 - 4 With K-Means clustering

centers <- 2L
(res11 <- ahead::ridge2f(fpp::insurance, 
                         centers = centers, 
                         type_pi = "bootstrap", B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.31666  8.479435
#> Jun 2005 14.25565  8.154158
#> Jul 2005 15.32783  8.849378
#> Aug 2005 14.97984  8.637996
#> Sep 2005 14.41556  8.203976
#> Oct 2005 14.04290  7.945506
#> Nov 2005 15.13941  8.638793
#> Dec 2005 15.89219  9.179574
#> Jan 2006 15.94669  9.254438
#> Feb 2006 15.77654  9.120208
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 11.23269  6.730729
#> Jun 2005 11.95664  6.225578
#> Jul 2005 12.06328  6.933796
#> Aug 2005 13.46931  6.740782
#> Sep 2005 11.79276  6.411252
#> Oct 2005 11.77875  6.409959
#> Nov 2005 12.60936  7.454808
#> Dec 2005 10.86506  6.316466
#> Jan 2006 12.03038  7.018978
#> Feb 2006 11.90936  7.053172
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.45780  9.997944
#> Jun 2005 15.83645  9.396756
#> Jul 2005 17.29322 10.338273
#> Aug 2005 17.19264 10.697252
#> Sep 2005 18.31322 10.266108
#> Oct 2005 17.38426  9.911348
#> Nov 2005 18.14288 10.850637
#> Dec 2005 20.46698 11.798062
#> Jan 2006 19.23890 10.905728
#> Feb 2006 19.93362 11.471999
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 15.44244  8.971895
#> Jul 2005 16.65242 10.398943
#> Aug 2005 13.76268  8.828618
#> Sep 2005 11.75347  6.747740
#> Oct 2005 12.05234  6.685820
#> Nov 2005 12.45439  7.763001
#> Dec 2005 13.29424  7.594640
#> Jan 2006 13.58044  7.687717
#> Feb 2006 13.42123  7.263689
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 14.05871  8.541413
#> Jun 2005 11.58674  5.895681
#> Jul 2005 17.36302 10.129300
#> Aug 2005 16.83803 10.749865
#> Sep 2005 12.00571  7.770114
#> Oct 2005 13.19773  8.953938
#> Nov 2005 13.14312  7.992185
#> Dec 2005 10.15981  5.945383
#> Jan 2006 14.22426  8.712856
#> Feb 2006 17.53565 10.402912
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 16.45780  9.997944
#> Jun 2005 13.51019  7.743635
#> Jul 2005 14.60094  7.899539
#> Aug 2005 13.65309  6.468222
#> Sep 2005 13.83630  6.387669
#> Oct 2005 15.24162  7.578134
#> Nov 2005 17.97593  8.817839
#> Dec 2005 21.01367 12.053274
#> Jan 2006 16.67699  9.595067
#> Feb 2006 15.53215  9.407233
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 10.74483  6.500090
#> Jun 2005 14.67264  8.833011
#> Jul 2005 16.82310  9.876155
#> Aug 2005 14.18025  8.028508
#> Sep 2005 13.53450  7.662989
#> Oct 2005 13.92479  7.693246
#> Nov 2005 13.94236  7.368910
#> Dec 2005 17.61862 10.054156
#> Jan 2006 19.26549 11.092494
#> Feb 2006 16.83597 10.007784
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 14.80227  8.942430
#> Jun 2005 15.95084  9.271505
#> Jul 2005 12.57431  6.816889
#> Aug 2005 15.14212  8.361352
#> Sep 2005 18.35558 10.352236
#> Oct 2005 16.16587  9.822699
#> Nov 2005 17.36944 10.147720
#> Dec 2005 15.38180  9.415355
#> Jan 2006 16.56284  9.978913
#> Feb 2006 11.47043  6.992055
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 14.57545  7.847502
#> Jul 2005 17.05279  9.871072
#> Aug 2005 17.29559 10.516029
#> Sep 2005 14.32197  8.573326
#> Oct 2005 13.88101  7.767504
#> Nov 2005 14.26019  7.750676
#> Dec 2005 16.62553  8.611713
#> Jan 2006 19.14733 10.262425
#> Feb 2006 20.62981 11.782378
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 16.45780  9.997944
#> Jun 2005 14.93806  8.718365
#> Jul 2005 14.85002  9.109522
#> Aug 2005 13.41595  7.972349
#> Sep 2005 16.11198  9.853527
#> Oct 2005 12.03373  6.614444
#> Nov 2005 13.54604  7.913295
#> Dec 2005 13.30479  7.682164
#> Jan 2006 11.58037  6.824828
#> Feb 2006 14.71056  8.799706
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 12.91307  7.525153
#> Jun 2005 13.23075  7.361891
#> Jul 2005 16.70298  9.709794
#> Aug 2005 15.28784  9.344990
#> Sep 2005 14.14065  8.230223
#> Oct 2005 11.70473  6.350592
#> Nov 2005 15.26251  9.033212
#> Dec 2005 18.58394 10.919000
#> Jan 2006 15.79686  9.100816
#> Feb 2006 14.80357  7.907821
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 13.72125  8.509620
#> Jun 2005 13.88374  9.433119
#> Jul 2005 11.91491  7.336475
#> Aug 2005 14.24248  8.430432
#> Sep 2005 11.92810  6.492484
#> Oct 2005 14.48919  8.051597
#> Nov 2005 15.24881  8.546381
#> Dec 2005 15.38240  8.756149
#> Jan 2006 17.29359  9.880730
#> Feb 2006 16.20881  8.726606
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 14.76563  7.464978
#> Jul 2005 14.74379  7.346095
#> Aug 2005 15.98038  7.679598
#> Sep 2005 18.16731  9.969448
#> Oct 2005 17.73799  9.937085
#> Nov 2005 18.19135 11.054709
#> Dec 2005 17.55707 10.763910
#> Jan 2006 15.33870  9.408533
#> Feb 2006 16.61721  9.911892
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res11$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res11$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

print(apply(res11$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

plot(res11, "Quotes")

plot(res11, "TV.advert")

3 - Prediction intervals based on block bootstrap

3 - 1 With default parameters

block_length <- 5L
(res7 <- ahead::ridge2f(fpp::insurance, 
                        type_pi = "blockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 15.13318  9.457489
#> Jun 2005 13.06337  8.059138
#> Jul 2005 12.29234  7.795193
#> Aug 2005 12.73193  7.997832
#> Sep 2005 12.95504  7.942264
#> Oct 2005 13.33709  8.048628
#> Nov 2005 14.27552  8.641482
#> Dec 2005 13.62071  8.193504
#> Jan 2006 13.64814  8.365103
#> Feb 2006 13.39110  8.073276
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 13.640946  8.306569
#> Jun 2005 10.252219  6.779478
#> Jul 2005  9.527768  6.292099
#> Aug 2005 10.764155  6.980176
#> Sep 2005 10.343459  6.313223
#> Oct 2005 10.244834  6.218023
#> Nov 2005 12.441824  8.122031
#> Dec 2005  9.709844  5.664417
#> Jan 2006  8.779085  5.462274
#> Feb 2006  8.997235  5.866455
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.66178 10.256485
#> Jun 2005 15.70532  9.203036
#> Jul 2005 15.91362 10.524887
#> Aug 2005 15.81746  9.412302
#> Sep 2005 17.83907 10.571785
#> Oct 2005 17.24635 10.231500
#> Nov 2005 15.92240  9.537640
#> Dec 2005 17.98942 10.281448
#> Jan 2006 19.43840 11.082768
#> Feb 2006 19.10688 11.037584
#> 
#> $sims
#> $sims[[1]]
#>             Quotes TV.advert
#> May 2005 14.961639  9.917500
#> Jun 2005 12.743466  8.424202
#> Jul 2005 10.975130  7.368483
#> Aug 2005 10.575207  6.859284
#> Sep 2005 10.219173  6.588731
#> Oct 2005 10.184934  6.103088
#> Nov 2005 13.689388  8.851325
#> Dec 2005  9.389090  5.325269
#> Jan 2006  8.593317  5.164489
#> Feb 2006  9.911279  5.838859
#> 
#> $sims[[2]]
#>             Quotes TV.advert
#> May 2005 14.923084 10.170358
#> Jun 2005 10.252219  6.779478
#> Jul 2005  9.527768  6.292099
#> Aug 2005 13.169226  8.421625
#> Sep 2005 14.211894  8.687393
#> Oct 2005 14.800152  8.720328
#> Nov 2005 15.592098  8.705125
#> Dec 2005 16.399773  9.292347
#> Jan 2006 16.353486  9.145230
#> Feb 2006 17.237784  9.606621
#> 
#> $sims[[3]]
#>             Quotes TV.advert
#> May 2005 14.923084 10.170358
#> Jun 2005 10.252219  6.779478
#> Jul 2005  9.527768  6.292099
#> Aug 2005 13.169226  8.421625
#> Sep 2005 14.211894  8.687393
#> Oct 2005 12.826568  7.584607
#> Nov 2005 14.675960  8.544418
#> Dec 2005 15.571327  8.625669
#> Jan 2006 16.417689  9.255064
#> Feb 2006 16.383271  9.126644
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 13.59321  8.278953
#> Jun 2005 11.20004  7.117434
#> Jul 2005 11.46427  7.551714
#> Aug 2005 12.22603  7.424259
#> Sep 2005 13.55649  8.331353
#> Oct 2005 12.81698  7.949924
#> Nov 2005 13.54421  8.564323
#> Dec 2005 12.06123  6.832594
#> Jan 2006 15.68414  9.311793
#> Feb 2006 18.64480 11.054873
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 16.80524 10.181310
#> Jun 2005 14.94485  9.028306
#> Jul 2005 13.57398  8.380690
#> Aug 2005 12.66011  7.870669
#> Sep 2005 13.41813  8.498107
#> Oct 2005 12.95384  7.940495
#> Nov 2005 13.59121  8.033005
#> Dec 2005 13.35516  8.005734
#> Jan 2006 14.59959  9.748521
#> Feb 2006 12.42439  8.281098
#> 
#> $sims[[6]]
#>             Quotes TV.advert
#> May 2005 15.729535  9.142963
#> Jun 2005 15.705318  9.203036
#> Jul 2005 15.913618 10.524887
#> Aug 2005 11.576221  7.396579
#> Sep 2005 10.771554  6.936910
#> Oct 2005 12.588134  7.571416
#> Nov 2005 13.803821  8.428673
#> Dec 2005 14.228496  9.854274
#> Jan 2006  9.606171  6.487977
#> Feb 2006  8.887510  5.961508
#> 
#> $sims[[7]]
#>             Quotes TV.advert
#> May 2005 15.729535  9.142963
#> Jun 2005 15.705318  9.203036
#> Jul 2005 15.913618 10.524887
#> Aug 2005 11.576221  7.396579
#> Sep 2005 10.771554  6.936910
#> Oct 2005 10.451156  6.613910
#> Nov 2005 12.121776  8.478145
#> Dec 2005 10.814666  7.561439
#> Jan 2006  9.418952  6.567691
#> Feb 2006  9.375176  6.226668
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.69348  8.890481
#> Jun 2005 13.48778  8.217040
#> Jul 2005 11.16029  7.102348
#> Aug 2005 11.41497  7.526636
#> Sep 2005 12.18701  7.403738
#> Oct 2005 16.57467 10.085307
#> Nov 2005 15.88382  9.736893
#> Dec 2005 12.94425  8.027855
#> Jan 2006 14.79569  9.634984
#> Feb 2006 10.75217  6.322139
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 13.80537  8.401690
#> Jun 2005 14.39382  8.981247
#> Jul 2005 12.72469  7.125754
#> Aug 2005 16.09680  9.479206
#> Sep 2005 18.89212 11.118867
#> Oct 2005 17.44136 10.273943
#> Nov 2005 15.93361  8.588339
#> Dec 2005 18.45093 10.405466
#> Jan 2006 20.31538 11.470130
#> Feb 2006 19.24104 10.978036
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 16.16762 10.278310
#> Jun 2005 11.94868  6.858120
#> Jul 2005 12.14228  6.788973
#> Aug 2005 14.85531  9.181855
#> Sep 2005 11.31063  6.233237
#> Oct 2005 12.73315  7.643266
#> Nov 2005 13.91935  8.484571
#> Dec 2005 12.99215  8.004390
#> Jan 2006 10.69701  6.865152
#> Feb 2006 11.05357  7.336318
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.40368643  1.33052782
#> 2003 -1.08056990 -1.14582835
#> 2004 -1.23343504 -1.15469695
#> 2005  1.40368643  1.33052782
#> 2006 -2.58725990 -2.26486335
#> 2007 -3.07477874 -1.76651025
#> 2008 -1.31677412 -1.06553020
#> 2009  0.65681006  0.07019039
#> 2010  0.33076863 -0.40143292
#> 2011  0.51993723  0.22434076
#> 2012  0.10053432 -0.17249791
#> 2013  0.95770684  0.35761548
#> 2014  0.26825909 -0.01668861
#> 2015 -0.12839403 -0.42852687
#> 2016 -1.17137522 -0.76232506
#> 2017  0.19770654  0.96971721
#> 2018  1.62259434  1.31981494
#> 2019 -0.14704277  0.17362676
#> 2020 -0.07045343 -0.05730098
#> 2021 -1.17072203 -0.66882907
#> 2022 -2.89887258 -1.53863637
#> 2023 -0.62736155 -0.06504866
#> 2024  0.96560231  0.19518092
#> 2025 -0.09354593 -0.06951135
#> 2026  0.15915097  1.22257588
#> 2027  0.29875209  0.25719228
#> 2028 -1.31329880 -0.69076345
#> 2029  2.48926902  1.54309187
#> 2030  1.91175656  0.91165371
#> 2031  2.04130891  1.23352769
#> 2032 -0.29759583 -0.05351566
#> 2033 -1.22025180 -0.56162404
#> 2034 -0.95856611 -0.54609232
#> 2035  0.20717781  0.29095534
#> 2036 -0.70462371 -1.15899926
#> 2037  2.26007518  1.40841386
#> 2038  2.95319223  1.69957104
#> 2039  0.89317411  0.77051186
#> 2040 -1.54622760 -0.71981400
#> 
#> $loocv
#> [1] 1.924173
#> 
#> $weighted_loocv
#> [1] -2.913168e-17
#> 
#> $loocv_per_series
#>    Quotes TV.advert 
#>  2.601976  1.246370 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res7$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-3.0748   Min.   :-2.26486  
#>  1st Qu.:-1.1256   1st Qu.:-0.67980  
#>  Median : 0.1005   Median :-0.05352  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9254   3rd Qu.: 0.84108  
#>  Max.   : 2.9532   Max.   : 1.69957

print(apply(res7$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.06458106 0.32251812

print(apply(res7$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.7471878 0.3924569

plot(res7, "Quotes")

plot(res7, "TV.advert")

3 - 2 With external regressors

(res8 <- ahead::ridge2f(fpp::insurance, xreg = xreg, 
                        type_pi = "blockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 15.93292  9.927867
#> Jun 2005 14.62582  9.159010
#> Jul 2005 13.75974  8.768195
#> Aug 2005 14.37756  9.002538
#> Sep 2005 14.82859  9.086496
#> Oct 2005 15.50606  9.312723
#> Nov 2005 16.56312  9.907583
#> Dec 2005 16.46826  9.883022
#> Jan 2006 16.63819 10.087099
#> Feb 2006 17.05429 10.250023
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 14.29124  8.706589
#> Jun 2005 11.34974  7.525175
#> Jul 2005 10.98139  7.100955
#> Aug 2005 11.91583  7.837910
#> Sep 2005 12.13687  7.691967
#> Oct 2005 12.46851  7.484113
#> Nov 2005 14.81651  8.811267
#> Dec 2005 13.63208  7.894731
#> Jan 2006 12.49490  7.621327
#> Feb 2006 12.76658  8.094808
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.39057  10.95110
#> Jun 2005 17.55234  10.44702
#> Jul 2005 17.42791  11.45397
#> Aug 2005 16.72187  10.33188
#> Sep 2005 18.98099  11.22142
#> Oct 2005 18.50937  10.97776
#> Nov 2005 18.37324  10.94087
#> Dec 2005 19.72103  11.60998
#> Jan 2006 21.10967  12.08978
#> Feb 2006 21.21273  11.98270
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 15.12578  9.878841
#> Jun 2005 15.41501 10.378988
#> Jul 2005 12.54950  8.710196
#> Aug 2005 11.73579  7.789848
#> Sep 2005 12.11073  7.680743
#> Oct 2005 13.33255  7.974199
#> Nov 2005 17.34201 10.874971
#> Dec 2005 13.79355  8.378351
#> Jan 2006 12.38883  7.467185
#> Feb 2006 14.73481  8.531035
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 15.97876 10.768360
#> Jun 2005 11.34974  7.525175
#> Jul 2005 10.98139  7.100955
#> Aug 2005 15.44516  9.621484
#> Sep 2005 16.72932 10.088001
#> Oct 2005 17.51265 10.291912
#> Nov 2005 18.38565 10.321924
#> Dec 2005 19.81424 11.270619
#> Jan 2006 20.19970 11.421108
#> Feb 2006 21.35149 12.002285
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 15.97876 10.768360
#> Jun 2005 11.34974  7.525175
#> Jul 2005 10.98139  7.100955
#> Aug 2005 15.44516  9.621484
#> Sep 2005 16.72932 10.088001
#> Oct 2005 15.62793  9.254044
#> Nov 2005 17.82430 10.383988
#> Dec 2005 18.86784 10.533007
#> Jan 2006 20.29563 11.474313
#> Feb 2006 20.70262 11.633848
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 14.87468  9.024316
#> Jun 2005 13.12866  8.202981
#> Jul 2005 14.10769  8.998497
#> Aug 2005 15.00476  9.017367
#> Sep 2005 16.41816  9.940874
#> Oct 2005 14.91342  9.130982
#> Nov 2005 15.22212  9.548900
#> Dec 2005 13.58520  7.754325
#> Jan 2006 17.39954 10.286250
#> Feb 2006 20.27961 11.897792
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 17.46588 10.522770
#> Jun 2005 16.00567  9.593406
#> Jul 2005 14.80009  9.068744
#> Aug 2005 13.96074  8.614358
#> Sep 2005 14.60824  9.186582
#> Oct 2005 15.14617  9.290168
#> Nov 2005 15.48430  9.114929
#> Dec 2005 15.11850  8.843028
#> Jan 2006 16.78499 10.650305
#> Feb 2006 17.26246 11.243429
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 16.46143  9.591273
#> Jun 2005 17.55234 10.447025
#> Jul 2005 17.42791 11.453975
#> Aug 2005 12.53595  8.003457
#> Sep 2005 12.22690  7.730630
#> Oct 2005 15.07683  8.841656
#> Nov 2005 17.30371 10.303588
#> Dec 2005 17.73890 11.695926
#> Jan 2006 12.86025  8.227742
#> Feb 2006 12.63914  7.968162
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 16.46143  9.591273
#> Jun 2005 17.55234 10.447025
#> Jul 2005 17.42791 11.453975
#> Aug 2005 12.53595  8.003457
#> Sep 2005 12.22690  7.730630
#> Oct 2005 12.21766  7.341830
#> Nov 2005 15.04576  9.633535
#> Dec 2005 16.51964 10.856920
#> Jan 2006 13.96170  9.379486
#> Feb 2006 13.20556  8.565970
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 15.72942  9.514972
#> Jun 2005 15.17064  9.280168
#> Jul 2005 13.04263  8.213002
#> Aug 2005 13.96530  8.942326
#> Sep 2005 14.94964  8.987836
#> Oct 2005 18.69609 11.083351
#> Nov 2005 18.33051 10.960007
#> Dec 2005 15.39545  9.235400
#> Jan 2006 18.16634 11.572344
#> Feb 2006 15.14727  9.561710
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 14.12186  8.614346
#> Jun 2005 14.71754  9.221823
#> Jul 2005 13.10834  7.471507
#> Aug 2005 16.84335  9.976562
#> Sep 2005 19.63471 11.550480
#> Oct 2005 17.86625 10.614059
#> Nov 2005 15.94289  8.723108
#> Dec 2005 19.39999 11.313965
#> Jan 2006 21.34601 12.240000
#> Feb 2006 20.73479 11.915256
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 17.13115 11.004154
#> Jun 2005 14.01651  8.968336
#> Jul 2005 13.17051  8.110142
#> Aug 2005 16.30345 10.435035
#> Sep 2005 12.65203  7.881186
#> Oct 2005 14.67111  9.305029
#> Nov 2005 14.74995  9.210878
#> Dec 2005 14.44934  8.948683
#> Jan 2006 12.97897  8.152258
#> Feb 2006 14.48511  9.180738
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes    TV.advert
#> 2002  1.67535261  1.600318363
#> 2003 -0.09454667  0.059245589
#> 2004 -0.61710349 -0.617562341
#> 2005  1.55432930  1.431543276
#> 2006 -1.65830176 -1.201288646
#> 2007 -3.27721670 -1.999680352
#> 2008 -0.83986146 -0.746117612
#> 2009  1.04485279  0.291750330
#> 2010  0.75352722 -0.155143365
#> 2011  0.26745241  0.004965096
#> 2012  0.43157027  0.102861987
#> 2013  1.13693270  0.494399368
#> 2014  0.49053241  0.135721074
#> 2015 -0.10253286 -0.465058629
#> 2016 -1.85353900 -1.312388835
#> 2017 -0.33001834  0.475005003
#> 2018  2.16635830  1.749815332
#> 2019  0.17739319  0.443529161
#> 2020  0.27361734  0.111136601
#> 2021 -0.58111949 -0.379519019
#> 2022 -2.30767070 -1.246635836
#> 2023 -0.19353452  0.176564889
#> 2024  1.00563207  0.187437814
#> 2025  0.41917089  0.235645632
#> 2026  0.52295932  1.364524163
#> 2027 -0.95933919 -0.673442252
#> 2028 -1.56353647 -0.843576594
#> 2029  2.26526358  1.468847972
#> 2030  1.69164304  0.697529740
#> 2031  2.01008148  1.118934668
#> 2032 -0.38235687 -0.261094013
#> 2033 -1.39361339 -0.785256869
#> 2034 -1.33394033 -0.789489620
#> 2035 -0.37279705 -0.008190995
#> 2036 -1.43004502 -1.625855697
#> 2037  0.16351013  0.242165460
#> 2038  2.64510868  1.356646561
#> 2039  0.59781674  0.490409659
#> 2040 -2.00203118 -1.128697062
#> 
#> $loocv
#> [1] 1.243869
#> 
#> $weighted_loocv
#> [1] 2.484213e-17
#> 
#> $loocv_per_series
#>       Quotes    TV.advert       xreg_1 
#> 2.556419e+00 1.175162e+00 2.765868e-05 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res8$residuals))
#>      Quotes          TV.advert      
#>  Min.   :-3.2772   Min.   :-1.9997  
#>  1st Qu.:-0.8996   1st Qu.:-0.7098  
#>  Median : 0.1635   Median : 0.1029  
#>  Mean   : 0.0000   Mean   : 0.0000  
#>  3rd Qu.: 0.8796   3rd Qu.: 0.4827  
#>  Max.   : 2.6451   Max.   : 1.7498

print(apply(res8$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.1553145 0.4247378

print(apply(res8$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9498845 0.5141136

plot(res8, "Quotes")

plot(res8, "TV.advert")

3 - 3 With external regressors and K-Means clustering

(res9 <- ahead::ridge2f(fpp::insurance, 
                        xreg = xreg, centers = centers, 
                        type_pi = "blockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 17.39363 10.956462
#> Jun 2005 15.32734  9.663891
#> Jul 2005 13.01520  8.462715
#> Aug 2005 14.30795  9.183655
#> Sep 2005 13.82213  8.684953
#> Oct 2005 15.50101  9.534365
#> Nov 2005 15.19927  9.272217
#> Dec 2005 15.44275  9.525640
#> Jan 2006 17.37508 10.905782
#> Feb 2006 18.05586 11.198881
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 16.14728  9.949966
#> Jun 2005 12.52053  7.901074
#> Jul 2005 10.35541  6.462820
#> Aug 2005 10.86573  7.069793
#> Sep 2005 10.68630  7.305022
#> Oct 2005 11.54633  7.180077
#> Nov 2005 12.28864  7.933619
#> Dec 2005 12.34999  7.209061
#> Jan 2006 11.82261  7.488115
#> Feb 2006 11.06547  7.540693
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 18.06444  12.17000
#> Jun 2005 17.58232  10.65359
#> Jul 2005 16.76485  11.25645
#> Aug 2005 16.46684  10.49139
#> Sep 2005 19.02135  11.43617
#> Oct 2005 20.27107  12.16026
#> Nov 2005 18.66400  10.75354
#> Dec 2005 21.94331  13.07700
#> Jan 2006 25.06197  14.92559
#> Feb 2006 27.86401  16.22140
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 16.82141 11.072938
#> Jun 2005 15.31206 10.300731
#> Jul 2005 12.68251  8.858470
#> Aug 2005 11.16030  7.598533
#> Sep 2005 13.53445  8.785540
#> Oct 2005 11.48830  7.132788
#> Nov 2005 16.17267 10.485054
#> Dec 2005 12.33995  7.812477
#> Jan 2006 12.05871  7.459504
#> Feb 2006 13.84908  8.181968
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 18.06444 12.169996
#> Jun 2005 15.06062  9.803147
#> Jul 2005 10.35541  7.015425
#> Aug 2005 16.32757 10.491386
#> Sep 2005 14.00206  8.542838
#> Oct 2005 20.54226 12.294248
#> Nov 2005 18.69799 10.357479
#> Dec 2005 22.80289 13.605061
#> Jan 2006 25.53267 15.151924
#> Feb 2006 28.64222 16.595618
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 18.06444 12.169996
#> Jun 2005 15.06062  9.803147
#> Jul 2005 10.35541  7.015425
#> Aug 2005 16.32757 10.491386
#> Sep 2005 14.00206  8.542838
#> Oct 2005 18.44692 11.119590
#> Nov 2005 18.54694 10.831490
#> Dec 2005 17.82191 10.048336
#> Jan 2006 23.06103 13.872155
#> Feb 2006 25.18352 14.932417
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 16.98829 10.432772
#> Jun 2005 12.42416  7.827292
#> Jul 2005 14.34085  9.307761
#> Aug 2005 13.56437  8.327571
#> Sep 2005 15.25635  9.414196
#> Oct 2005 11.74621  7.342958
#> Nov 2005 14.02463  8.797739
#> Dec 2005 12.38456  7.033875
#> Jan 2006 17.06191 10.120543
#> Feb 2006 21.36329 12.846685
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 17.83632 10.901091
#> Jun 2005 17.03125 10.472043
#> Jul 2005 14.59177  9.329591
#> Aug 2005 10.78022  6.916288
#> Sep 2005 13.61707  8.672344
#> Oct 2005 13.71126  8.656449
#> Nov 2005 13.50935  8.227707
#> Dec 2005 13.97536  8.507638
#> Jan 2006 16.47141 10.886484
#> Feb 2006 14.99904 10.327371
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 17.71992 10.519621
#> Jun 2005 17.58232 10.653589
#> Jul 2005 16.76485 11.256446
#> Aug 2005 14.30530  9.522930
#> Sep 2005 10.68630  7.305022
#> Oct 2005 15.32595  9.404004
#> Nov 2005 13.60859  8.463473
#> Dec 2005 14.95386 10.338206
#> Jan 2006 13.55924  9.120954
#> Feb 2006 10.80552  7.376831
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 17.71992 10.519621
#> Jun 2005 17.58232 10.653589
#> Jul 2005 16.76485 11.256446
#> Aug 2005 14.30530  9.522930
#> Sep 2005 10.68630  7.305022
#> Oct 2005 12.60375  8.024398
#> Nov 2005 12.91387  8.802365
#> Dec 2005 13.80778  9.481290
#> Jan 2006 12.80654  8.924245
#> Feb 2006 11.96083  8.105108
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 16.79008 10.383108
#> Jun 2005 13.93942  8.573201
#> Jul 2005 11.71636  7.455115
#> Aug 2005 13.93505  9.054271
#> Sep 2005 13.57815  8.311494
#> Oct 2005 17.34450 10.337137
#> Nov 2005 15.92128  9.769700
#> Dec 2005 14.01365  8.773361
#> Jan 2006 18.00455 11.789364
#> Feb 2006 15.90175 10.303007
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 15.96066  9.824215
#> Jun 2005 12.85247  8.155213
#> Jul 2005 11.39427  6.378208
#> Aug 2005 16.50727  9.772911
#> Sep 2005 20.11441 12.023197
#> Oct 2005 19.33697 11.698746
#> Nov 2005 16.49027  9.138918
#> Dec 2005 18.98253 11.258109
#> Jan 2006 23.44069 14.145984
#> Feb 2006 23.77138 14.099723
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 17.97083 11.571263
#> Jun 2005 16.42812 10.396952
#> Jul 2005 11.18577  6.754259
#> Aug 2005 15.86655 10.138341
#> Sep 2005 12.74417  7.947039
#> Oct 2005 14.46394  9.333333
#> Nov 2005 12.10712  7.848239
#> Dec 2005 13.34502  8.398047
#> Jan 2006 11.75407  7.586662
#> Feb 2006 14.08193  9.220084
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.11874618  1.19921616
#> 2003  0.09390745  0.01901986
#> 2004 -1.43434322 -1.16781389
#> 2005  1.27193197  1.24470742
#> 2006 -1.49065784 -1.21723396
#> 2007 -2.34261676 -1.51417390
#> 2008 -0.46298299 -0.60250287
#> 2009  1.63235318  0.57215494
#> 2010  0.40411221 -0.32801633
#> 2011  0.48277892  0.18055867
#> 2012  0.57087239  0.21240486
#> 2013  1.17734665  0.57331263
#> 2014  0.11531808  0.03121107
#> 2015 -0.42672324 -0.52697371
#> 2016 -1.85435793 -1.23203209
#> 2017 -0.03066633  0.70089111
#> 2018  2.03550258  1.71783757
#> 2019  0.10090940  0.43690274
#> 2020 -0.06200294  0.01106144
#> 2021  0.13620873  0.06072585
#> 2022 -1.89082466 -0.95683806
#> 2023 -0.59474544  0.01552906
#> 2024  0.86784469  0.14757423
#> 2025  0.64438935  0.39749239
#> 2026  1.21235947  1.79794968
#> 2027 -0.72834160 -0.52926182
#> 2028 -2.25083381 -1.18159761
#> 2029  1.33778080  0.97343623
#> 2030  1.97229999  0.89921107
#> 2031  0.98424529  0.52904493
#> 2032 -0.10842601 -0.11619745
#> 2033 -2.24369150 -1.28978017
#> 2034 -0.89142004 -0.54783127
#> 2035 -0.04783668  0.13304638
#> 2036 -1.11899454 -1.45036144
#> 2037  0.60160639  0.35230619
#> 2038  2.54250721  1.21587964
#> 2039  0.43166441  0.33684394
#> 2040 -1.75521983 -1.09770352
#> 
#> $loocv
#> [1] 0.7972857
#> 
#> $weighted_loocv
#> [1] 3.031318e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert        xreg_1 xreg_cluster1 xreg_cluster2 
#>  2.444624e+00  1.186564e+00  2.824183e-05  1.776061e-01  1.776061e-01 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res9$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-2.3426   Min.   :-1.51417  
#>  1st Qu.:-0.8099   1st Qu.:-0.57517  
#>  Median : 0.1009   Median : 0.06073  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9260   3rd Qu.: 0.55060  
#>  Max.   : 2.5425   Max.   : 1.79795

print(apply(res9$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.2913532 0.6500526

print(apply(res9$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.4150247 0.2507454

plot(res9, "Quotes")

plot(res9, "TV.advert")

3 - 4 With K-Means clustering

(res12 <- ahead::ridge2f(fpp::insurance, 
                         centers = centers, 
                         type_pi = "blockbootstrap", 
                         block_length = block_length,
                         B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.51281  9.015085
#> Jun 2005 13.23118  7.958613
#> Jul 2005 12.82519  7.737483
#> Aug 2005 13.51905  8.276239
#> Sep 2005 13.62542  8.047506
#> Oct 2005 13.98444  8.116326
#> Nov 2005 14.78781  8.551834
#> Dec 2005 14.85769  8.456788
#> Jan 2006 14.95417  8.951681
#> Feb 2006 14.46327  8.860760
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 13.265492  7.992650
#> Jun 2005 10.011067  6.730118
#> Jul 2005  9.204177  5.861179
#> Aug 2005 11.973064  7.397854
#> Sep 2005 10.636133  6.193960
#> Oct 2005 10.445116  5.935517
#> Nov 2005 11.876683  7.255208
#> Dec 2005 10.265930  5.726151
#> Jan 2006  8.507776  5.625904
#> Feb 2006  8.972333  5.749044
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.33594 10.027338
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 16.79755  9.613295
#> Sep 2005 19.38790 11.284791
#> Oct 2005 18.43310 10.642500
#> Nov 2005 18.73383 10.685986
#> Dec 2005 18.11738 10.017318
#> Jan 2006 21.39913 12.165645
#> Feb 2006 22.41147 13.357304
#> 
#> $sims
#> $sims[[1]]
#>             Quotes TV.advert
#> May 2005 13.708143  9.097808
#> Jun 2005 13.823827  8.723703
#> Jul 2005 12.985556  7.775434
#> Aug 2005 13.002288  8.319168
#> Sep 2005 11.866496  7.127413
#> Oct 2005 11.510448  6.770107
#> Nov 2005 13.720577  9.022614
#> Dec 2005  9.872584  5.481126
#> Jan 2006  8.226746  5.428077
#> Feb 2006  8.661913  5.570686
#> 
#> $sims[[2]]
#>             Quotes TV.advert
#> May 2005 14.740008  9.977827
#> Jun 2005 10.011067  6.804492
#> Jul 2005  9.204177  5.861179
#> Aug 2005 13.246686  8.256440
#> Sep 2005 14.839684  8.638325
#> Oct 2005 15.631655  9.011983
#> Nov 2005 16.541170  8.472721
#> Dec 2005 18.020906 10.018941
#> Jan 2006 17.078373  9.955407
#> Feb 2006 16.392166  9.716583
#> 
#> $sims[[3]]
#>             Quotes TV.advert
#> May 2005 14.740008  9.977827
#> Jun 2005 10.011067  6.804492
#> Jul 2005  9.204177  5.861179
#> Aug 2005 13.246686  8.256440
#> Sep 2005 14.839684  8.638325
#> Oct 2005 13.660979  7.898109
#> Nov 2005 15.506526  8.218883
#> Dec 2005 17.067402  8.785388
#> Jan 2006 18.227690 10.460169
#> Feb 2006 16.289102  9.816177
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 13.22573  7.968126
#> Jun 2005 11.18621  6.708525
#> Jul 2005 12.05381  7.414828
#> Aug 2005 13.43576  7.497384
#> Sep 2005 15.38119  8.639415
#> Oct 2005 15.38756  8.490611
#> Nov 2005 17.10059  9.536766
#> Dec 2005 16.57768  8.263104
#> Jan 2006 21.23660 11.215624
#> Feb 2006 23.43130 13.838238
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 16.59331 10.041712
#> Jun 2005 14.65011  8.987163
#> Jul 2005 12.88358  8.196931
#> Aug 2005 11.88947  7.415110
#> Sep 2005 12.78137  8.239067
#> Oct 2005 12.37170  7.570947
#> Nov 2005 12.55772  7.107249
#> Dec 2005 12.01519  6.570126
#> Jan 2006 13.65345  8.300098
#> Feb 2006 15.70388  9.272431
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 12.59529  6.898842
#> Nov 2005 14.72629  8.516219
#> Dec 2005 15.79601 10.011731
#> Jan 2006 11.78220  7.555532
#> Feb 2006 10.65551  6.363389
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 10.13583  5.693216
#> Nov 2005 11.70276  7.901233
#> Dec 2005 13.44827  8.585715
#> Jan 2006 12.66159  7.604806
#> Feb 2006 12.68982  8.254080
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.05871  8.541413
#> Jun 2005 13.06677  7.477091
#> Jul 2005 11.35088  6.837713
#> Aug 2005 12.26101  7.392845
#> Sep 2005 13.72248  7.598158
#> Oct 2005 18.69996 10.457618
#> Nov 2005 19.20800 11.019630
#> Dec 2005 16.01270  9.630958
#> Jan 2006 15.75294 10.248332
#> Feb 2006 11.86871  7.214686
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 13.40246  8.077121
#> Jun 2005 14.35696  8.591234
#> Jul 2005 13.32299  7.043137
#> Aug 2005 17.40934  9.718443
#> Sep 2005 20.55114 12.052804
#> Oct 2005 17.51390 10.696175
#> Nov 2005 14.33869  7.764844
#> Dec 2005 18.14539  9.894546
#> Jan 2006 21.44632 12.441457
#> Feb 2006 18.89871 11.700751
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 15.44948  9.719358
#> Jun 2005 12.71580  7.341674
#> Jul 2005 12.38966  7.090157
#> Aug 2005 14.69030  9.251118
#> Sep 2005 10.99989  6.030998
#> Oct 2005 12.33706  7.675653
#> Nov 2005 12.47576  7.958183
#> Dec 2005 11.62079  7.326245
#> Jan 2006  9.47577  6.307308
#> Feb 2006 10.04156  6.860583
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res12$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res12$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

print(apply(res12$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

plot(res12, "Quotes")

plot(res12, "TV.advert")

3 - 5 Using the median instead of the mean in bootstrap aggregation

(res13 <- ahead::ridge2f(fpp::insurance, 
                         centers = centers, 
                         type_pi = "blockbootstrap", 
                         type_aggregation = "median",
                         block_length = block_length,
                         B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.60513  8.819610
#> Jun 2005 13.44530  8.034163
#> Jul 2005 12.63662  7.252493
#> Aug 2005 13.12559  8.287804
#> Sep 2005 13.25193  7.918613
#> Oct 2005 13.12813  7.786881
#> Nov 2005 14.53249  8.345802
#> Dec 2005 15.90436  8.685551
#> Jan 2006 14.70320  9.127752
#> Feb 2006 14.19685  8.763255
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 13.265492  7.992650
#> Jun 2005 10.011067  6.730118
#> Jul 2005  9.204177  5.861179
#> Aug 2005 11.973064  7.397854
#> Sep 2005 10.636133  6.193960
#> Oct 2005 10.445116  5.935517
#> Nov 2005 11.876683  7.255208
#> Dec 2005 10.265930  5.726151
#> Jan 2006  8.507776  5.625904
#> Feb 2006  8.972333  5.749044
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.33594 10.027338
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 16.79755  9.613295
#> Sep 2005 19.38790 11.284791
#> Oct 2005 18.43310 10.642500
#> Nov 2005 18.73383 10.685986
#> Dec 2005 18.11738 10.017318
#> Jan 2006 21.39913 12.165645
#> Feb 2006 22.41147 13.357304
#> 
#> $sims
#> $sims[[1]]
#>             Quotes TV.advert
#> May 2005 13.708143  9.097808
#> Jun 2005 13.823827  8.723703
#> Jul 2005 12.985556  7.775434
#> Aug 2005 13.002288  8.319168
#> Sep 2005 11.866496  7.127413
#> Oct 2005 11.510448  6.770107
#> Nov 2005 13.720577  9.022614
#> Dec 2005  9.872584  5.481126
#> Jan 2006  8.226746  5.428077
#> Feb 2006  8.661913  5.570686
#> 
#> $sims[[2]]
#>             Quotes TV.advert
#> May 2005 14.740008  9.977827
#> Jun 2005 10.011067  6.804492
#> Jul 2005  9.204177  5.861179
#> Aug 2005 13.246686  8.256440
#> Sep 2005 14.839684  8.638325
#> Oct 2005 15.631655  9.011983
#> Nov 2005 16.541170  8.472721
#> Dec 2005 18.020906 10.018941
#> Jan 2006 17.078373  9.955407
#> Feb 2006 16.392166  9.716583
#> 
#> $sims[[3]]
#>             Quotes TV.advert
#> May 2005 14.740008  9.977827
#> Jun 2005 10.011067  6.804492
#> Jul 2005  9.204177  5.861179
#> Aug 2005 13.246686  8.256440
#> Sep 2005 14.839684  8.638325
#> Oct 2005 13.660979  7.898109
#> Nov 2005 15.506526  8.218883
#> Dec 2005 17.067402  8.785388
#> Jan 2006 18.227690 10.460169
#> Feb 2006 16.289102  9.816177
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 13.22573  7.968126
#> Jun 2005 11.18621  6.708525
#> Jul 2005 12.05381  7.414828
#> Aug 2005 13.43576  7.497384
#> Sep 2005 15.38119  8.639415
#> Oct 2005 15.38756  8.490611
#> Nov 2005 17.10059  9.536766
#> Dec 2005 16.57768  8.263104
#> Jan 2006 21.23660 11.215624
#> Feb 2006 23.43130 13.838238
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 16.59331 10.041712
#> Jun 2005 14.65011  8.987163
#> Jul 2005 12.88358  8.196931
#> Aug 2005 11.88947  7.415110
#> Sep 2005 12.78137  8.239067
#> Oct 2005 12.37170  7.570947
#> Nov 2005 12.55772  7.107249
#> Dec 2005 12.01519  6.570126
#> Jan 2006 13.65345  8.300098
#> Feb 2006 15.70388  9.272431
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 12.59529  6.898842
#> Nov 2005 14.72629  8.516219
#> Dec 2005 15.79601 10.011731
#> Jan 2006 11.78220  7.555532
#> Feb 2006 10.65551  6.363389
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 10.13583  5.693216
#> Nov 2005 11.70276  7.901233
#> Dec 2005 13.44827  8.585715
#> Jan 2006 12.66159  7.604806
#> Feb 2006 12.68982  8.254080
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.05871  8.541413
#> Jun 2005 13.06677  7.477091
#> Jul 2005 11.35088  6.837713
#> Aug 2005 12.26101  7.392845
#> Sep 2005 13.72248  7.598158
#> Oct 2005 18.69996 10.457618
#> Nov 2005 19.20800 11.019630
#> Dec 2005 16.01270  9.630958
#> Jan 2006 15.75294 10.248332
#> Feb 2006 11.86871  7.214686
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 13.40246  8.077121
#> Jun 2005 14.35696  8.591234
#> Jul 2005 13.32299  7.043137
#> Aug 2005 17.40934  9.718443
#> Sep 2005 20.55114 12.052804
#> Oct 2005 17.51390 10.696175
#> Nov 2005 14.33869  7.764844
#> Dec 2005 18.14539  9.894546
#> Jan 2006 21.44632 12.441457
#> Feb 2006 18.89871 11.700751
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 15.44948  9.719358
#> Jun 2005 12.71580  7.341674
#> Jul 2005 12.38966  7.090157
#> Aug 2005 14.69030  9.251118
#> Sep 2005 10.99989  6.030998
#> Oct 2005 12.33706  7.675653
#> Nov 2005 12.47576  7.958183
#> Dec 2005 11.62079  7.326245
#> Jan 2006  9.47577  6.307308
#> Feb 2006 10.04156  6.860583
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(apply(res13$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

print(summary(res13$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res13$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

plot(res13, "Quotes")

plot(res13, "TV.advert")

4 - Prediction intervals based on moving block bootstrap

4 - 1 With default parameters

block_length <- 5L
(res16 <- ahead::ridge2f(fpp::insurance, 
                        type_pi = "movingblockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.95424  8.917110
#> Jun 2005 15.06970  8.993896
#> Jul 2005 14.90589  8.905918
#> Aug 2005 14.25561  8.465204
#> Sep 2005 14.36862  8.515677
#> Oct 2005 13.81543  8.178355
#> Nov 2005 13.10715  7.889358
#> Dec 2005 13.34434  8.031641
#> Jan 2006 13.95208  8.361007
#> Feb 2006 14.31399  8.419853
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 12.084705  7.338993
#> Jun 2005 12.163965  7.321763
#> Jul 2005 10.414064  6.835108
#> Aug 2005  9.643880  6.345469
#> Sep 2005 10.732927  6.987392
#> Oct 2005  9.575235  6.024506
#> Nov 2005  7.941691  5.525525
#> Dec 2005  7.724104  5.197418
#> Jan 2006 11.224315  6.593768
#> Feb 2006 11.875286  6.734046
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 17.12849 10.070346
#> Jul 2005 18.57187 10.842775
#> Aug 2005 17.07368  9.921982
#> Sep 2005 16.90692  9.425962
#> Oct 2005 17.73942 10.288472
#> Nov 2005 16.99782  9.979098
#> Dec 2005 17.85605 10.725460
#> Jan 2006 17.06614 10.187037
#> Feb 2006 17.62241 10.423499
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 15.09470  8.546350
#> Jun 2005 16.08327  9.235840
#> Jul 2005 16.13305  9.130626
#> Aug 2005 17.07368  9.608318
#> Sep 2005 16.90692  9.425962
#> Oct 2005 16.29015  9.308269
#> Nov 2005 14.94118  8.768067
#> Dec 2005 14.39134  8.577792
#> Jan 2006 15.21351  9.345297
#> Feb 2006 13.52463  7.488525
#> 
#> $sims[[2]]
#>             Quotes TV.advert
#> May 2005 14.693479  8.890481
#> Jun 2005 13.487777  8.217040
#> Jul 2005 11.160285  7.102348
#> Aug 2005 11.414974  7.526636
#> Sep 2005 12.187011  7.403738
#> Oct 2005 11.034216  6.120872
#> Nov 2005  9.636303  5.642301
#> Dec 2005 10.564867  6.117515
#> Jan 2006 13.134670  7.546796
#> Feb 2006 14.491503  7.991768
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 17.12849  9.960231
#> Jul 2005 18.57187 10.842775
#> Aug 2005 16.97110  9.921982
#> Sep 2005 15.20504  9.035494
#> Oct 2005 15.73824  9.498906
#> Nov 2005 13.76043  8.355235
#> Dec 2005 16.74295 10.268957
#> Jan 2006 16.61087  9.724574
#> Feb 2006 18.27261 10.750198
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 11.68915  7.181272
#> Jun 2005 11.80378  7.061844
#> Jul 2005 14.01943  8.221683
#> Aug 2005 15.18397  8.487453
#> Sep 2005 16.17743  9.204073
#> Oct 2005 18.16018 10.517700
#> Nov 2005 16.79192  9.782343
#> Dec 2005 15.12627  8.975603
#> Jan 2006 14.44565  8.670010
#> Feb 2006 15.08582  9.298202
#> 
#> $sims[[5]]
#>             Quotes TV.advert
#> May 2005 14.670387  8.878271
#> Jun 2005 14.803293 10.102315
#> Jul 2005 10.197419  6.757522
#> Aug 2005  9.463849  6.259460
#> Sep 2005 13.119058  8.395221
#> Oct 2005  9.197635  5.996528
#> Nov 2005  9.544667  5.887399
#> Dec 2005 12.563623  7.490782
#> Jan 2006 14.192807  8.026854
#> Feb 2006 15.382826  8.904826
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 15.09470  8.546350
#> Jun 2005 16.08327  9.235840
#> Jul 2005 16.13305  9.130626
#> Aug 2005 17.07368  9.608318
#> Sep 2005 16.90692  9.425962
#> Oct 2005 15.35431  8.207088
#> Nov 2005 17.05760 10.036221
#> Dec 2005 13.85604  7.383623
#> Jan 2006 11.39597  6.368848
#> Feb 2006 11.64467  6.515003
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 17.25320 10.490874
#> Jun 2005 17.12849  9.960231
#> Jul 2005 18.57187 10.842775
#> Aug 2005 16.97110  9.921982
#> Sep 2005 15.20504  9.035494
#> Oct 2005 15.36903  9.184413
#> Nov 2005 14.17257  8.528787
#> Dec 2005 11.79673  7.389492
#> Jan 2006 12.04580  7.852352
#> Feb 2006 12.66964  7.658352
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 13.44716  7.882252
#> Jun 2005 15.06869  8.722399
#> Jul 2005 15.79874  8.695518
#> Aug 2005 16.56357  9.279584
#> Sep 2005 16.48552  9.132644
#> Oct 2005 15.21677  8.568928
#> Nov 2005 12.74914  7.478873
#> Dec 2005 13.32305  8.324360
#> Jan 2006 14.10877  8.385544
#> Feb 2006 14.77661  8.903792
#> 
#> $sims[[9]]
#>             Quotes TV.advert
#> May 2005 14.616890  9.121409
#> Jun 2005 13.404611  8.240180
#> Jul 2005 12.559525  7.810416
#> Aug 2005 10.263987  6.641726
#> Sep 2005 10.721712  7.161274
#> Oct 2005 10.875855  8.163611
#> Nov 2005  7.476311  5.491622
#> Dec 2005  6.899366  4.930294
#> Jan 2006 11.174478  7.368494
#> Feb 2006 13.067008  8.079515
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 15.72954  9.142963
#> Jun 2005 15.70532  9.203036
#> Jul 2005 15.91362 10.524887
#> Aug 2005 11.57622  7.396579
#> Sep 2005 10.77155  6.936910
#> Oct 2005 10.91791  6.217236
#> Nov 2005 14.94133  8.922739
#> Dec 2005 18.17921 10.857993
#> Jan 2006 17.19832 10.321301
#> Feb 2006 14.22454  8.608352
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.40368643  1.33052782
#> 2003 -1.08056990 -1.14582835
#> 2004 -1.23343504 -1.15469695
#> 2005  1.40368643  1.33052782
#> 2006 -2.58725990 -2.26486335
#> 2007 -3.07477874 -1.76651025
#> 2008 -1.31677412 -1.06553020
#> 2009  0.65681006  0.07019039
#> 2010  0.33076863 -0.40143292
#> 2011  0.51993723  0.22434076
#> 2012  0.10053432 -0.17249791
#> 2013  0.95770684  0.35761548
#> 2014  0.26825909 -0.01668861
#> 2015 -0.12839403 -0.42852687
#> 2016 -1.17137522 -0.76232506
#> 2017  0.19770654  0.96971721
#> 2018  1.62259434  1.31981494
#> 2019 -0.14704277  0.17362676
#> 2020 -0.07045343 -0.05730098
#> 2021 -1.17072203 -0.66882907
#> 2022 -2.89887258 -1.53863637
#> 2023 -0.62736155 -0.06504866
#> 2024  0.96560231  0.19518092
#> 2025 -0.09354593 -0.06951135
#> 2026  0.15915097  1.22257588
#> 2027  0.29875209  0.25719228
#> 2028 -1.31329880 -0.69076345
#> 2029  2.48926902  1.54309187
#> 2030  1.91175656  0.91165371
#> 2031  2.04130891  1.23352769
#> 2032 -0.29759583 -0.05351566
#> 2033 -1.22025180 -0.56162404
#> 2034 -0.95856611 -0.54609232
#> 2035  0.20717781  0.29095534
#> 2036 -0.70462371 -1.15899926
#> 2037  2.26007518  1.40841386
#> 2038  2.95319223  1.69957104
#> 2039  0.89317411  0.77051186
#> 2040 -1.54622760 -0.71981400
#> 
#> $loocv
#> [1] 1.924173
#> 
#> $weighted_loocv
#> [1] -2.913168e-17
#> 
#> $loocv_per_series
#>    Quotes TV.advert 
#>  2.601976  1.246370 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res16$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-3.0748   Min.   :-2.26486  
#>  1st Qu.:-1.1256   1st Qu.:-0.67980  
#>  Median : 0.1005   Median :-0.05352  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9254   3rd Qu.: 0.84108  
#>  Max.   : 2.9532   Max.   : 1.69957

print(apply(res16$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.06458106 0.32251812

print(apply(res16$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.7471878 0.3924569

plot(res16, "Quotes", type = "sims")

plot(res16, "TV.advert")

4 - 2 With external regressors

(res8 <- ahead::ridge2f(fpp::insurance, xreg = xreg, 
                        type_pi = "movingblockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 15.83543  9.489772
#> Jun 2005 16.55420  9.952436
#> Jul 2005 16.60903  9.978960
#> Aug 2005 16.14855  9.585632
#> Sep 2005 16.75980  9.901060
#> Oct 2005 16.52638  9.757841
#> Nov 2005 16.20550  9.633730
#> Dec 2005 16.59988  9.841196
#> Jan 2006 17.43929 10.277301
#> Feb 2006 17.90968 10.399618
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 12.72699  7.686207
#> Jun 2005 14.05032  8.359338
#> Jul 2005 11.63180  7.645953
#> Aug 2005 11.21991  7.230665
#> Sep 2005 12.55685  7.961274
#> Oct 2005 12.04592  7.122343
#> Nov 2005 11.07888  7.381414
#> Dec 2005 11.22579  7.240579
#> Jan 2006 15.05035  8.985558
#> Feb 2006 15.08776  8.617056
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.72106  10.87268
#> Jun 2005 17.92847  10.86512
#> Jul 2005 19.29555  11.43451
#> Aug 2005 19.48949  11.08961
#> Sep 2005 20.02551  11.28706
#> Oct 2005 21.38539  12.11528
#> Nov 2005 20.92989  12.36284
#> Dec 2005 20.30573  11.79615
#> Jan 2006 19.68644  11.36362
#> Feb 2006 20.36054  11.67601
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 16.20933  9.248692
#> Jun 2005 17.92847 10.448051
#> Jul 2005 18.16175 10.522865
#> Aug 2005 19.21817 11.070449
#> Sep 2005 19.24082 10.967224
#> Oct 2005 18.82074 10.778483
#> Nov 2005 17.56392 10.189387
#> Dec 2005 16.79434  9.908283
#> Jan 2006 17.02486 10.433824
#> Feb 2006 14.88077  8.367710
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 15.72942  9.514972
#> Jun 2005 15.17064  9.280168
#> Jul 2005 13.04263  8.213002
#> Aug 2005 13.96530  8.942326
#> Sep 2005 14.94964  8.987836
#> Oct 2005 14.39268  8.525416
#> Nov 2005 13.11037  7.798914
#> Dec 2005 15.13573  8.754940
#> Jan 2006 18.51636 10.608660
#> Feb 2006 19.86284 10.967012
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 17.72106 10.872683
#> Jun 2005 17.53445 10.312671
#> Jul 2005 19.29555 11.367458
#> Aug 2005 17.52208 10.193230
#> Sep 2005 16.40079  9.716833
#> Oct 2005 15.94805  9.521983
#> Nov 2005 15.06181  9.227088
#> Dec 2005 17.96759 11.093207
#> Jan 2006 17.94621 10.616732
#> Feb 2006 19.80624 11.705974
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 12.17858  7.404155
#> Jun 2005 13.84444  8.102730
#> Jul 2005 17.25717  9.926768
#> Aug 2005 18.75814 10.433791
#> Sep 2005 20.06183 11.304300
#> Oct 2005 21.99834 12.464595
#> Nov 2005 20.27751 11.305189
#> Dec 2005 19.22525 10.855547
#> Jan 2006 18.46526 10.570034
#> Feb 2006 18.73089 11.109911
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 15.87497  9.639481
#> Jun 2005 16.19959 10.986202
#> Jul 2005 11.22221  7.481325
#> Aug 2005 10.91819  7.069941
#> Sep 2005 15.48265  9.636346
#> Oct 2005 11.87311  7.446051
#> Nov 2005 13.44818  7.958237
#> Dec 2005 17.25606  9.922121
#> Jan 2006 19.16939 10.706057
#> Feb 2006 20.50504 11.572811
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 16.20933  9.248692
#> Jun 2005 17.92847 10.448051
#> Jul 2005 18.16175 10.522865
#> Aug 2005 19.21817 11.070449
#> Sep 2005 19.24082 10.967224
#> Oct 2005 18.58599 10.422015
#> Nov 2005 21.11929 12.669903
#> Dec 2005 16.43824  9.331846
#> Jan 2006 15.15428  8.828476
#> Feb 2006 16.46135  9.510545
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 17.72106 10.872683
#> Jun 2005 17.53445 10.312671
#> Jul 2005 19.29555 11.367458
#> Aug 2005 17.52208 10.193230
#> Sep 2005 16.40079  9.716833
#> Oct 2005 17.18101 10.306562
#> Nov 2005 16.25646  9.773591
#> Dec 2005 14.25513  8.842654
#> Jan 2006 15.02018  9.526620
#> Feb 2006 15.80073  9.475915
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.61594  8.657718
#> Jun 2005 17.08964  9.943581
#> Jul 2005 18.24222 10.200904
#> Aug 2005 19.56826 11.095179
#> Sep 2005 19.90040 11.227659
#> Oct 2005 19.27412 10.912062
#> Nov 2005 17.24203  9.961629
#> Dec 2005 17.96073 10.902199
#> Jan 2006 17.84079 10.348311
#> Feb 2006 18.82829 11.083172
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 15.63319  9.847365
#> Jun 2005 14.75946  9.243212
#> Jul 2005 13.98353  8.732984
#> Aug 2005 12.25917  7.784271
#> Sep 2005 13.69333  8.755714
#> Oct 2005 14.54859 10.172875
#> Nov 2005 10.48910  7.260205
#> Dec 2005 10.34631  6.800926
#> Jan 2006 15.41898  9.579770
#> Feb 2006 17.21899 10.313983
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 16.46143  9.591273
#> Jun 2005 17.55234 10.447025
#> Jul 2005 17.42791 11.453975
#> Aug 2005 12.53595  8.003457
#> Sep 2005 12.22690  7.730630
#> Oct 2005 12.64115  7.028363
#> Nov 2005 17.48638 10.193159
#> Dec 2005 20.61942 12.000236
#> Jan 2006 19.83655 11.554531
#> Feb 2006 17.00171  9.889151
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes    TV.advert
#> 2002  1.67535261  1.600318363
#> 2003 -0.09454667  0.059245589
#> 2004 -0.61710349 -0.617562341
#> 2005  1.55432930  1.431543276
#> 2006 -1.65830176 -1.201288646
#> 2007 -3.27721670 -1.999680352
#> 2008 -0.83986146 -0.746117612
#> 2009  1.04485279  0.291750330
#> 2010  0.75352722 -0.155143365
#> 2011  0.26745241  0.004965096
#> 2012  0.43157027  0.102861987
#> 2013  1.13693270  0.494399368
#> 2014  0.49053241  0.135721074
#> 2015 -0.10253286 -0.465058629
#> 2016 -1.85353900 -1.312388835
#> 2017 -0.33001834  0.475005003
#> 2018  2.16635830  1.749815332
#> 2019  0.17739319  0.443529161
#> 2020  0.27361734  0.111136601
#> 2021 -0.58111949 -0.379519019
#> 2022 -2.30767070 -1.246635836
#> 2023 -0.19353452  0.176564889
#> 2024  1.00563207  0.187437814
#> 2025  0.41917089  0.235645632
#> 2026  0.52295932  1.364524163
#> 2027 -0.95933919 -0.673442252
#> 2028 -1.56353647 -0.843576594
#> 2029  2.26526358  1.468847972
#> 2030  1.69164304  0.697529740
#> 2031  2.01008148  1.118934668
#> 2032 -0.38235687 -0.261094013
#> 2033 -1.39361339 -0.785256869
#> 2034 -1.33394033 -0.789489620
#> 2035 -0.37279705 -0.008190995
#> 2036 -1.43004502 -1.625855697
#> 2037  0.16351013  0.242165460
#> 2038  2.64510868  1.356646561
#> 2039  0.59781674  0.490409659
#> 2040 -2.00203118 -1.128697062
#> 
#> $loocv
#> [1] 1.243869
#> 
#> $weighted_loocv
#> [1] 2.484213e-17
#> 
#> $loocv_per_series
#>       Quotes    TV.advert       xreg_1 
#> 2.556419e+00 1.175162e+00 2.765868e-05 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res8$residuals))
#>      Quotes          TV.advert      
#>  Min.   :-3.2772   Min.   :-1.9997  
#>  1st Qu.:-0.8996   1st Qu.:-0.7098  
#>  Median : 0.1635   Median : 0.1029  
#>  Mean   : 0.0000   Mean   : 0.0000  
#>  3rd Qu.: 0.8796   3rd Qu.: 0.4827  
#>  Max.   : 2.6451   Max.   : 1.7498

print(apply(res8$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.1553145 0.4247378

print(apply(res8$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9498845 0.5141136

plot(res8, "Quotes")

plot(res8, "TV.advert", type = "dist")

4 - 3 With external regressors and K-Means clustering

(res15 <- ahead::ridge2f(fpp::insurance, 
                        xreg = xreg, centers = centers, 
                        type_pi = "movingblockbootstrap", 
                        block_length = block_length,
                        B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 17.07501  10.38877
#> Jun 2005 16.45140  10.04062
#> Jul 2005 16.88215  10.40328
#> Aug 2005 17.13418  10.51880
#> Sep 2005 18.01493  11.01536
#> Oct 2005 17.33220  10.42757
#> Nov 2005 18.10396  10.92651
#> Dec 2005 18.91033  11.36575
#> Jan 2006 19.80824  11.71867
#> Feb 2006 20.74098  12.13798
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 14.93238  9.062999
#> Jun 2005 12.88308  7.956234
#> Jul 2005 12.04991  7.616191
#> Aug 2005 10.72144  7.171073
#> Sep 2005 11.33697  7.531479
#> Oct 2005 10.03032  6.298655
#> Nov 2005 12.28857  7.983691
#> Dec 2005 10.51174  7.070266
#> Jan 2006 14.65461  8.834809
#> Feb 2006 13.62371  8.462634
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 18.18986  11.34548
#> Jun 2005 19.33016  11.71025
#> Jul 2005 20.35521  12.53866
#> Aug 2005 21.72036  13.00379
#> Sep 2005 22.92964  13.55248
#> Oct 2005 24.77466  14.59971
#> Nov 2005 25.98420  15.11039
#> Dec 2005 27.21477  15.82385
#> Jan 2006 27.91976  15.64467
#> Feb 2006 31.10361  17.50968
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 17.25619  10.04403
#> Jun 2005 19.33016  11.71025
#> Jul 2005 20.15885  12.15493
#> Aug 2005 21.51097  12.82818
#> Sep 2005 21.82774  12.86799
#> Oct 2005 22.44978  13.16231
#> Nov 2005 22.58879  13.43424
#> Dec 2005 20.70159  11.94721
#> Jan 2006 20.60520  12.18114
#> Feb 2006 19.05591  10.49339
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 16.79008 10.383108
#> Jun 2005 13.93942  8.573201
#> Jul 2005 11.71636  7.455115
#> Aug 2005 13.93505  9.054271
#> Sep 2005 13.57815  8.311494
#> Oct 2005 13.31134  7.904023
#> Nov 2005 13.50743  8.094346
#> Dec 2005 15.17695  8.929781
#> Jan 2006 18.29377 10.714590
#> Feb 2006 17.48727  9.943893
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 18.18986 11.345483
#> Jun 2005 17.99973 10.758966
#> Jul 2005 20.35521 12.538664
#> Aug 2005 20.34391 12.365944
#> Sep 2005 18.49977 11.303287
#> Oct 2005 14.89496  8.867121
#> Nov 2005 15.94287  9.987713
#> Dec 2005 18.61563 11.935030
#> Jan 2006 17.84076 10.883685
#> Feb 2006 19.91270 12.458101
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 14.50946  8.857873
#> Jun 2005 13.38237  7.947634
#> Jul 2005 17.16769  9.985982
#> Aug 2005 15.63562  8.731688
#> Sep 2005 22.66423 13.521189
#> Oct 2005 24.82852 14.652416
#> Nov 2005 26.76353 15.557821
#> Dec 2005 28.00769 16.179192
#> Jan 2006 28.20487 15.703695
#> Feb 2006 31.35046 17.594465
#> 
#> $sims[[5]]
#>             Quotes TV.advert
#> May 2005 17.496468 10.769539
#> Jun 2005 15.637835 10.560953
#> Jul 2005 13.669633  9.097900
#> Aug 2005 10.503801  7.132658
#> Sep 2005 15.735215 10.141976
#> Oct 2005  9.077772  5.861019
#> Nov 2005 13.317551  7.951565
#> Dec 2005 16.531561  9.621613
#> Jan 2006 15.243530  8.642931
#> Feb 2006 23.226958 13.955736
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 17.25619  10.04403
#> Jun 2005 19.33016  11.71025
#> Jul 2005 20.15885  12.15493
#> Aug 2005 21.51097  12.82818
#> Sep 2005 21.82774  12.86799
#> Oct 2005 21.12386  12.11069
#> Nov 2005 22.56860  13.56923
#> Dec 2005 22.97395  13.45951
#> Jan 2006 21.32899  12.25840
#> Feb 2006 21.90323  12.49329
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 18.18986 11.345483
#> Jun 2005 17.99973 10.758966
#> Jul 2005 20.35521 12.538664
#> Aug 2005 20.34391 12.365944
#> Sep 2005 18.49977 11.303287
#> Oct 2005 15.56130  9.407444
#> Nov 2005 14.62434  9.009706
#> Dec 2005 12.69170  8.118623
#> Jan 2006 14.48363  9.495723
#> Feb 2006 14.09050  8.733523
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 16.38910  9.769544
#> Jun 2005 16.57417  9.746495
#> Jul 2005 15.27606  8.679168
#> Aug 2005 21.78114 13.054775
#> Sep 2005 23.00669 13.561559
#> Oct 2005 24.58913 14.418151
#> Nov 2005 23.29987 13.490909
#> Dec 2005 24.48361 14.599876
#> Jan 2006 26.93771 15.441377
#> Feb 2006 30.25333 17.217660
#> 
#> $sims[[9]]
#>             Quotes TV.advert
#> May 2005 16.952988 10.808949
#> Jun 2005 12.738130  7.985858
#> Jul 2005 13.198802  8.171010
#> Aug 2005 11.471074  7.303391
#> Sep 2005 13.823664  8.969811
#> Oct 2005 14.146241 10.086509
#> Nov 2005 11.989837  8.246787
#> Dec 2005  9.878854  6.765904
#> Jan 2006 15.811323 10.167268
#> Feb 2006 13.488190  8.383989
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 17.71992 10.519621
#> Jun 2005 17.58232 10.653589
#> Jul 2005 16.76485 11.256446
#> Aug 2005 14.30530  9.522930
#> Sep 2005 10.68630  7.305022
#> Oct 2005 13.33911  7.806068
#> Nov 2005 16.43676  9.922775
#> Dec 2005 20.04176 12.100780
#> Jan 2006 19.33258 11.697906
#> Feb 2006 16.64125 10.105781
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.11874618  1.19921616
#> 2003  0.09390745  0.01901986
#> 2004 -1.43434322 -1.16781389
#> 2005  1.27193197  1.24470742
#> 2006 -1.49065784 -1.21723396
#> 2007 -2.34261676 -1.51417390
#> 2008 -0.46298299 -0.60250287
#> 2009  1.63235318  0.57215494
#> 2010  0.40411221 -0.32801633
#> 2011  0.48277892  0.18055867
#> 2012  0.57087239  0.21240486
#> 2013  1.17734665  0.57331263
#> 2014  0.11531808  0.03121107
#> 2015 -0.42672324 -0.52697371
#> 2016 -1.85435793 -1.23203209
#> 2017 -0.03066633  0.70089111
#> 2018  2.03550258  1.71783757
#> 2019  0.10090940  0.43690274
#> 2020 -0.06200294  0.01106144
#> 2021  0.13620873  0.06072585
#> 2022 -1.89082466 -0.95683806
#> 2023 -0.59474544  0.01552906
#> 2024  0.86784469  0.14757423
#> 2025  0.64438935  0.39749239
#> 2026  1.21235947  1.79794968
#> 2027 -0.72834160 -0.52926182
#> 2028 -2.25083381 -1.18159761
#> 2029  1.33778080  0.97343623
#> 2030  1.97229999  0.89921107
#> 2031  0.98424529  0.52904493
#> 2032 -0.10842601 -0.11619745
#> 2033 -2.24369150 -1.28978017
#> 2034 -0.89142004 -0.54783127
#> 2035 -0.04783668  0.13304638
#> 2036 -1.11899454 -1.45036144
#> 2037  0.60160639  0.35230619
#> 2038  2.54250721  1.21587964
#> 2039  0.43166441  0.33684394
#> 2040 -1.75521983 -1.09770352
#> 
#> $loocv
#> [1] 0.7972857
#> 
#> $weighted_loocv
#> [1] 3.031318e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert        xreg_1 xreg_cluster1 xreg_cluster2 
#>  2.444624e+00  1.186564e+00  2.824183e-05  1.776061e-01  1.776061e-01 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res15$residuals))
#>      Quotes          TV.advert       
#>  Min.   :-2.3426   Min.   :-1.51417  
#>  1st Qu.:-0.8099   1st Qu.:-0.57517  
#>  Median : 0.1009   Median : 0.06073  
#>  Mean   : 0.0000   Mean   : 0.00000  
#>  3rd Qu.: 0.9260   3rd Qu.: 0.55060  
#>  Max.   : 2.5425   Max.   : 1.79795

print(apply(res15$residuals, 2, function (x) Box.test(x)$p.value))
#>    Quotes TV.advert 
#> 0.2913532 0.6500526

print(apply(res15$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.4150247 0.2507454

plot(res15, "Quotes", type = "dist")

plot(res15, "TV.advert")

4 - 4 With K-Means clustering

(res12 <- ahead::ridge2f(fpp::insurance, 
                         centers = centers, 
                         type_pi = "movingblockbootstrap", 
                         block_length = block_length,
                         B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.31666  8.479435
#> Jun 2005 14.79897  8.522877
#> Jul 2005 15.09848  8.875160
#> Aug 2005 14.22806  8.466408
#> Sep 2005 13.99722  8.338759
#> Oct 2005 13.54689  7.913198
#> Nov 2005 13.06803  7.808593
#> Dec 2005 13.03884  7.791509
#> Jan 2006 13.87904  8.068425
#> Feb 2006 14.66930  8.463263
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 11.232687  6.730729
#> Jun 2005 11.459137  6.577596
#> Jul 2005 11.422812  6.949462
#> Aug 2005 10.624930  6.395101
#> Sep 2005 10.788193  6.855985
#> Oct 2005  9.960566  5.924176
#> Nov 2005  9.061971  5.858474
#> Dec 2005  7.793197  5.783134
#> Jan 2006  8.527205  6.394150
#> Feb 2006  9.228331  6.358024
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.45780  9.997944
#> Jun 2005 16.66378  9.616582
#> Jul 2005 18.61269 11.226706
#> Aug 2005 17.26857  9.904980
#> Sep 2005 16.82548  9.501291
#> Oct 2005 18.09187 10.509248
#> Nov 2005 18.28160 10.518851
#> Dec 2005 17.83084 10.529534
#> Jan 2006 17.70148 10.051625
#> Feb 2006 18.82529 10.784185
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 15.52232  8.928961
#> Jul 2005 15.55556  8.827251
#> Aug 2005 16.67225  9.337125
#> Sep 2005 16.82548  9.283747
#> Oct 2005 16.97846  9.692513
#> Nov 2005 15.10292  9.459100
#> Dec 2005 13.23888  7.654781
#> Jan 2006 14.40625  8.717364
#> Feb 2006 13.37501  6.937512
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 14.05871  8.541413
#> Jun 2005 13.06677  7.477091
#> Jul 2005 11.35088  6.837713
#> Aug 2005 12.26101  7.392845
#> Sep 2005 13.72248  7.598158
#> Oct 2005 12.65635  6.153684
#> Nov 2005 11.71547  6.214864
#> Dec 2005 12.87835  6.672577
#> Jan 2006 15.76593  8.234121
#> Feb 2006 17.81317  8.953783
#> 
#> $sims[[3]]
#>             Quotes TV.advert
#> May 2005 16.457798  9.997944
#> Jun 2005 16.663777  9.396767
#> Jul 2005 18.612688 11.226706
#> Aug 2005 15.084954  9.699241
#> Sep 2005 11.789007  8.045646
#> Oct 2005  9.639296  6.491743
#> Nov 2005  9.005032  5.800762
#> Dec 2005 13.055676  8.182040
#> Jan 2006 14.667122  8.628919
#> Feb 2006 17.058451 10.296485
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 10.74483  6.500090
#> Jun 2005 10.99241  6.316452
#> Jul 2005 13.34599  7.651722
#> Aug 2005 14.59826  8.007984
#> Sep 2005 15.63830  8.843359
#> Oct 2005 18.41512 10.746365
#> Nov 2005 16.00308  9.864953
#> Dec 2005 12.95937  8.616656
#> Jan 2006 11.29621  6.894039
#> Feb 2006 12.32859  8.222790
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 14.80227  8.942430
#> Jun 2005 15.22211  9.680400
#> Jul 2005 11.67059  7.334376
#> Aug 2005 10.92209  6.350966
#> Sep 2005 15.22994  8.764685
#> Oct 2005 12.35934  6.694546
#> Nov 2005 13.08265  6.890438
#> Dec 2005 15.82867  8.254189
#> Jan 2006 17.86954  8.989473
#> Feb 2006 19.11913 10.925775
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 15.52232  8.928961
#> Jul 2005 15.55556  8.827251
#> Aug 2005 16.67225  9.337125
#> Sep 2005 16.82548  9.283747
#> Oct 2005 16.11146  8.568119
#> Nov 2005 18.94310 10.708692
#> Dec 2005 14.75202  8.143139
#> Jan 2006 12.65213  6.782360
#> Feb 2006 13.47381  6.948807
#> 
#> $sims[[7]]
#>             Quotes TV.advert
#> May 2005 16.457798  9.997944
#> Jun 2005 16.663777  9.396767
#> Jul 2005 18.612688 11.226706
#> Aug 2005 15.084954  9.699241
#> Sep 2005 11.789007  8.045646
#> Oct 2005 11.067163  7.466473
#> Nov 2005  9.985271  6.982575
#> Dec 2005  7.506050  5.768294
#> Jan 2006  7.723300  6.281444
#> Feb 2006  8.328257  6.189786
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 12.91307  7.525153
#> Jun 2005 14.88878  8.032791
#> Jul 2005 16.31092  8.541330
#> Aug 2005 17.44170  9.964710
#> Sep 2005 16.20438  9.564449
#> Oct 2005 14.22024  8.577016
#> Nov 2005 11.92526  6.784466
#> Dec 2005 12.97497  7.704279
#> Jan 2006 14.50529  7.746611
#> Feb 2006 16.65637  8.979287
#> 
#> $sims[[9]]
#>             Quotes TV.advert
#> May 2005 13.721246  8.509620
#> Jun 2005 13.202438  7.996705
#> Jul 2005 12.541417  7.631407
#> Aug 2005 10.538657  6.547122
#> Sep 2005 11.311957  7.202873
#> Oct 2005 12.708390  8.883981
#> Nov 2005  9.258095  6.057260
#> Dec 2005  8.782260  5.834253
#> Jan 2006 12.782055  8.049928
#> Feb 2006 14.442329  8.659598
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 11.31305  5.857544
#> Nov 2005 15.65940  9.322824
#> Dec 2005 18.41211 11.084886
#> Jan 2006 17.12258 10.359991
#> Feb 2006 14.09788  8.518803
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(summary(res12$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res12$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

print(apply(res12$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

plot(res12, "Quotes")

plot(res12, "TV.advert", type = "sims")

4 - 5 Using the median instead of the mean in bootstrap aggregation

(res14 <- ahead::ridge2f(fpp::insurance, 
                         centers = centers, 
                         type_pi = "movingblockbootstrap", 
                         type_aggregation = "median",
                         block_length = block_length,
                         B = B, h = h))
#>   |                                                                              |                                                                      |   0%  |                                                                              |=======                                                               |  10%  |                                                                              |==============                                                        |  20%  |                                                                              |=====================                                                 |  30%  |                                                                              |============================                                          |  40%  |                                                                              |===================================                                   |  50%  |                                                                              |==========================================                            |  60%  |                                                                              |=================================================                     |  70%  |                                                                              |========================================================              |  80%  |                                                                              |===============================================================       |  90%  |                                                                              |======================================================================| 100%
#> $mean
#>            Quotes TV.advert
#> May 2005 14.65401  8.442224
#> Jun 2005 15.37221  8.928961
#> Jul 2005 15.55556  8.684291
#> Aug 2005 14.84160  8.832423
#> Sep 2005 14.47621  8.405166
#> Oct 2005 12.68237  8.017296
#> Nov 2005 12.50396  6.936506
#> Dec 2005 13.01532  7.923709
#> Jan 2006 14.45577  8.142024
#> Feb 2006 14.27011  8.589201
#> 
#> $lower
#>             Quotes TV.advert
#> May 2005 11.232687  6.730729
#> Jun 2005 11.459137  6.577596
#> Jul 2005 11.422812  6.949462
#> Aug 2005 10.624930  6.395101
#> Sep 2005 10.788193  6.855985
#> Oct 2005  9.960566  5.924176
#> Nov 2005  9.061971  5.858474
#> Dec 2005  7.793197  5.783134
#> Jan 2006  8.527205  6.394150
#> Feb 2006  9.228331  6.358024
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 16.45780  9.997944
#> Jun 2005 16.66378  9.616582
#> Jul 2005 18.61269 11.226706
#> Aug 2005 17.26857  9.904980
#> Sep 2005 16.82548  9.501291
#> Oct 2005 18.09187 10.509248
#> Nov 2005 18.28160 10.518851
#> Dec 2005 17.83084 10.529534
#> Jan 2006 17.70148 10.051625
#> Feb 2006 18.82529 10.784185
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 15.52232  8.928961
#> Jul 2005 15.55556  8.827251
#> Aug 2005 16.67225  9.337125
#> Sep 2005 16.82548  9.283747
#> Oct 2005 16.97846  9.692513
#> Nov 2005 15.10292  9.459100
#> Dec 2005 13.23888  7.654781
#> Jan 2006 14.40625  8.717364
#> Feb 2006 13.37501  6.937512
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 14.05871  8.541413
#> Jun 2005 13.06677  7.477091
#> Jul 2005 11.35088  6.837713
#> Aug 2005 12.26101  7.392845
#> Sep 2005 13.72248  7.598158
#> Oct 2005 12.65635  6.153684
#> Nov 2005 11.71547  6.214864
#> Dec 2005 12.87835  6.672577
#> Jan 2006 15.76593  8.234121
#> Feb 2006 17.81317  8.953783
#> 
#> $sims[[3]]
#>             Quotes TV.advert
#> May 2005 16.457798  9.997944
#> Jun 2005 16.663777  9.396767
#> Jul 2005 18.612688 11.226706
#> Aug 2005 15.084954  9.699241
#> Sep 2005 11.789007  8.045646
#> Oct 2005  9.639296  6.491743
#> Nov 2005  9.005032  5.800762
#> Dec 2005 13.055676  8.182040
#> Jan 2006 14.667122  8.628919
#> Feb 2006 17.058451 10.296485
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 10.74483  6.500090
#> Jun 2005 10.99241  6.316452
#> Jul 2005 13.34599  7.651722
#> Aug 2005 14.59826  8.007984
#> Sep 2005 15.63830  8.843359
#> Oct 2005 18.41512 10.746365
#> Nov 2005 16.00308  9.864953
#> Dec 2005 12.95937  8.616656
#> Jan 2006 11.29621  6.894039
#> Feb 2006 12.32859  8.222790
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 14.80227  8.942430
#> Jun 2005 15.22211  9.680400
#> Jul 2005 11.67059  7.334376
#> Aug 2005 10.92209  6.350966
#> Sep 2005 15.22994  8.764685
#> Oct 2005 12.35934  6.694546
#> Nov 2005 13.08265  6.890438
#> Dec 2005 15.82867  8.254189
#> Jan 2006 17.86954  8.989473
#> Feb 2006 19.11913 10.925775
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.70289  8.202462
#> Jun 2005 15.52232  8.928961
#> Jul 2005 15.55556  8.827251
#> Aug 2005 16.67225  9.337125
#> Sep 2005 16.82548  9.283747
#> Oct 2005 16.11146  8.568119
#> Nov 2005 18.94310 10.708692
#> Dec 2005 14.75202  8.143139
#> Jan 2006 12.65213  6.782360
#> Feb 2006 13.47381  6.948807
#> 
#> $sims[[7]]
#>             Quotes TV.advert
#> May 2005 16.457798  9.997944
#> Jun 2005 16.663777  9.396767
#> Jul 2005 18.612688 11.226706
#> Aug 2005 15.084954  9.699241
#> Sep 2005 11.789007  8.045646
#> Oct 2005 11.067163  7.466473
#> Nov 2005  9.985271  6.982575
#> Dec 2005  7.506050  5.768294
#> Jan 2006  7.723300  6.281444
#> Feb 2006  8.328257  6.189786
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 12.91307  7.525153
#> Jun 2005 14.88878  8.032791
#> Jul 2005 16.31092  8.541330
#> Aug 2005 17.44170  9.964710
#> Sep 2005 16.20438  9.564449
#> Oct 2005 14.22024  8.577016
#> Nov 2005 11.92526  6.784466
#> Dec 2005 12.97497  7.704279
#> Jan 2006 14.50529  7.746611
#> Feb 2006 16.65637  8.979287
#> 
#> $sims[[9]]
#>             Quotes TV.advert
#> May 2005 13.721246  8.509620
#> Jun 2005 13.202438  7.996705
#> Jul 2005 12.541417  7.631407
#> Aug 2005 10.538657  6.547122
#> Sep 2005 11.311957  7.202873
#> Oct 2005 12.708390  8.883981
#> Nov 2005  9.258095  6.057260
#> Dec 2005  8.782260  5.834253
#> Jan 2006 12.782055  8.049928
#> Feb 2006 14.442329  8.659598
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 14.60513  8.374828
#> Jun 2005 16.24500  9.073878
#> Jul 2005 17.42852 10.647134
#> Aug 2005 13.00449  8.327721
#> Sep 2005 10.63613  6.755276
#> Oct 2005 11.31305  5.857544
#> Nov 2005 15.65940  9.322824
#> Dec 2005 18.41211 11.084886
#> Jan 2006 17.12258 10.359991
#> Feb 2006 14.09788  8.518803
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.34231135  1.22967613
#> 2003 -0.85477619 -0.98393029
#> 2004 -1.00310653 -1.06267920
#> 2005  1.34231135  1.22967613
#> 2006 -2.36146619 -2.10296529
#> 2007 -3.36233492 -1.98959149
#> 2008 -1.19409276 -0.96452874
#> 2009  0.77658242  0.14934530
#> 2010  0.59572299 -0.28721951
#> 2011 -0.05623678  0.01878023
#> 2012  0.04224137 -0.21855884
#> 2013  0.78363351  0.22949465
#> 2014  0.06745138 -0.16899703
#> 2015 -0.47113613 -0.67865735
#> 2016 -1.96150053 -1.32047922
#> 2017 -0.39902461  0.60812604
#> 2018  1.60642970  1.31542231
#> 2019 -0.38592136  0.01993791
#> 2020 -0.04845633  0.05173157
#> 2021 -0.88143921 -0.52155537
#> 2022 -2.56255162 -1.33780704
#> 2023 -0.54244587  0.01936417
#> 2024  0.49795898 -0.11485376
#> 2025  0.69509909  0.45274834
#> 2026  0.63284088  1.48814560
#> 2027 -1.47632294 -0.92299854
#> 2028 -1.78763672 -0.97448637
#> 2029  2.35063076  1.50826223
#> 2030  1.67726281  0.73013283
#> 2031  2.48613791  1.55203022
#> 2032 -0.13610811  0.06171409
#> 2033 -0.79383525 -0.25751529
#> 2034 -0.70470857 -0.41256032
#> 2035  0.53215201  0.47720624
#> 2036 -0.78427459 -1.15615114
#> 2037  2.38131744  1.46510425
#> 2038  3.68215047  2.20096869
#> 2039  1.36156864  1.07925077
#> 2040 -1.08642784 -0.41158288
#> 
#> $loocv
#> [1] 1.180296
#> 
#> $weighted_loocv
#> [1] 1.534077e-17
#> 
#> $loocv_per_series
#>        Quotes     TV.advert xreg_cluster1 xreg_cluster2 
#>     2.9554745     1.4096052     0.1780526     0.1780526 
#> 
#> attr(,"class")
#> [1] "mtsforecast"

print(apply(res14$residuals, 2, function (x) shapiro.test(x)$p.value)) # Gaussian?
#>    Quotes TV.advert 
#> 0.9888696 0.5936285

print(summary(res14$residuals))
#>      Quotes           TV.advert       
#>  Min.   :-3.36233   Min.   :-2.10297  
#>  1st Qu.:-0.86811   1st Qu.:-0.80083  
#>  Median :-0.05624   Median : 0.01878  
#>  Mean   : 0.00000   Mean   : 0.00000  
#>  3rd Qu.: 0.78011   3rd Qu.: 0.66913  
#>  Max.   : 3.68215   Max.   : 2.20097

print(apply(res14$residuals, 2, function (x) Box.test(x)$p.value))
#>     Quotes  TV.advert 
#> 0.04802834 0.26497281

plot(res14, "Quotes", type = "dist")

plot(res14, "TV.advert")

5 - Prediction intervals based on R-Vine copula simulation

(obj <- ahead::ridge2f(fpp::insurance, 
                         type_pi = "rvinecopula", 
                         B = B, h = h))
#> $mean
#>            Quotes TV.advert
#> May 2005 15.31002  9.226379
#> Jun 2005 14.79886  8.907883
#> Jul 2005 14.69090  8.827267
#> Aug 2005 13.84373  8.454779
#> Sep 2005 14.24753  8.815409
#> Oct 2005 14.34184  8.562135
#> Nov 2005 14.27625  8.705987
#> Dec 2005 13.68829  8.423866
#> Jan 2006 13.43270  8.073619
#> Feb 2006 14.13381  8.591550
#> 
#> $lower
#>            Quotes TV.advert
#> May 2005 14.36799  8.113840
#> Jun 2005 11.38913  7.103785
#> Jul 2005 13.71769  8.065010
#> Aug 2005 11.21805  6.285053
#> Sep 2005 12.75942  7.497283
#> Oct 2005 12.01240  6.759396
#> Nov 2005 12.00517  7.184455
#> Dec 2005 11.98408  6.689318
#> Jan 2006 11.33370  6.726070
#> Feb 2006 11.49345  7.273658
#> 
#> $upper
#>            Quotes TV.advert
#> May 2005 17.41644 10.773176
#> Jun 2005 17.02214 10.527019
#> Jul 2005 15.93492  9.663288
#> Aug 2005 17.10199 10.801843
#> Sep 2005 15.73552  9.983540
#> Oct 2005 16.10716  9.894525
#> Nov 2005 15.71202  9.710339
#> Dec 2005 15.41633  9.427269
#> Jan 2006 15.27358  9.197813
#> Feb 2006 15.84641  9.555500
#> 
#> $sims
#> $sims[[1]]
#>            Quotes TV.advert
#> May 2005 14.30454  7.922151
#> Jun 2005 15.39684  9.474422
#> Jul 2005 13.61132  8.018403
#> Aug 2005 14.79509  8.853827
#> Sep 2005 14.86888  9.087402
#> Oct 2005 16.02594  9.976760
#> Nov 2005 15.18795  9.697184
#> Dec 2005 14.10462  8.661040
#> Jan 2006 15.50597  9.328438
#> Feb 2006 13.85247  7.707187
#> 
#> $sims[[2]]
#>            Quotes TV.advert
#> May 2005 15.16700  8.873611
#> Jun 2005 14.13293  7.794348
#> Jul 2005 15.02135  8.761327
#> Aug 2005 12.10252  7.190436
#> Sep 2005 12.61971  7.407260
#> Oct 2005 14.26606  8.819319
#> Nov 2005 14.69218  8.938429
#> Dec 2005 14.34228  8.756040
#> Jan 2006 10.96740  6.436479
#> Feb 2006 14.43336  8.809632
#> 
#> $sims[[3]]
#>            Quotes TV.advert
#> May 2005 15.11036  8.774100
#> Jun 2005 10.59254  6.903299
#> Jul 2005 14.97116  9.042900
#> Aug 2005 15.09260  9.808833
#> Sep 2005 14.19820  8.952254
#> Oct 2005 15.35600  9.529417
#> Nov 2005 14.31119  8.667339
#> Dec 2005 12.18599  7.624836
#> Jan 2006 14.07058  8.522981
#> Feb 2006 15.09665  9.428995
#> 
#> $sims[[4]]
#>            Quotes TV.advert
#> May 2005 14.62871  9.125950
#> Jun 2005 16.38452 10.189155
#> Jul 2005 16.14851  9.798552
#> Aug 2005 13.84074  8.374269
#> Sep 2005 15.34916  9.348647
#> Oct 2005 15.78587  9.336611
#> Nov 2005 14.22790  8.189069
#> Dec 2005 11.92546  6.417716
#> Jan 2006 12.99929  8.274889
#> Feb 2006 11.36920  7.172730
#> 
#> $sims[[5]]
#>            Quotes TV.advert
#> May 2005 15.57734  9.316228
#> Jun 2005 15.48842  9.271059
#> Jul 2005 15.19923  9.197380
#> Aug 2005 14.81119  9.556891
#> Sep 2005 13.24064  8.785248
#> Oct 2005 12.35213  6.925970
#> Nov 2005 11.99369  7.081032
#> Dec 2005 14.31366  8.795979
#> Jan 2006 14.47314  8.747883
#> Feb 2006 15.89136  9.490070
#> 
#> $sims[[6]]
#>            Quotes TV.advert
#> May 2005 14.97411  9.106063
#> Jun 2005 17.20726 10.625109
#> Jul 2005 14.50950  8.691795
#> Aug 2005 14.11632  8.408133
#> Sep 2005 13.98775  8.495862
#> Oct 2005 11.91377  6.711036
#> Nov 2005 12.04474  7.540691
#> Dec 2005 12.51957  7.633600
#> Jan 2006 13.84837  7.779032
#> Feb 2006 15.11033  9.023259
#> 
#> $sims[[7]]
#>            Quotes TV.advert
#> May 2005 14.97313  8.935483
#> Jun 2005 14.82365  8.982032
#> Jul 2005 14.32267  8.273417
#> Aug 2005 12.57208  7.749678
#> Sep 2005 15.51601  9.664412
#> Oct 2005 13.70746  7.888991
#> Nov 2005 15.22292  9.168991
#> Dec 2005 13.76598  8.928246
#> Jan 2006 13.11415  7.870575
#> Feb 2006 13.70643  8.547298
#> 
#> $sims[[8]]
#>            Quotes TV.advert
#> May 2005 14.58655  9.243993
#> Jun 2005 14.86911  8.758953
#> Jul 2005 14.08406  8.225544
#> Aug 2005 12.46013  7.493386
#> Sep 2005 15.79925 10.076191
#> Oct 2005 16.13074  9.611269
#> Nov 2005 14.88994  9.307531
#> Dec 2005 14.72159  9.411932
#> Jan 2006 12.59542  7.844081
#> Feb 2006 15.69158  9.574496
#> 
#> $sims[[9]]
#>            Quotes TV.advert
#> May 2005 15.93061  9.955696
#> Jun 2005 14.72513  8.466719
#> Jul 2005 14.73991  9.195801
#> Aug 2005 17.68536 11.090136
#> Sep 2005 13.24996  7.807364
#> Oct 2005 13.71088  8.620053
#> Nov 2005 14.33793  8.755450
#> Dec 2005 15.61802  9.431722
#> Jan 2006 13.81751  8.208286
#> Feb 2006 14.26531  8.540535
#> 
#> $sims[[10]]
#>            Quotes TV.advert
#> May 2005 17.84780 11.010509
#> Jun 2005 14.36824  8.613730
#> Jul 2005 14.30128  9.067548
#> Aug 2005 10.96127  6.022200
#> Sep 2005 13.64573  8.529455
#> Oct 2005 14.16951  8.201922
#> Nov 2005 15.85402  9.714159
#> Dec 2005 13.38572  8.577552
#> Jan 2006 12.93521  7.723549
#> Feb 2006 11.92145  7.621296
#> 
#> 
#> $x
#>            Quotes TV.advert
#> Jan 2002 12.97065  7.212725
#> Feb 2002 15.38714  9.443570
#> Mar 2002 13.22957  7.534250
#> Apr 2002 12.97065  7.212725
#> May 2002 15.38714  9.443570
#> Jun 2002 11.72288  6.415215
#> Jul 2002 10.06177  5.806990
#> Aug 2002 10.82279  6.203600
#> Sep 2002 13.28707  7.586430
#> Oct 2002 14.57832  8.004935
#> Nov 2002 15.60542  8.834980
#> Dec 2002 15.93515  8.957255
#> Jan 2003 16.99486  9.532990
#> Feb 2003 16.87821  9.392950
#> Mar 2003 16.45128  8.918560
#> Apr 2003 15.28118  8.374120
#> May 2003 15.88901  9.844505
#> Jun 2003 15.67747  9.849390
#> Jul 2003 13.28780  8.402730
#> Aug 2003 12.64484  7.920675
#> Sep 2003 11.82771  7.436085
#> Oct 2003  9.69184  6.340490
#> Nov 2003 10.30415  6.939995
#> Dec 2003 11.38253  6.977100
#> Jan 2004 12.95149  8.010201
#> Feb 2004 13.63092  9.565460
#> Mar 2004  9.12098  6.272510
#> Apr 2004  8.39468  5.707495
#> May 2004 12.30076  7.963540
#> Jun 2004 13.84831  8.494221
#> Jul 2004 15.96246  9.789085
#> Aug 2004 14.19738  8.692825
#> Sep 2004 12.85922  8.057230
#> Oct 2004 12.08837  7.588995
#> Nov 2004 12.93375  8.244881
#> Dec 2004 11.72235  6.675540
#> Jan 2005 15.47126  9.219604
#> Feb 2005 18.43898 10.963800
#> Mar 2005 17.49186 10.456290
#> Apr 2005 14.49168  8.728600
#> 
#> $level
#> [1] 95
#> 
#> $method
#> [1] "ridge2"
#> 
#> $residuals
#> Time Series:
#> Start = 2002 
#> End = 2040 
#> Frequency = 1 
#>           Quotes   TV.advert
#> 2002  1.40368643  1.33052782
#> 2003 -1.08056990 -1.14582835
#> 2004 -1.23343504 -1.15469695
#> 2005  1.40368643  1.33052782
#> 2006 -2.58725990 -2.26486335
#> 2007 -3.07477874 -1.76651025
#> 2008 -1.31677412 -1.06553020
#> 2009  0.65681006  0.07019039
#> 2010  0.33076863 -0.40143292
#> 2011  0.51993723  0.22434076
#> 2012  0.10053432 -0.17249791
#> 2013  0.95770684  0.35761548
#> 2014  0.26825909 -0.01668861
#> 2015 -0.12839403 -0.42852687
#> 2016 -1.17137522 -0.76232506
#> 2017  0.19770654  0.96971721
#> 2018  1.62259434  1.31981494
#> 2019 -0.14704277  0.17362676
#> 2020 -0.07045343 -0.05730098
#> 2021 -1.17072203 -0.66882907
#> 2022 -2.89887258 -1.53863637
#> 2023 -0.62736155 -0.06504866
#> 2024  0.96560231  0.19518092
#> 2025 -0.09354593 -0.06951135
#> 2026  0.15915097  1.22257588
#> 2027  0.29875209  0.25719228
#> 2028 -1.31329880 -0.69076345
#> 2029  2.48926902  1.54309187
#> 2030  1.91175656  0.91165371
#> 2031  2.04130891  1.23352769
#> 2032 -0.29759583 -0.05351566
#> 2033 -1.22025180 -0.56162404
#> 2034 -0.95856611 -0.54609232
#> 2035  0.20717781  0.29095534
#> 2036 -0.70462371 -1.15899926
#> 2037  2.26007518  1.40841386
#> 2038  2.95319223  1.69957104
#> 2039  0.89317411  0.77051186
#> 2040 -1.54622760 -0.71981400
#> 
#> $copula
#> $copula$params
#> $copula$params$Quotes
#>        mean             sd      
#>   -2.277575e-16    1.409030e+00 
#>  ( 2.256254e-01) ( 1.595412e-01)
#> 
#> $copula$params$TV.advert
#>        mean             sd      
#>   -2.049170e-16    9.751965e-01 
#>  ( 1.561564e-01) ( 1.104193e-01)
#> 
#> 
#> $copula$distro
#> [1] "normal"
#> 
#> $copula$RVM_U
#> C-vine copula with the following pair-copulas:
#> Tree 1:
#> 1,2  Gumbel (par = 4.19, tau = 0.76) 
#> 
#> ---
#> 1 <-> Quotes,   2 <-> TV.advert,   1 <-> Quotes,
#> 2 <-> TV.advert
#> 
#> $margins
#> [1] "gaussian"
#> 
#> $loocv
#> [1] 1.924173
#> 
#> $weighted_loocv
#> [1] -2.913168e-17
#> 
#> $loocv_per_series
#>    Quotes TV.advert 
#>  2.601976  1.246370 
#> 
#> attr(,"class")
#> [1] "mtsforecast"


plot(obj, "Quotes", type = "dist")

plot(obj, "TV.advert", type = "sims")