# Introduction to Pattern Sequence based Forecasting (PSF) algorithm

## Introduction

The Algorithm Pattern Sequence based Forecasting (PSF) was first proposed by Martinez Alvarez, et al., 2008 and then modified and suggested improvement by Martinez Alvarez, et al., 2011. The technical detailes are mentioned in referenced articles. PSF algorithm consists of various statistical operations like:

• Data Normalization/ Denormalization
• Calculation of optimum Window size (W)
• Calculation of optimum cluster size (k)
• Pattern Sequence based Forecasting
• RMSE/MAE Calculation, etc..

## Example

This section discusses about the examples to introduce the use of the PSF package and to compare it with auto.arima() and ets() functions, which are well accepted functions in the R community working over time series forecasting techniques. The data used in this example are ’nottem’ and ’sunspots’ which are the standard time series dataset available in R. The ’nottem’ dataset is the average air temperatures at Nottingham Castle in degrees Fahrenheit, collected for 20 years, on monthly basis.

Similarly, ’sunspots’ dataset is mean relative sunspot numbers from 1749 to 1983, measured on monthly basis. First of all, the psf() function from PSF package is used to forecast the future values. For both datasets, all the recorded values except for the final year are considered as training data, and the last year is used for testing purposes. The predicted values for final year with psf() function for both datasets are now discussed.

#### Install library

Download the Package and install with instruction:

library(PSF)

#### Model building for ’nottem’ dataset with psf() function.

nottem_model <- psf(nottem)
nottem_model
## $original_series ## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ## 1920 40.6 40.8 44.4 46.7 54.1 58.5 57.7 56.4 54.3 50.5 42.9 39.8 ## 1921 44.2 39.8 45.1 47.0 54.1 58.7 66.3 59.9 57.0 54.2 39.7 42.8 ## 1922 37.5 38.7 39.5 42.1 55.7 57.8 56.8 54.3 54.3 47.1 41.8 41.7 ## 1923 41.8 40.1 42.9 45.8 49.2 52.7 64.2 59.6 54.4 49.2 36.3 37.6 ## 1924 39.3 37.5 38.3 45.5 53.2 57.7 60.8 58.2 56.4 49.8 44.4 43.6 ## 1925 40.0 40.5 40.8 45.1 53.8 59.4 63.5 61.0 53.0 50.0 38.1 36.3 ## 1926 39.2 43.4 43.4 48.9 50.6 56.8 62.5 62.0 57.5 46.7 41.6 39.8 ## 1927 39.4 38.5 45.3 47.1 51.7 55.0 60.4 60.5 54.7 50.3 42.3 35.2 ## 1928 40.8 41.1 42.8 47.3 50.9 56.4 62.2 60.5 55.4 50.2 43.0 37.3 ## 1929 34.8 31.3 41.0 43.9 53.1 56.9 62.5 60.3 59.8 49.2 42.9 41.9 ## 1930 41.6 37.1 41.2 46.9 51.2 60.4 60.1 61.6 57.0 50.9 43.0 38.8 ## 1931 37.1 38.4 38.4 46.5 53.5 58.4 60.6 58.2 53.8 46.6 45.5 40.6 ## 1932 42.4 38.4 40.3 44.6 50.9 57.0 62.1 63.5 56.3 47.3 43.6 41.8 ## 1933 36.2 39.3 44.5 48.7 54.2 60.8 65.5 64.9 60.1 50.2 42.1 35.8 ## 1934 39.4 38.2 40.4 46.9 53.4 59.6 66.5 60.4 59.2 51.2 42.8 45.8 ## 1935 40.0 42.6 43.5 47.1 50.0 60.5 64.6 64.0 56.8 48.6 44.2 36.4 ## 1936 37.3 35.0 44.0 43.9 52.7 58.6 60.0 61.1 58.1 49.6 41.6 41.3 ## 1937 40.8 41.0 38.4 47.4 54.1 58.6 61.4 61.8 56.3 50.9 41.4 37.1 ## 1938 42.1 41.2 47.3 46.6 52.4 59.0 59.6 60.4 57.0 50.7 47.8 39.2 ## 1939 39.4 40.9 42.4 47.8 52.4 58.0 60.7 61.8 58.2 46.7 46.6 37.8 ## ##$train_data
##             V1        V2        V3        V4        V5        V6        V7
##  1: 0.26420455 0.2698864 0.3721591 0.4375000 0.6477273 0.7727273 0.7500000
##  2: 0.36647727 0.2414773 0.3920455 0.4460227 0.6477273 0.7784091 0.9943182
##  3: 0.17613636 0.2102273 0.2329545 0.3068182 0.6931818 0.7528409 0.7244318
##  4: 0.29829545 0.2500000 0.3295455 0.4119318 0.5085227 0.6079545 0.9346591
##  5: 0.22727273 0.1761364 0.1988636 0.4034091 0.6221591 0.7500000 0.8380682
##  6: 0.24715909 0.2613636 0.2698864 0.3920455 0.6392045 0.7982955 0.9147727
##  7: 0.22443182 0.3437500 0.3437500 0.5000000 0.5482955 0.7244318 0.8863636
##  8: 0.23011364 0.2045455 0.3977273 0.4488636 0.5795455 0.6732955 0.8267045
##  9: 0.26988636 0.2784091 0.3267045 0.4545455 0.5568182 0.7130682 0.8778409
## 10: 0.09943182 0.0000000 0.2755682 0.3579545 0.6193182 0.7272727 0.8863636
## 11: 0.29261364 0.1647727 0.2812500 0.4431818 0.5653409 0.8267045 0.8181818
## 12: 0.16477273 0.2017045 0.2017045 0.4318182 0.6306818 0.7698864 0.8323864
## 13: 0.31534091 0.2017045 0.2556818 0.3778409 0.5568182 0.7301136 0.8750000
## 14: 0.13920455 0.2272727 0.3750000 0.4943182 0.6505682 0.8380682 0.9715909
## 15: 0.23011364 0.1960227 0.2585227 0.4431818 0.6278409 0.8039773 1.0000000
## 16: 0.24715909 0.3210227 0.3465909 0.4488636 0.5312500 0.8295455 0.9460227
## 17: 0.17045455 0.1051136 0.3607955 0.3579545 0.6079545 0.7755682 0.8153409
## 18: 0.26988636 0.2755682 0.2017045 0.4573864 0.6477273 0.7755682 0.8551136
## 19: 0.30681818 0.2812500 0.4545455 0.4346591 0.5994318 0.7869318 0.8039773
## 20: 0.23011364 0.2727273 0.3153409 0.4687500 0.5994318 0.7585227 0.8352273
##            V8        V9       V10       V11       V12
##  1: 0.7130682 0.6534091 0.5454545 0.3295455 0.2414773
##  2: 0.8125000 0.7301136 0.6505682 0.2386364 0.3267045
##  3: 0.6534091 0.6534091 0.4488636 0.2982955 0.2954545
##  4: 0.8039773 0.6562500 0.5085227 0.1420455 0.1789773
##  5: 0.7642045 0.7130682 0.5255682 0.3721591 0.3494318
##  6: 0.8437500 0.6164773 0.5312500 0.1931818 0.1420455
##  7: 0.8721591 0.7443182 0.4375000 0.2926136 0.2414773
##  8: 0.8295455 0.6647727 0.5397727 0.3125000 0.1107955
##  9: 0.8295455 0.6846591 0.5369318 0.3323864 0.1704545
## 10: 0.8238636 0.8096591 0.5085227 0.3295455 0.3011364
## 11: 0.8607955 0.7301136 0.5568182 0.3323864 0.2130682
## 12: 0.7642045 0.6392045 0.4346591 0.4034091 0.2642045
## 13: 0.9147727 0.7102273 0.4545455 0.3494318 0.2982955
## 14: 0.9545455 0.8181818 0.5369318 0.3068182 0.1278409
## 15: 0.8267045 0.7926136 0.5653409 0.3267045 0.4119318
## 16: 0.9289773 0.7244318 0.4914773 0.3664773 0.1448864
## 17: 0.8465909 0.7613636 0.5198864 0.2926136 0.2840909
## 18: 0.8664773 0.7102273 0.5568182 0.2869318 0.1647727
## 19: 0.8267045 0.7301136 0.5511364 0.4687500 0.2244318
## 20: 0.8664773 0.7642045 0.4375000 0.4346591 0.1846591
##
## $k ## [1] 2 ## ##$w
## [1] 1
##
## $cycle ## [1] 12 ## ##$dmin
## [1] 31.3
##
## $dmax ## [1] 66.5 ## ## attr(,"class") ## [1] "psf" #### Model building for ’sunspots’ dataset with psf() function. sunspots_model <- psf(sunspots) sunspots_model ##$original_series
##        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov
## 1749  58.0  62.6  70.0  55.7  85.0  83.5  94.8  66.3  75.9  75.5 158.6
## 1750  73.3  75.9  89.2  88.3  90.0 100.0  85.4 103.0  91.2  65.7  63.3
## 1751  70.0  43.5  45.3  56.4  60.7  50.7  66.3  59.8  23.5  23.2  28.5
## 1752  35.0  50.0  71.0  59.3  59.7  39.6  78.4  29.3  27.1  46.6  37.6
## 1753  44.0  32.0  45.7  38.0  36.0  31.7  22.2  39.0  28.0  25.0  20.0
## 1754   0.0   3.0   1.7  13.7  20.7  26.7  18.8  12.3   8.2  24.1  13.2
## 1755  10.2  11.2   6.8   6.5   0.0   0.0   8.6   3.2  17.8  23.7   6.8
## 1756  12.5   7.1   5.4   9.4  12.5  12.9   3.6   6.4  11.8  14.3  17.0
## 1757  14.1  21.2  26.2  30.0  38.1  12.8  25.0  51.3  39.7  32.5  64.7
## 1758  37.6  52.0  49.0  72.3  46.4  45.0  44.0  38.7  62.5  37.7  43.0
## 1759  48.3  44.0  46.8  47.0  49.0  50.0  51.0  71.3  77.2  59.7  46.3
## 1760  67.3  59.5  74.7  58.3  72.0  48.3  66.0  75.6  61.3  50.6  59.7
## 1761  70.0  91.0  80.7  71.7 107.2  99.3  94.1  91.1 100.7  88.7  89.7
## 1762  43.8  72.8  45.7  60.2  39.9  77.1  33.8  67.7  68.5  69.3  77.8
## 1763  56.5  31.9  34.2  32.9  32.7  35.8  54.2  26.5  68.1  46.3  60.9
## 1764  59.7  59.7  40.2  34.4  44.3  30.0  30.0  30.0  28.2  28.0  26.0
## 1765  24.0  26.0  25.0  22.0  20.2  20.0  27.0  29.7  16.0  14.0  14.0
## 1766  12.0  11.0  36.6   6.0  26.8   3.0   3.3   4.0   4.3   5.0   5.7
## 1767  27.4  30.0  43.0  32.9  29.8  33.3  21.9  40.8  42.7  44.1  54.7
## 1768  53.5  66.1  46.3  42.7  77.7  77.4  52.6  66.8  74.8  77.8  90.6
## 1769  73.9  64.2  64.3  96.7  73.6  94.4 118.6 120.3 148.8 158.2 148.1
## 1770 104.0 142.5  80.1  51.0  70.1  83.3 109.8 126.3 104.4 103.6 132.2
## 1771  36.0  46.2  46.7  64.9 152.7 119.5  67.7  58.5 101.4  90.0  99.7
## 1772 100.9  90.8  31.1  92.2  38.0  57.0  77.3  56.2  50.5  78.6  61.3
## 1773  54.6  29.0  51.2  32.9  41.1  28.4  27.7  12.7  29.3  26.3  40.9
## 1774  46.8  65.4  55.7  43.8  51.3  28.5  17.5   6.6   7.9  14.0  17.7
## 1775   4.4   0.0  11.6  11.2   3.9  12.3   1.0   7.9   3.2   5.6  15.1
## 1776  21.7  11.6   6.3  21.8  11.2  19.0   1.0  24.2  16.0  30.0  35.0
## 1777  45.0  36.5  39.0  95.5  80.3  80.7  95.0 112.0 116.2 106.5 146.0
## 1778 177.3 109.3 134.0 145.0 238.9 171.6 153.0 140.0 171.7 156.3 150.3
## 1779 114.7 165.7 118.0 145.0 140.0 113.7 143.0 112.0 111.0 124.0 114.0
## 1780  70.0  98.0  98.0  95.0 107.2  88.0  86.0  86.0  93.7  77.0  60.0
## 1781  98.7  74.7  53.0  68.3 104.7  97.7  73.5  66.0  51.0  27.3  67.0
## 1782  54.0  37.5  37.0  41.0  54.3  38.0  37.0  44.0  34.0  23.2  31.5
## 1783  28.0  38.7  26.7  28.3  23.0  25.2  32.2  20.0  18.0   8.0  15.0
## 1784  13.0   8.0  11.0  10.0   6.0   9.0   6.0  10.0  10.0   8.0  17.0
## 1785   6.5   8.0   9.0  15.7  20.7  26.3  36.3  20.0  32.0  47.2  40.2
## 1786  37.2  47.6  47.7  85.4  92.3  59.0  83.0  89.7 111.5 112.3 116.0
## 1787 134.7 106.0  87.4 127.2 134.8  99.2 128.0 137.2 157.3 157.0 141.5
## 1788 138.0 129.2 143.3 108.5 113.0 154.2 141.5 136.0 141.0 142.0  94.7
## 1789 114.0 125.3 120.0 123.3 123.5 120.0 117.0 103.0 112.0  89.7 134.0
## 1790 103.0 127.5  96.3  94.0  93.0  91.0  69.3  87.0  77.3  84.3  82.0
## 1791  72.7  62.0  74.0  77.2  73.7  64.2  71.0  43.0  66.5  61.7  67.0
## 1792  58.0  64.0  63.0  75.7  62.0  61.0  45.8  60.0  59.0  59.0  57.0
## 1793  56.0  55.0  55.5  53.0  52.3  51.0  50.0  29.3  24.0  47.0  44.0
## 1794  45.0  44.0  38.0  28.4  55.7  41.5  41.0  40.0  11.1  28.5  67.4
## 1795  21.4  39.9  12.6  18.6  31.0  17.1  12.9  25.7  13.5  19.5  25.0
## 1796  22.0  23.8  15.7  31.7  21.0   6.7  26.9   1.5  18.4  11.0   8.4
## 1797  14.4   4.2   4.0   4.0   7.3  11.1   4.3   6.0   5.7   6.9   5.8
## 1798   2.0   4.0  12.4   1.1   0.0   0.0   0.0   3.0   2.4   1.5  12.5
## 1799   1.6  12.6  21.7   8.4   8.2  10.6   2.1   0.0   0.0   4.6   2.7
## 1800   6.9   9.3  13.9   0.0   5.0  23.7  21.0  19.5  11.5  12.3  10.5
## 1801  27.0  29.0  30.0  31.0  32.0  31.2  35.0  38.7  33.5  32.6  39.8
## 1802  47.8  47.0  40.8  42.0  44.0  46.0  48.0  50.0  51.8  38.5  34.5
## 1803  50.0  50.8  29.5  25.0  44.3  36.0  48.3  34.1  45.3  54.3  51.0
## 1804  45.3  48.3  48.0  50.6  33.4  34.8  29.8  43.1  53.0  62.3  61.0
## 1805  61.0  44.1  51.4  37.5  39.0  40.5  37.6  42.7  44.4  29.4  41.0
## 1806  39.0  29.6  32.7  27.7  26.4  25.6  30.0  26.3  24.0  27.0  25.0
## 1807  12.0  12.2   9.6  23.8  10.0  12.0  12.7  12.0   5.7   8.0   2.6
## 1808   0.0   4.5   0.0  12.3  13.5  13.5   6.7   8.0  11.7   4.7  10.5
## 1809   7.2   9.2   0.9   2.5   2.0   7.7   0.3   0.2   0.4   0.0   0.0
## 1810   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
## 1811   0.0   0.0   0.0   0.0   0.0   0.0   6.6   0.0   2.4   6.1   0.8
## 1812  11.3   1.9   0.7   0.0   1.0   1.3   0.5  15.6   5.2   3.9   7.9
## 1813   0.0  10.3   1.9  16.6   5.5  11.2  18.3   8.4  15.3  27.8  16.7
## 1814  22.2  12.0   5.7  23.8   5.8  14.9  18.5   2.3   8.1  19.3  14.5
## 1815  19.2  32.2  26.2  31.6   9.8  55.9  35.5  47.2  31.5  33.5  37.2
## 1816  26.3  68.8  73.7  58.8  44.3  43.6  38.8  23.2  47.8  56.4  38.1
## 1817  36.4  57.9  96.2  26.4  21.2  40.0  50.0  45.0  36.7  25.6  28.9
## 1818  34.9  22.4  25.4  34.5  53.1  36.4  28.0  31.5  26.1  31.7  10.9
## 1819  32.5  20.7   3.7  20.2  19.6  35.0  31.4  26.1  14.9  27.5  25.1
## 1820  19.2  26.6   4.5  19.4  29.3  10.8  20.6  25.9   5.2   9.0   7.9
## 1821  21.5   4.3   5.7   9.2   1.7   1.8   2.5   4.8   4.4  18.8   4.4
## 1822   0.0   0.9  16.1  13.5   1.5   5.6   7.9   2.1   0.0   0.4   0.0
## 1823   0.0   0.0   0.6   0.0   0.0   0.0   0.5   0.0   0.0   0.0   0.0
## 1824  21.6  10.8   0.0  19.4   2.8   0.0   0.0   1.4  20.5  25.2   0.0
## 1825   5.0  15.5  22.4   3.8  15.4  15.4  30.9  25.4  15.7  15.6  11.7
## 1826  17.7  18.2  36.7  24.0  32.4  37.1  52.5  39.6  18.9  50.6  39.5
## 1827  34.6  47.4  57.8  46.0  56.3  56.7  42.9  53.7  49.6  57.2  48.2
## 1828  52.8  64.4  65.0  61.1  89.1  98.0  54.3  76.4  50.4  54.7  57.0
## 1829  43.0  49.4  72.3  95.0  67.5  73.9  90.8  78.3  52.8  57.2  67.6
## 1830  52.2  72.1  84.6 107.1  66.3  65.1  43.9  50.7  62.1  84.4  81.2
## 1831  47.5  50.1  93.4  54.6  38.1  33.4  45.2  54.9  37.9  46.2  43.5
## 1832  30.9  55.5  55.1  26.9  41.3  26.7  13.9   8.9   8.2  21.1  14.3
## 1833  11.3  14.9  11.8   2.8  12.9   1.0   7.0   5.7  11.6   7.5   5.9
## 1834   4.9  18.1   3.9   1.4   8.8   7.8   8.7   4.0  11.5  24.8  30.5
## 1835   7.5  24.5  19.7  61.5  43.6  33.2  59.8  59.0 100.8  95.2 100.0
## 1836  88.6 107.6  98.1 142.9 111.4 124.7 116.7 107.8  95.1 137.4 120.9
## 1837 188.0 175.6 134.6 138.2 111.3 158.0 162.8 134.0  96.3 123.7 107.0
## 1838 144.9  84.8 140.8 126.6 137.6  94.5 108.2  78.8  73.6  90.8  77.4
## 1839 107.6 102.5  77.7  61.8  53.8  54.6  84.7 131.2 132.7  90.8  68.8
## 1840  81.2  87.7  55.5  65.9  69.2  48.5  60.7  57.8  74.0  49.8  54.3
## 1841  24.0  29.9  29.7  42.6  67.4  55.7  30.8  39.3  35.1  28.5  19.8
## 1842  20.4  22.1  21.7  26.9  24.9  20.5  12.6  26.5  18.5  38.1  40.5
## 1843  13.3   3.5   8.3   8.8  21.1  10.5   9.5  11.8   4.2   5.3  19.1
## 1844   9.4  14.7  13.6  20.8  12.0   3.7  21.2  23.9   6.9  21.5  10.7
## 1845  25.7  43.6  43.3  56.9  47.8  31.1  30.6  32.3  29.6  40.7  39.4
## 1846  38.7  51.0  63.9  69.2  59.9  65.1  46.5  54.8 107.1  55.9  60.4
## 1847  62.6  44.9  85.7  44.7  75.4  85.3  52.2 140.6 161.2 180.4 138.9
## 1848 159.1 111.8 108.9 107.1 102.2 123.8 139.2 132.5 100.3 132.4 114.6
## 1849 156.7 131.7  96.5 102.5  80.6  81.2  78.0  61.3  93.7  71.5  99.7
## 1850  78.0  89.4  82.6  44.1  61.6  70.0  39.1  61.6  86.2  71.0  54.8
## 1851  75.5 105.4  64.6  56.5  62.6  63.2  36.1  57.4  67.9  62.5  50.9
## 1852  68.4  67.5  61.2  65.4  54.9  46.9  42.0  39.7  37.5  67.3  54.3
## 1853  41.1  42.9  37.7  47.6  34.7  40.0  45.9  50.4  33.5  42.3  28.8
## 1854  15.4  20.0  20.7  26.4  24.0  21.1  18.7  15.8  22.4  12.7  28.2
## 1855  12.3  11.4  17.4   4.4   9.1   5.3   0.4   3.1   0.0   9.7   4.3
## 1856   0.5   4.9   0.4   6.5   0.0   5.0   4.6   5.9   4.4   4.5   7.7
## 1857  13.7   7.4   5.2  11.1  29.2  16.0  22.2  16.9  42.4  40.6  31.4
## 1858  39.0  34.9  57.5  38.3  41.4  44.5  56.7  55.3  80.1  91.2  51.9
## 1859  83.7  87.6  90.3  85.7  91.0  87.1  95.2 106.8 105.8 114.6  97.2
## 1860  81.5  88.0  98.9  71.4 107.1 108.6 116.7 100.3  92.2  90.1  97.9
## 1861  62.3  77.8 101.0  98.5  56.8  87.8  78.0  82.5  79.9  67.2  53.7
## 1862  63.1  64.5  43.6  53.7  64.4  84.0  73.4  62.5  66.6  42.0  50.6
## 1863  48.3  56.7  66.4  40.6  53.8  40.8  32.7  48.1  22.0  39.9  37.7
## 1864  57.7  47.1  66.3  35.8  40.6  57.8  54.7  54.8  28.5  33.9  57.6
## 1865  48.7  39.3  39.5  29.4  34.5  33.6  26.8  37.8  21.6  17.1  24.6
## 1866  31.6  38.4  24.6  17.6  12.9  16.5   9.3  12.7   7.3  14.1   9.0
## 1867   0.0   0.7   9.2   5.1   2.9   1.5   5.0   4.9   9.8  13.5   9.3
## 1868  15.6  15.8  26.5  36.6  26.7  31.1  28.6  34.4  43.8  61.7  59.1
## 1869  60.9  59.3  52.7  41.0 104.0 108.4  59.2  79.6  80.6  59.4  77.4
## 1870  77.3 114.9 159.4 160.0 176.0 135.6 132.4 153.8 136.0 146.4 147.5
## 1871  88.3 125.3 143.2 162.4 145.5  91.7 103.0 110.0  80.3  89.0 105.4
## 1872  79.5 120.1  88.4 102.1 107.6 109.9 105.5  92.9 114.6 103.5 112.0
## 1873  86.7 107.0  98.3  76.2  47.9  44.8  66.9  68.2  47.5  47.4  55.4
## 1874  60.8  64.2  46.4  32.0  44.6  38.2  67.8  61.3  28.0  34.3  28.9
## 1875  14.6  22.2  33.8  29.1  11.5  23.9  12.5  14.6   2.4  12.7  17.7
## 1876  14.3  15.0  31.2   2.3   5.1   1.6  15.2   8.8   9.9  14.3   9.9
## 1877  24.4   8.7  11.7  15.8  21.2  13.4   5.9   6.3  16.4   6.7  14.5
## 1878   3.3   6.0   7.8   0.1   5.8   6.4   0.1   0.0   5.3   1.1   4.1
## 1879   0.8   0.6   0.0   6.2   2.4   4.8   7.5  10.7   6.1  12.3  12.9
## 1880  24.0  27.5  19.5  19.3  23.5  34.1  21.9  48.1  66.0  43.0  30.7
## 1881  36.4  53.2  51.5  51.7  43.5  60.5  76.9  58.0  53.2  64.0  54.8
## 1882  45.0  69.3  67.5  95.8  64.1  45.2  45.4  40.4  57.7  59.2  84.4
## 1883  60.6  46.9  42.8  82.1  32.1  76.5  80.6  46.0  52.6  83.8  84.5
## 1884  91.5  86.9  86.8  76.1  66.5  51.2  53.1  55.8  61.9  47.8  36.6
## 1885  42.8  71.8  49.8  55.0  73.0  83.7  66.5  50.0  39.6  38.7  33.3
## 1886  29.9  25.9  57.3  43.7  30.7  27.1  30.3  16.9  21.4   8.6   0.3
## 1887  10.3  13.2   4.2   6.9  20.0  15.7  23.3  21.4   7.4   6.6   6.9
## 1888  12.7   7.1   7.8   5.1   7.0   7.1   3.1   2.8   8.8   2.1  10.7
## 1889   0.8   8.5   7.0   4.3   2.4   6.4   9.7  20.6   6.5   2.1   0.2
## 1890   5.3   0.6   5.1   1.6   4.8   1.3  11.6   8.5  17.2  11.2   9.6
## 1891  13.5  22.2  10.4  20.5  41.1  48.3  58.8  33.2  53.8  51.5  41.9
## 1892  69.1  75.6  49.9  69.6  79.6  76.3  76.8 101.4  62.8  70.5  65.4
## 1893  75.0  73.0  65.7  88.1  84.7  88.2  88.8 129.2  77.9  79.7  75.1
## 1894  83.2  84.6  52.3  81.6 101.2  98.9 106.0  70.3  65.9  75.5  56.6
## 1895  63.3  67.2  61.0  76.9  67.5  71.5  47.8  68.9  57.7  67.9  47.2
## 1896  29.0  57.4  52.0  43.8  27.7  49.0  45.0  27.2  61.3  28.4  38.0
## 1897  40.6  29.4  29.1  31.0  20.0  11.3  27.6  21.8  48.1  14.3   8.4
## 1898  30.2  36.4  38.3  14.5  25.8  22.3   9.0  31.4  34.8  34.4  30.9
## 1899  19.5   9.2  18.1  14.2   7.7  20.5  13.5   2.9   8.4  13.0   7.8
## 1900   9.4  13.6   8.6  16.0  15.2  12.1   8.3   4.3   8.3  12.9   4.5
## 1901   0.2   2.4   4.5   0.0  10.2   5.8   0.7   1.0   0.6   3.7   3.8
## 1902   5.2   0.0  12.4   0.0   2.8   1.4   0.9   2.3   7.6  16.3  10.3
## 1903   8.3  17.0  13.5  26.1  14.6  16.3  27.9  28.8  11.1  38.9  44.5
## 1904  31.6  24.5  37.2  43.0  39.5  41.9  50.6  58.2  30.1  54.2  38.0
## 1905  54.8  85.8  56.5  39.3  48.0  49.0  73.0  58.8  55.0  78.7 107.2
## 1906  45.5  31.3  64.5  55.3  57.7  63.2 103.6  47.7  56.1  17.8  38.9
## 1907  76.4 108.2  60.7  52.6  42.9  40.4  49.7  54.3  85.0  65.4  61.5
## 1908  39.2  33.9  28.7  57.6  40.8  48.1  39.5  90.5  86.9  32.3  45.5
## 1909  56.7  46.6  66.3  32.3  36.0  22.6  35.8  23.1  38.8  58.4  55.8
## 1910  26.4  31.5  21.4   8.4  22.2  12.3  14.1  11.5  26.2  38.3   4.9
## 1911   3.4   9.0   7.8  16.5   9.0   2.2   3.5   4.0   4.0   2.6   4.2
## 1912   0.3   0.0   4.9   4.5   4.4   4.1   3.0   0.3   9.5   4.6   1.1
## 1913   2.3   2.9   0.5   0.9   0.0   0.0   1.7   0.2   1.2   3.1   0.7
## 1914   2.8   2.6   3.1  17.3   5.2  11.4   5.4   7.7  12.7   8.2  16.4
## 1915  23.0  42.3  38.8  41.3  33.0  68.8  71.6  69.6  49.5  53.5  42.5
## 1916  45.3  55.4  67.0  71.8  74.5  67.7  53.5  35.2  45.1  50.7  65.6
## 1917  74.7  71.9  94.8  74.7 114.1 114.9 119.8 154.5 129.4  72.2  96.4
## 1918  96.0  65.3  72.2  80.5  76.7  59.4 107.6 101.7  79.9  85.0  83.4
## 1919  48.1  79.5  66.5  51.8  88.1 111.2  64.7  69.0  54.7  52.8  42.0
## 1920  51.1  53.9  70.2  14.8  33.3  38.7  27.5  19.2  36.3  49.6  27.2
## 1921  31.5  28.3  26.7  32.4  22.2  33.7  41.9  22.8  17.8  18.2  17.8
## 1922  11.8  26.4  54.7  11.0   8.0   5.8  10.9   6.5   4.7   6.2   7.4
## 1923   4.5   1.5   3.3   6.1   3.2   9.1   3.5   0.5  13.2  11.6  10.0
## 1924   0.5   5.1   1.8  11.3  20.8  24.0  28.1  19.3  25.1  25.6  22.5
## 1925   5.5  23.2  18.0  31.7  42.8  47.5  38.5  37.9  60.2  69.2  58.6
## 1926  71.8  70.0  62.5  38.5  64.3  73.5  52.3  61.6  60.8  71.5  60.5
## 1927  81.6  93.0  69.6  93.5  79.1  59.1  54.9  53.8  68.4  63.1  67.2
## 1928  83.5  73.5  85.4  80.6  76.9  91.4  98.0  83.8  89.7  61.4  50.3
## 1929  68.9  64.1  50.2  52.8  58.2  71.9  70.2  65.8  34.4  54.0  81.1
## 1930  65.3  49.2  35.0  38.2  36.8  28.8  21.9  24.9  32.1  34.4  35.6
## 1931  14.6  43.1  30.0  31.2  24.6  15.3  17.4  13.0  19.0  10.0  18.7
## 1932  12.1  10.6  11.2  11.2  17.9  22.2   9.6   6.8   4.0   8.9   8.2
## 1933  12.3  22.2  10.1   2.9   3.2   5.2   2.8   0.2   5.1   3.0   0.6
## 1934   3.4   7.8   4.3  11.3  19.7   6.7   9.3   8.3   4.0   5.7   8.7
## 1935  18.9  20.5  23.1  12.2  27.3  45.7  33.9  30.1  42.1  53.2  64.2
## 1936  62.8  74.3  77.1  74.9  54.6  70.0  52.3  87.0  76.0  89.0 115.4
## 1937 132.5 128.5  83.9 109.3 116.7 130.3 145.1 137.7 100.7 124.9  74.4
## 1938  98.4 119.2  86.5 101.0 127.4  97.5 165.3 115.7  89.6  99.1 122.2
## 1939  80.3  77.4  64.6 109.1 118.3 101.0  97.6 105.8 112.6  88.1  68.1
## 1940  50.5  59.4  83.3  60.7  54.4  83.9  67.5 105.5  66.5  55.0  58.4
## 1941  45.6  44.5  46.4  32.8  29.5  59.8  66.9  60.0  65.9  46.3  38.3
## 1942  35.6  52.8  54.2  60.7  25.0  11.4  17.7  20.2  17.2  19.2  30.7
## 1943  12.4  28.9  27.4  26.1  14.1   7.6  13.2  19.4  10.0   7.8  10.2
## 1944   3.7   0.5  11.0   0.3   2.5   5.0   5.0  16.7  14.3  16.9  10.8
## 1945  18.5  12.7  21.5  32.0  30.6  36.2  42.6  25.9  34.9  68.8  46.0
## 1946  47.6  86.2  76.6  75.7  84.9  73.5 116.2 107.2  94.4 102.3 123.8
## 1947 115.7 113.4 129.8 149.8 201.3 163.9 157.9 188.8 169.4 163.6 128.0
## 1948 108.5  86.1  94.8 189.7 174.0 167.8 142.2 157.9 143.3 136.3  95.8
## 1949 119.1 182.3 157.5 147.0 106.2 121.7 125.8 123.8 145.3 131.6 143.5
## 1950 101.6  94.8 109.7 113.4 106.2  83.6  91.0  85.2  51.3  61.4  54.8
## 1951  59.9  59.9  59.9  92.9 108.5 100.6  61.5  61.0  83.1  51.6  52.4
## 1952  40.7  22.7  22.0  29.1  23.4  36.4  39.3  54.9  28.2  23.8  22.1
## 1953  26.5   3.9  10.0  27.8  12.5  21.8   8.6  23.5  19.3   8.2   1.6
## 1954   0.2   0.5  10.9   1.8   0.8   0.2   4.8   8.4   1.5   7.0   9.2
## 1955  23.1  20.8   4.9  11.3  28.9  31.7  26.7  40.7  42.7  58.5  89.2
## 1956  73.6 124.0 118.4 110.7 136.6 116.6 129.1 169.6 173.2 155.3 201.3
## 1957 165.0 130.2 157.4 175.2 164.6 200.7 187.2 158.0 235.8 253.8 210.9
## 1958 202.5 164.9 190.7 196.0 175.3 171.5 191.4 200.2 201.2 181.5 152.3
## 1959 217.4 143.1 185.7 163.3 172.0 168.7 149.6 199.6 145.2 111.4 124.0
## 1960 146.3 106.0 102.2 122.0 119.6 110.2 121.7 134.1 127.2  82.8  89.6
## 1961  57.9  46.1  53.0  61.4  51.0  77.4  70.2  55.9  63.6  37.7  32.6
## 1962  38.7  50.3  45.6  46.4  43.7  42.0  21.8  21.8  51.3  39.5  26.9
## 1963  19.8  24.4  17.1  29.3  43.0  35.9  19.6  33.2  38.8  35.3  23.4
## 1964  15.3  17.7  16.5   8.6   9.5   9.1   3.1   9.3   4.7   6.1   7.4
## 1965  17.5  14.2  11.7   6.8  24.1  15.9  11.9   8.9  16.8  20.1  15.8
## 1966  28.2  24.4  25.3  48.7  45.3  47.7  56.7  51.2  50.2  57.2  57.2
## 1967 110.9  93.6 111.8  69.5  86.5  67.3  91.5 107.2  76.8  88.2  94.3
## 1968 121.8 111.9  92.2  81.2 127.2 110.3  96.1 109.3 117.2 107.7  86.0
## 1969 104.4 120.5 135.8 106.8 120.0 106.0  96.8  98.0  91.3  95.7  93.5
## 1970 111.5 127.8 102.9 109.5 127.5 106.8 112.5  93.0  99.5  86.6  95.2
## 1971  91.3  79.0  60.7  71.8  57.5  49.8  81.0  61.4  50.2  51.7  63.2
## 1972  61.5  88.4  80.1  63.2  80.5  88.0  76.5  76.8  64.0  61.3  41.6
## 1973  43.4  42.9  46.0  57.7  42.4  39.5  23.1  25.6  59.3  30.7  23.9
## 1974  27.6  26.0  21.3  40.3  39.5  36.0  55.8  33.6  40.2  47.1  25.0
## 1975  18.9  11.5  11.5   5.1   9.0  11.4  28.2  39.7  13.9   9.1  19.4
## 1976   8.1   4.3  21.9  18.8  12.4  12.2   1.9  16.4  13.5  20.6   5.2
## 1977  16.4  23.1   8.7  12.9  18.6  38.5  21.4  30.1  44.0  43.8  29.1
## 1978  51.9  93.6  76.5  99.7  82.7  95.1  70.4  58.1 138.2 125.1  97.9
## 1979 166.6 137.5 138.0 101.5 134.4 149.5 159.4 142.2 188.4 186.2 183.3
## 1980 159.6 155.0 126.2 164.1 179.9 157.3 136.3 135.4 155.0 164.7 147.9
## 1981 114.0 141.3 135.5 156.4 127.5  90.0 143.8 158.7 167.3 162.4 137.5
## 1982 111.2 163.6 153.8 122.0  82.2 110.4 106.1 107.6 118.8  94.7  98.1
## 1983  84.3  51.0  66.5  80.7  99.2  91.1  82.2  71.8  50.3  55.8  33.3
##        Dec
## 1749  85.2
## 1750  75.4
## 1751  44.0
## 1752  40.0
## 1753   6.7
## 1754   4.2
## 1755  20.0
## 1756   9.4
## 1757  33.5
## 1758  43.0
## 1759  57.0
## 1760  61.0
## 1761  46.0
## 1762  77.2
## 1763  61.4
## 1764  25.7
## 1765  13.0
## 1766  19.2
## 1767  53.3
## 1768 111.8
## 1769 112.0
## 1770 102.3
## 1771  95.7
## 1772  64.0
## 1773  43.2
## 1774  12.2
## 1775   7.9
## 1776  40.0
## 1777 157.3
## 1778 105.0
## 1779 110.0
## 1780  58.7
## 1781  35.2
## 1782  30.0
## 1783  10.5
## 1784  14.0
## 1785  27.3
## 1786 112.7
## 1787 174.0
## 1788 129.5
## 1789 135.5
## 1790  74.0
## 1791  66.0
## 1792  56.0
## 1793  45.7
## 1794  51.4
## 1795  18.0
## 1796   5.1
## 1797   3.0
## 1798   9.9
## 1799   8.6
## 1800  40.1
## 1801  48.2
## 1802  50.0
## 1803  48.0
## 1804  60.0
## 1805  38.3
## 1806  24.0
## 1807   0.0
## 1808  12.3
## 1809   0.0
## 1810   0.0
## 1811   1.1
## 1812  10.1
## 1813  14.3
## 1814  20.1
## 1815  65.0
## 1816  29.9
## 1817  28.4
## 1818  25.8
## 1819  30.6
## 1820   9.7
## 1821   0.0
## 1822   0.0
## 1823  20.4
## 1824   0.8
## 1825  22.0
## 1826  68.1
## 1827  46.1
## 1828  46.6
## 1829  56.5
## 1830  82.1
## 1831  28.9
## 1832  27.5
## 1833   9.9
## 1834  34.5
## 1835  77.5
## 1836 206.2
## 1837 129.8
## 1838  79.8
## 1839  63.6
## 1840  53.7
## 1841  38.8
## 1842  17.6
## 1843  12.7
## 1844  21.6
## 1845  59.7
## 1846  65.5
## 1847 109.6
## 1848 159.9
## 1849  97.0
## 1850  60.0
## 1851  71.4
## 1852  45.4
## 1853  23.4
## 1854  21.4
## 1855   3.1
## 1856   7.2
## 1857  37.2
## 1858  66.9
## 1859  81.0
## 1860  95.6
## 1861  80.5
## 1862  40.9
## 1863  41.2
## 1864  28.6
## 1865  12.8
## 1866   1.5
## 1867  25.2
## 1868  67.6
## 1869 104.3
## 1870 130.0
## 1871  90.3
## 1872  83.9
## 1873  49.2
## 1874  29.3
## 1875   9.9
## 1876   8.2
## 1877   2.3
## 1878   0.5
## 1879   7.2
## 1880  29.6
## 1881  47.3
## 1882  41.8
## 1883  75.9
## 1884  47.2
## 1885  21.7
## 1886  12.4
## 1887  20.7
## 1888   6.7
## 1889   6.7
## 1890   7.8
## 1891  32.3
## 1892  78.6
## 1893  93.8
## 1894  60.0
## 1895  70.7
## 1896  42.6
## 1897  33.3
## 1898  12.6
## 1899  10.5
## 1900   0.3
## 1901   0.0
## 1902   1.1
## 1903  45.6
## 1904  54.6
## 1905  55.5
## 1906  64.7
## 1907  47.3
## 1908  39.5
## 1909  54.2
## 1910   5.8
## 1911   2.2
## 1912   6.4
## 1913   3.8
## 1914  22.3
## 1915  34.5
## 1916  53.0
## 1917 129.3
## 1918  59.2
## 1919  34.9
## 1920  29.9
## 1921  20.3
## 1922  17.5
## 1923   2.8
## 1924  16.5
## 1925  98.6
## 1926  79.4
## 1927  45.2
## 1928  59.0
## 1929 108.0
## 1930  25.8
## 1931  17.8
## 1932  11.0
## 1933   0.3
## 1934  15.4
## 1935  61.5
## 1936 123.4
## 1937  88.8
## 1938  92.7
## 1939  42.1
## 1940  68.3
## 1941  33.7
## 1942  22.5
## 1943  18.8
## 1944  28.4
## 1945  27.4
## 1946 121.7
## 1947 116.5
## 1948 138.0
## 1949 117.6
## 1950  54.1
## 1951  45.8
## 1952  34.3
## 1953   2.5
## 1954   7.6
## 1955  76.9
## 1956 192.1
## 1957 239.4
## 1958 187.6
## 1959 125.0
## 1960  85.6
## 1961  40.0
## 1962  23.2
## 1963  14.9
## 1964  15.1
## 1965  17.0
## 1966  70.4
## 1967 126.4
## 1968 109.8
## 1969  97.9
## 1970  83.5
## 1971  82.2
## 1972  45.3
## 1973  23.3
## 1974  20.5
## 1975   7.8
## 1976  15.3
## 1977  43.2
## 1978 122.7
## 1979 176.3
## 1980 174.4
## 1981 150.1
## 1982 127.0
## 1983  33.4
##
## $train_data ## V1 V2 V3 V4 V5 V6 ## 1: 0.2285264 0.2466509 0.2758077 0.2194641 0.3349094 0.3289992 ## 2: 0.2888101 0.2990544 0.3514578 0.3479117 0.3546099 0.3940110 ## 3: 0.2758077 0.1713948 0.1784870 0.2222222 0.2391647 0.1997636 ## 4: 0.1379039 0.1970055 0.2797478 0.2336485 0.2352246 0.1560284 ## 5: 0.1733649 0.1260835 0.1800630 0.1497242 0.1418440 0.1249015 ## --- ## 231: 0.6564224 0.5417652 0.5437352 0.3999212 0.5295508 0.5890465 ## 232: 0.6288416 0.6107171 0.4972419 0.6465721 0.7088258 0.6197794 ## 233: 0.4491726 0.5567376 0.5338849 0.6162333 0.5023641 0.3546099 ## 234: 0.4381403 0.6446020 0.6059890 0.4806935 0.3238771 0.4349882 ## 235: 0.3321513 0.2009456 0.2620173 0.3179669 0.3908589 0.3589441 ## V7 V8 V9 V10 V11 V12 ## 1: 0.37352246 0.2612293 0.29905437 0.29747833 0.62490150 0.33569740 ## 2: 0.33648542 0.4058314 0.35933806 0.25886525 0.24940898 0.29708432 ## 3: 0.26122931 0.2356186 0.09259259 0.09141056 0.11229314 0.17336485 ## 4: 0.30890465 0.1154452 0.10677699 0.18360914 0.14814815 0.15760441 ## 5: 0.08747045 0.1536643 0.11032309 0.09850276 0.07880221 0.02639874 ## --- ## 231: 0.62805359 0.5602837 0.74231678 0.73364854 0.72222222 0.69464145 ## 232: 0.53703704 0.5334909 0.61071710 0.64893617 0.58274232 0.68715524 ## 233: 0.56658786 0.6252955 0.65918046 0.63987392 0.54176517 0.59141056 ## 234: 0.41804571 0.4239559 0.46808511 0.37312845 0.38652482 0.50039401 ## 235: 0.32387707 0.2828999 0.19818755 0.21985816 0.13120567 0.13159968 ## ##$k
## [1] 2
##
## $w ## [1] 7 ## ##$cycle
## [1] 12
##
## $dmin ## [1] 0 ## ##$dmax
## [1] 253.8
##
## attr(,"class")
## [1] "psf"

#### Perform predictions from trained PSF model for ’nottem’ dataset using the standard predict() function.

nottem_preds <- predict(nottem_model, n.ahead = 12)
nottem_preds
##  [1] 39.83125 38.96250 42.39375 46.41250 52.35000 58.12500 62.51250
##  [8] 61.21875 56.69375 49.73750 42.55625 39.10625

#### Perform predictions from trained PSF model for ’sunspots’ dataset using the standard predict() function.

sunspots_preds <- predict(sunspots_model, n.ahead = 48)
sunspots_preds
##  [1] 48.35000 57.15000 54.30000 61.05000 52.85000 51.50000 33.80000
##  [8] 40.90000 55.15000 49.25000 41.95000 39.60000 34.10000 27.76667
## [15] 27.53333 36.70000 35.93333 36.23333 26.06667 28.66667 27.36667
## [22] 30.16667 23.00000 21.03333 15.67000 15.58000 18.77000 14.94000
## [29] 18.27000 15.11000 15.72000 19.50000 12.50000 13.36000 21.89000
## [36] 16.47000 14.80833 14.08333 12.75833 12.76667 14.11667 14.04167
## [43] 10.89167 15.78333 14.45833 21.05833 20.89167 21.56667

To represent the prediction performance in plot format, the plot() function is used as shown in the following code.

#### Plot for ‘nottem’ dataset

plot(nottem_model, nottem_preds)

#### Plot for ‘sunspots’ dataset

plot(sunspots_model, sunspots_preds)

## Comparison of psf() with auto.arima() and ets() functions:

Example below shows the comparisons for psf(), auto.arima() and ets() functions when using the Root Mean Square Error (RMSE) parameter as metric, for ’sunspots’ dataset. In order to avail more accurate and robust comparison results, error values are calculated for 5 times and the mean value of error values for methods under comparison are also shown. These values clearly state that ‘psf()’ function is able to outperform the comparative time series prediction methods. Additionally, the reader might want to refer to the results published in the original work Martinez Alvarez et al. (2011), in which it was shown that PSF outperformed many different methods when applied to electricity prices and demand forecasting.

library(PSF)
library(forecast)
options(warn=-1)

## Consider data sunspots with removal of last years's readings
# Training Data
train <- sunspots[1:2772]

# Test Data
test <- sunspots[2773:2820]

PSF <- NULL
ARIMA <- NULL
ETS <- NULL

for(i in 1:5)
{
set.seed(i)

# for PSF
psf_model <- psf(train)
a <- predict(psf_model, n.ahead = 48)

# for ARIMA
b <- forecast(auto.arima(train), 48)$mean # for ets c <- as.numeric(forecast(ets(train), 48)$mean)

## For Error Calculations
# Error for PSF
PSF[i] <- sqrt(mean((test - a)^2))
# Error for ARIMA
ARIMA[i] <- sqrt(mean((test - b)^2))
# Error for ETS
ETS[i] <- sqrt(mean((test - c)^2))

}

## Error values for PSF
PSF
## [1] 61.08512 61.08512 61.08512 61.08512 61.08512
  mean(PSF)
## [1] 61.08512
## Error values for ARIMA
ARIMA
## [1] 103.0719 103.0719 103.0719 103.0719 103.0719
  mean(ARIMA)
## [1] 103.0719
## Error values for ETS
ETS
## [1] 70.66554 70.66554 70.66554 70.66554 70.66554
  mean(ETS)
## [1] 70.66554

## References

Martínez-Álvarez, F., Troncoso, A., Riquelme, J.C. and Ruiz, J.S.A., 2008, December. LBF: A labeled-based forecasting algorithm and its application to electricity price time series. In Data Mining, 2008. ICDM’08. Eighth IEEE International Conference on (pp. 453-461). IEEE.

Martinez Alvarez, F., Troncoso, A., Riquelme, J.C. and Aguilar Ruiz, J.S., 2011. Energy time series forecasting based on pattern sequence similarity. Knowledge and Data Engineering, IEEE Transactions on, 23(8), pp.1230-1243.