Pull in the data.

project1_624 <- read.csv("https://raw.githubusercontent.com/juliaDataScience-22/project1-624/main/project1_624.csv")
View(project1_624)

s02 <- project1_624[project1_624$category == "S02", ]
s06 <- project1_624[project1_624$category == "S06", ]

Then show some graphs

plot(s02$Var02)

plot(s02$Var03)

plot(s06$Var05)

Then forecast var02.

Separated by months. Not by days. Could switch. Check with group.

Based on all these options, prophet and BSTS seem to be the best options.

# Option 1 using ARIMA
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
ts_var02 <- ts(na.omit(s02$Var02), frequency = 12)

model <- auto.arima(ts_var02)

forecasted <- forecast(model, h = 140)

s02_part2 <- s02
s02_part2$Var02 <- c(s02_part2$Var02[1:1622], forecasted$mean)

print(forecasted)
##         Point Forecast     Lo 80    Hi 80     Lo 95     Hi 95
## Mar 136       25651647  -6700799 58004093 -23827150  75130443
## Apr 136       28018506  -8660865 64697877 -28077751  84114764
## May 136       28780507  -9177382 66738396 -29271075  86832088
## Jun 136       28061544 -10371026 66494114 -30716000  86839088
## Jul 136       29039536  -9616812 67695885 -30080247  88159320
## Aug 136       29184461  -9605916 67974838 -30140301  88509223
## Sep 136       29876255  -9011994 68764504 -29598190  89350700
## Oct 136       29826126  -9144053 68796305 -29773619  89425872
## Nov 136       30249191  -8795446 69293827 -29464429  89962810
## Dec 136       30204620  -8910827 69320068 -29617294  90026535
## Jan 137       29891821  -9292595 69076237 -30035572  89819214
## Feb 137       30122107  -9130312 69374525 -29909287  90153501
## Mar 137       29971208  -9294409 69236825 -30080372  90022788
## Apr 137       29884906  -9413816 69183627 -30217303  89987114
## May 137       29805540  -9539532 69150613 -30367555  89978636
## Jun 137       29658479  -9740333 69057292 -30596804  89913763
## Jul 137       29710562  -9745830 69166954 -30632782  90053906
## Aug 137       29714973  -9800914 69230860 -30719361  90149307
## Sep 137       29765068  -9811234 69341369 -30761662  90291797
## Oct 137       29755752  -9881377 69392880 -30864005  90375509
## Nov 137       29791326  -9906789 69489441 -30921701  90504354
## Dec 137       29783311  -9975823 69542444 -31023037  90589658
## Jan 138       29754651 -10065470 69574772 -31144969  90654271
## Feb 138       29781818 -10099229 69662864 -31210980  90774615
## Mar 138       29760970 -10174171 69696111 -31314558  90836498
## Apr 138       29751448 -10240814 69743710 -31411439  90914335
## May 138       29750430 -10300447 69801307 -31502101  91002961
## Jun 138       29760202 -10349998 69870401 -31583055  91103458
## Jul 138       29752784 -10417047 69922615 -31681671  91187239
## Aug 138       29751816 -10477756 69981388 -31774005  91277636
## Sep 138       29746042 -10543280 70035365 -31871160  91363244
## Oct 138       29746687 -10602348 70095721 -31961837  91455210
## Nov 138       29742960 -10665723 70151642 -32056787  91542707
## Dec 138       29743522 -10724733 70211776 -32147333  91634376
## Jan 139       29746371 -10781374 70274116 -32235467  91728209
## Feb 139       29744033 -10843119 70331184 -32328660  91816725
## Mar 139       29745686 -10901439 70392810 -32418727  91910098
## Apr 139       29746536 -10960170 70453242 -32508999  92002070
## May 139       29747010 -11019040 70513061 -32599283  92093304
## Jun 139       29747427 -11077806 70572660 -32689379  92184233
## Jul 139       29747437 -11136857 70631730 -32779694  92274567
## Aug 139       29747451 -11195799 70690701 -32869846  92364748
## Sep 139       29747406 -11254706 70749518 -32959913  92454725
## Oct 139       29747431 -11313454 70808317 -33049774  92544637
## Nov 139       29747385 -11372188 70866957 -33139575  92634344
## Dec 139       29747406 -11430769 70925581 -33229178  92723990
## Jan 140       29747450 -11489243 70984143 -33318630  92813530
## Feb 140       29747394 -11547734 71042523 -33408055  92902843
## Mar 140       29747443 -11606050 71100935 -33497266  92992152
## Apr 140       29747461 -11664308 71159230 -33586374  93081296
## May 140       29747448 -11722513 71217408 -33675384  93170279
## Jun 140       29747378 -11780692 71275447 -33764324  93259079
## Jul 140       29747418 -11838678 71333514 -33853028  93347864
## Aug 140       29747423 -11896620 71391465 -33941644  93436489
## Sep 140       29747456 -11954451 71449363 -34030108  93525019
## Oct 140       29747451 -12012241 71507144 -34118486  93613389
## Nov 140       29747474 -12069923 71564871 -34206716  93701664
## Dec 140       29747470 -12127553 71622492 -34294851  93789790
## Jan 141       29747452 -12185116 71680021 -34382878  93877782
## Feb 141       29747467 -12242569 71737504 -34470751  93965686
## Mar 141       29747456 -12299965 71794878 -34558525  94053438
## Apr 141       29747451 -12357279 71852181 -34646176  94141078
## May 141       29747449 -12414512 71909410 -34733706  94228604
## Jun 141       29747450 -12471666 71966565 -34821116  94316015
## Jul 141       29747448 -12528745 72023641 -34908410  94403306
## Aug 141       29747448 -12585746 72080641 -34995585  94490480
## Sep 141       29747447 -12642671 72137564 -35082644  94577537
## Oct 141       29747447 -12699519 72194412 -35169585  94664478
## Nov 141       29747446 -12756291 72251183 -35256410  94751302
## Dec 141       29747446 -12812987 72307879 -35343119  94838011
## Jan 142       29747446 -12869607 72364500 -35429712  94924605
## Feb 142       29747446 -12926152 72421045 -35516191  95011083
## Mar 142       29747446 -12982623 72477515 -35602555  95097448
## Apr 142       29747446 -13039019 72533912 -35688805  95183698
## May 142       29747447 -13095340 72590234 -35774942  95269835
## Jun 142       29747447 -13151588 72646482 -35860965  95355859
## Jul 142       29747447 -13207762 72702656 -35946876  95441770
## Aug 142       29747447 -13263863 72758756 -36032675  95527568
## Sep 142       29747447 -13319891 72814784 -36118362  95613255
## Oct 142       29747447 -13375846 72870739 -36203938  95698831
## Nov 142       29747447 -13431728 72926621 -36289403  95784296
## Dec 142       29747447 -13487538 72982432 -36374757  95869650
## Jan 143       29747447 -13543277 73038170 -36460001  95954894
## Feb 143       29747447 -13598943 73093836 -36545136  96040029
## Mar 143       29747447 -13654538 73149432 -36630161  96125055
## Apr 143       29747447 -13710062 73204956 -36715078  96209971
## May 143       29747447 -13765516 73260409 -36799886  96294780
## Jun 143       29747447 -13820898 73315791 -36884587  96379480
## Jul 143       29747447 -13876210 73371104 -36969180  96464073
## Aug 143       29747447 -13931453 73426346 -37053665  96548559
## Sep 143       29747447 -13986625 73481518 -37138044  96632938
## Oct 143       29747447 -14041728 73536621 -37222317  96717210
## Nov 143       29747447 -14096762 73591655 -37306484  96801377
## Dec 143       29747447 -14151727 73646620 -37390545  96885438
## Jan 144       29747447 -14206622 73701516 -37474501  96969395
## Feb 144       29747447 -14261450 73756343 -37558353  97053246
## Mar 144       29747447 -14316209 73811103 -37642100  97136993
## Apr 144       29747447 -14370901 73865794 -37725743  97220636
## May 144       29747447 -14425524 73920418 -37809282  97304176
## Jun 144       29747447 -14480080 73974974 -37892719  97387612
## Jul 144       29747447 -14534569 74029463 -37976053  97470946
## Aug 144       29747447 -14588991 74083885 -38059284  97554177
## Sep 144       29747447 -14643346 74138240 -38142413  97637306
## Oct 144       29747447 -14697635 74192529 -38225440  97720334
## Nov 144       29747447 -14751858 74246751 -38308367  97803260
## Dec 144       29747447 -14806014 74300908 -38391192  97886085
## Jan 145       29747447 -14860105 74354998 -38473917  97968810
## Feb 145       29747447 -14914130 74409024 -38556541  98051434
## Mar 145       29747447 -14968090 74462984 -38639066  98133959
## Apr 145       29747447 -15021985 74516879 -38721491  98216384
## May 145       29747447 -15075816 74570709 -38803817  98298711
## Jun 145       29747447 -15129581 74624474 -38886045  98380938
## Jul 145       29747447 -15183282 74678176 -38968174  98463067
## Aug 145       29747447 -15236920 74731813 -39050205  98545098
## Sep 145       29747447 -15290493 74785386 -39132138  98627031
## Oct 145       29747447 -15344002 74838896 -39213974  98708867
## Nov 145       29747447 -15397449 74892342 -39295713  98790606
## Dec 145       29747447 -15450832 74945725 -39377355  98872248
## Jan 146       29747447 -15504152 74999045 -39458901  98953794
## Feb 146       29747447 -15557409 75052302 -39540351  99035244
## Mar 146       29747447 -15610604 75105497 -39621705  99116598
## Apr 146       29747447 -15663736 75158629 -39702964  99197858
## May 146       29747447 -15716806 75211700 -39784128  99279022
## Jun 146       29747447 -15769815 75264708 -39865198  99360091
## Jul 146       29747447 -15822762 75317655 -39946173  99441066
## Aug 146       29747447 -15875647 75370540 -40027054  99521947
## Sep 146       29747447 -15928471 75423365 -40107842  99602735
## Oct 146       29747447 -15981234 75476128 -40188536  99683429
## Nov 146       29747447 -16033937 75528830 -40269137  99764030
## Dec 146       29747447 -16086578 75581472 -40349645  99844539
## Jan 147       29747447 -16139160 75634053 -40430062  99924955
## Feb 147       29747447 -16191681 75686574 -40510386 100005279
## Mar 147       29747447 -16244142 75739035 -40590618 100085511
## Apr 147       29747447 -16296543 75791437 -40670759 100165652
## May 147       29747447 -16348885 75843778 -40750809 100245702
## Jun 147       29747447 -16401167 75896061 -40830768 100325661
## Jul 147       29747447 -16453391 75948284 -40910637 100405530
## Aug 147       29747447 -16505555 76000448 -40990415 100485308
## Sep 147       29747447 -16557661 76052554 -41070104 100564997
## Oct 147       29747447 -16609707 76104601 -41149703 100644596
plot(forecasted)

# Option 2 using prophet
library(prophet)
## Warning: package 'prophet' was built under R version 4.3.3
## Loading required package: Rcpp
## Loading required package: rlang
df <- data.frame(
  ds = seq.Date(from = as.Date("2000-01-01"), by = "month", length.out = length(na.omit(s02$Var02))),  # Replace the start date as needed
  y = na.omit(s02$Var02)
)

# Fit the Prophet model
model <- prophet(df)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
# Create future dataframe for 140 months ahead
future <- make_future_dataframe(model, periods = 140, freq = "month")

# Forecast
forecast <- predict(model, future)

# Print the forecasted values
print(tail(forecast[c('ds', 'yhat')], 140))
##              ds     yhat
## 1623 2135-03-01 35145160
## 1624 2135-04-01 34504521
## 1625 2135-05-01 33814531
## 1626 2135-06-01 33348232
## 1627 2135-07-01 36450979
## 1628 2135-08-01 33188018
## 1629 2135-09-01 29756331
## 1630 2135-10-01 28911719
## 1631 2135-11-01 29972569
## 1632 2135-12-01 35583665
## 1633 2136-01-01 33065149
## 1634 2136-02-01 32381932
## 1635 2136-03-01 35156760
## 1636 2136-04-01 34688619
## 1637 2136-05-01 34365482
## 1638 2136-06-01 32183488
## 1639 2136-07-01 36905829
## 1640 2136-08-01 32744037
## 1641 2136-09-01 31278660
## 1642 2136-10-01 29600298
## 1643 2136-11-01 29719713
## 1644 2136-12-01 33697756
## 1645 2137-01-01 34233401
## 1646 2137-02-01 33240887
## 1647 2137-03-01 35343983
## 1648 2137-04-01 34820439
## 1649 2137-05-01 34369357
## 1650 2137-06-01 32763960
## 1651 2137-07-01 36955957
## 1652 2137-08-01 33094362
## 1653 2137-09-01 30969617
## 1654 2137-10-01 29553145
## 1655 2137-11-01 29984148
## 1656 2137-12-01 34529062
## 1657 2138-01-01 34052552
## 1658 2138-02-01 33147625
## 1659 2138-03-01 35534319
## 1660 2138-04-01 34953265
## 1661 2138-05-01 34379904
## 1662 2138-06-01 33346581
## 1663 2138-07-01 36998563
## 1664 2138-08-01 33436570
## 1665 2138-09-01 30656185
## 1666 2138-10-01 29517730
## 1667 2138-11-01 30262748
## 1668 2138-12-01 35352168
## 1669 2139-01-01 33857253
## 1670 2139-02-01 33055395
## 1671 2139-03-01 35727784
## 1672 2139-04-01 35087145
## 1673 2139-05-01 34397155
## 1674 2139-06-01 33930856
## 1675 2139-07-01 37033602
## 1676 2139-08-01 33770642
## 1677 2139-09-01 30338955
## 1678 2139-10-01 29494343
## 1679 2139-11-01 30555193
## 1680 2139-12-01 36166289
## 1681 2140-01-01 33647773
## 1682 2140-02-01 32964555
## 1683 2140-03-01 35739384
## 1684 2140-04-01 35271243
## 1685 2140-05-01 34948106
## 1686 2140-06-01 32766112
## 1687 2140-07-01 37488453
## 1688 2140-08-01 33326661
## 1689 2140-09-01 31861284
## 1690 2140-10-01 30182922
## 1691 2140-11-01 30302337
## 1692 2140-12-01 34280380
## 1693 2141-01-01 34816024
## 1694 2141-02-01 33823510
## 1695 2141-03-01 35926607
## 1696 2141-04-01 35403063
## 1697 2141-05-01 34951980
## 1698 2141-06-01 33346584
## 1699 2141-07-01 37538581
## 1700 2141-08-01 33676986
## 1701 2141-09-01 31552241
## 1702 2141-10-01 30135769
## 1703 2141-11-01 30566771
## 1704 2141-12-01 35111686
## 1705 2142-01-01 34635175
## 1706 2142-02-01 33730249
## 1707 2142-03-01 36116943
## 1708 2142-04-01 35535889
## 1709 2142-05-01 34962528
## 1710 2142-06-01 33929204
## 1711 2142-07-01 37581187
## 1712 2142-08-01 34019194
## 1713 2142-09-01 31238809
## 1714 2142-10-01 30100354
## 1715 2142-11-01 30845372
## 1716 2142-12-01 35934791
## 1717 2143-01-01 34439877
## 1718 2143-02-01 33638019
## 1719 2143-03-01 36310408
## 1720 2143-04-01 35669769
## 1721 2143-05-01 34979779
## 1722 2143-06-01 34513480
## 1723 2143-07-01 37616226
## 1724 2143-08-01 34353266
## 1725 2143-09-01 30921579
## 1726 2143-10-01 30076967
## 1727 2143-11-01 31137817
## 1728 2143-12-01 36748913
## 1729 2144-01-01 34230397
## 1730 2144-02-01 33547179
## 1731 2144-03-01 36322008
## 1732 2144-04-01 35853867
## 1733 2144-05-01 35530730
## 1734 2144-06-01 33348736
## 1735 2144-07-01 38071077
## 1736 2144-08-01 33909285
## 1737 2144-09-01 32443908
## 1738 2144-10-01 30765546
## 1739 2144-11-01 30884960
## 1740 2144-12-01 34863004
## 1741 2145-01-01 35398648
## 1742 2145-02-01 34406134
## 1743 2145-03-01 36509231
## 1744 2145-04-01 35985687
## 1745 2145-05-01 35534604
## 1746 2145-06-01 33929208
## 1747 2145-07-01 38121205
## 1748 2145-08-01 34259610
## 1749 2145-09-01 32134865
## 1750 2145-10-01 30718393
## 1751 2145-11-01 31149395
## 1752 2145-12-01 35694310
## 1753 2146-01-01 35217799
## 1754 2146-02-01 34312873
## 1755 2146-03-01 36699567
## 1756 2146-04-01 36118512
## 1757 2146-05-01 35545152
## 1758 2146-06-01 34511828
## 1759 2146-07-01 38163811
## 1760 2146-08-01 34601818
## 1761 2146-09-01 31821433
## 1762 2146-10-01 30682978
# Plot the forecast
plot(model, forecast)

# Option 3 using BSTS
library(bsts)
## Warning: package 'bsts' was built under R version 4.3.3
## Loading required package: BoomSpikeSlab
## Warning: package 'BoomSpikeSlab' was built under R version 4.3.3
## Loading required package: Boom
## Warning: package 'Boom' was built under R version 4.3.3
## 
## Attaching package: 'Boom'
## The following object is masked from 'package:stats':
## 
##     rWishart
## 
## Attaching package: 'BoomSpikeSlab'
## The following object is masked from 'package:stats':
## 
##     knots
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.3.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: xts
## 
## Attaching package: 'bsts'
## The following object is masked from 'package:BoomSpikeSlab':
## 
##     SuggestBurn
ts_var02 <- ts(na.omit(s02$Var02), frequency = 12)

stateSpace <- AddLocalLevel(list(), ts_var02)

model <- bsts(ts_var02, state.specification = stateSpace, niter = 1000)
## =-=-=-=-= Iteration 0 Thu Jun 20 19:11:52 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 100 Thu Jun 20 19:11:54 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 200 Thu Jun 20 19:11:56 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 300 Thu Jun 20 19:11:58 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 400 Thu Jun 20 19:11:59 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 500 Thu Jun 20 19:12:01 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 600 Thu Jun 20 19:12:03 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 700 Thu Jun 20 19:12:05 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 800 Thu Jun 20 19:12:06 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 900 Thu Jun 20 19:12:07 2024
##  =-=-=-=-=
newForecast <- predict(model, horizon = 140)

forecasted_values <- as.numeric(newForecast$mean)

print(forecasted_values)
##   [1] 21427865 23608510 24511426 23694619 23605715 24793382 24086813 25497669
##   [9] 25417916 26587503 28198307 27147778 25347122 25756905 27265751 26106421
##  [17] 28365773 28259284 27710359 30344537 29703577 30904673 28447438 30601001
##  [25] 29681736 30787546 29944752 29283621 29765888 30338297 30392109 30099245
##  [33] 27359561 26689754 29394704 29139872 30721526 28753214 28735799 28260863
##  [41] 30745134 30250794 27676323 28340393 24178313 27760644 26746990 26008721
##  [49] 27599945 26816945 27488045 28219091 29721597 30352406 27641447 27151697
##  [57] 29320558 26758059 27245237 26799631 29525672 26765366 27666344 28459138
##  [65] 29341256 26128459 27578571 27411802 26851687 30311585 28290787 31154933
##  [73] 30375598 29002881 31334743 30232508 28730988 29774669 31492470 30258944
##  [81] 32012352 30947045 30705336 28422137 29302251 29114041 27735609 29679765
##  [89] 26747070 28242708 30132422 29004639 29378082 29026810 29466835 28594923
##  [97] 30971876 30170593 32291231 32762298 31276077 31434299 31783348 30324557
## [105] 31136897 31326286 29787674 29135105 28397591 29251919 29011510 28134240
## [113] 29407902 30454439 27606414 27521643 30155357 28416128 30090610 29799665
## [121] 31358312 30277539 31413329 27906649 31461471 28542874 29853401 30685199
## [129] 31778948 30343569 29923776 30888847 30567689 28737441 28888008 30424756
## [137] 29802721 29398130 25554164 25084763
library(ggplot2)

plot_data <- data.frame(
  Time = 1:(length(ts_var02) + 140),
  Value = c(ts_var02, forecasted_values),
  Type = c(rep("Original", length(ts_var02)), rep("Forecast", length(forecasted_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "blue"))  # Adjust colors if needed

# Option 4 using TBATS

model2 <- tbats(ts_var02)

forecastTBATS <- forecast(model2, h = 140)

new_values <- as.numeric(forecastTBATS$mean)

print(new_values)
##   [1] 23546926 24962825 25776960 26207823 26444534 26576592 26654526 26704150
##   [9] 26739035 26766184 26789228 26810042 26829598 26848398 26866702 26884647
##  [17] 26902303 26919708 26936883 26953841 26970588 26987129 27003468 27019607
##  [25] 27035551 27051300 27066857 27082225 27097405 27112401 27127213 27141845
##  [33] 27156298 27170575 27184676 27198606 27212365 27225955 27239379 27252639
##  [41] 27265735 27278672 27291449 27304069 27316534 27328847 27341007 27353018
##  [49] 27364881 27376598 27388171 27399601 27410890 27422040 27433052 27443928
##  [57] 27454670 27465279 27475757 27486105 27496326 27506420 27516388 27526234
##  [65] 27535957 27545560 27555044 27564410 27573660 27582795 27591817 27600727
##  [73] 27609526 27618215 27626797 27635272 27643641 27651907 27660069 27668130
##  [81] 27676090 27683951 27691715 27699381 27706952 27714428 27721811 27729102
##  [89] 27736302 27743412 27750434 27757367 27764214 27770975 27777652 27784246
##  [97] 27790756 27797186 27803535 27809804 27815995 27822108 27828145 27834106
## [105] 27839993 27845806 27851545 27857213 27862810 27868337 27873794 27879183
## [113] 27884504 27889758 27894947 27900070 27905129 27910124 27915056 27919927
## [121] 27924736 27929485 27934174 27938804 27943376 27947890 27952348 27956750
## [129] 27961096 27965387 27969624 27973808 27977940 27982019 27986047 27990024
## [137] 27993951 27997828 28001657 28005437
plot_data <- data.frame(
  Time = 1:(length(ts_var02) + 140),
  Value = c(ts_var02, new_values),
  Type = c(rep("Original", length(ts_var02)), rep("Forecast", length(new_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "red"))  # Adjust colors if needed

There appeared to be an outlier in the data for var03, so that will be removed first. The 1573rd value is an outlier, so that will be removed.

Then forecast var03. Prophet appears to be the best option.

# Calculate Z-scores
z_scores <- abs(scale(s02$Var03))

# Identify outliers based on Z-score threshold (e.g., 3)
outliers <- which(z_scores > 3)

# Print outliers
print(outliers)
## [1] 1573
s02$Var03[1573] <- NA

# Option 1 using ARIMA
library(forecast)
ts_var03 <- ts(na.omit(s02$Var03), frequency = 12)

model <- auto.arima(ts_var03)

forecasted <- forecast(model, h = 140)

s02_part2 <- s02
s02_part2$Var03 <- c(s02_part2$Var03[1:1622], forecasted$mean)

print(forecasted)
##         Point Forecast     Lo 80    Hi 80     Lo 95    Hi 95
## Oct 135       12.95365 12.610765 13.29654 12.429252 13.47805
## Nov 135       12.95310 12.466351 13.43985 12.208682 13.69752
## Dec 135       12.96513 12.364415 13.56584 12.046418 13.88383
## Jan 136       12.95695 12.260630 13.65327 11.892021 14.02187
## Feb 136       12.94834 12.170849 13.72582 11.759272 14.13740
## Mar 136       12.93501 12.085112 13.78491 11.635204 14.23481
## Apr 136       12.94116 12.022671 13.85965 11.536450 14.34587
## May 136       12.94582 11.961915 13.92973 11.441065 14.45058
## Jun 136       12.95241 11.908197 13.99663 11.355422 14.54941
## Jul 136       12.93812 11.838635 14.03760 11.256604 14.61963
## Aug 136       12.93672 11.784401 14.08904 11.174398 14.69905
## Sep 136       12.94660 11.742141 14.15107 11.104536 14.78867
## Oct 136       12.95566 11.698006 14.21332 11.032242 14.87908
## Nov 136       12.95562 11.648229 14.26301 10.956138 14.95510
## Dec 136       12.94849 11.594029 14.30296 10.877020 15.01997
## Jan 137       12.92394 11.523145 14.32473 10.781611 15.06626
## Feb 137       12.93407 11.487347 14.38079 10.721499 15.14664
## Mar 137       12.93028 11.439387 14.42117 10.650157 15.21040
## Apr 137       12.93279 11.400120 14.46547 10.588773 15.27681
## May 137       12.93456 11.361333 14.50779 10.528516 15.34061
## Jun 137       12.94560 11.331857 14.55934 10.477595 15.41360
## Jul 137       12.94203 11.288280 14.59577 10.412839 15.47121
## Aug 137       12.93802 11.245920 14.63011 10.350178 15.52586
## Sep 137       12.92721 11.198330 14.65609 10.283117 15.57130
## Oct 137       12.92396 11.154903 14.69302 10.218422 15.62950
## Nov 137       12.92847 11.119249 14.73769 10.161504 15.69544
## Dec 137       12.93349 11.084936 14.78205 10.106368 15.76062
## Jan 138       12.92975 11.043469 14.81602 10.044935 15.81456
## Feb 138       12.92426 11.001348 14.84716  9.983422 15.86509
## Mar 138       12.92367 10.964212 14.88312  9.926939 15.92039
## Apr 138       12.92960 10.933710 14.92550  9.877148 15.98206
## May 138       12.93234 10.901008 14.96368  9.825684 16.03900
## Jun 138       12.92853 10.863016 14.99405  9.769597 16.08747
## Jul 138       12.92398 10.824776 15.02319  9.713524 16.13444
## Aug 138       12.92636 10.793365 15.05935  9.664226 16.18849
## Sep 138       12.93087 10.764427 15.09732  9.617580 16.24417
## Oct 138       12.93019 10.731123 15.12927  9.567005 16.29338
## Nov 138       12.92528 10.694439 15.15612  9.513502 16.33706
## Dec 138       12.92399 10.661485 15.18650  9.463785 16.38420
## Jan 139       12.92812 10.633897 15.22235  9.419407 16.43684
## Feb 139       12.93106 10.605676 15.25644  9.374694 16.48742
## Mar 139       12.92811 10.572499 15.28373  9.325512 16.53072
## Apr 139       12.92418 10.538789 15.30957  9.276040 16.57232
## May 139       12.92544 10.510194 15.34068  9.231642 16.61923
## Jun 139       12.92978 10.484798 15.37476  9.190504 16.66905
## Jul 139       12.93027 10.456228 15.40430  9.146551 16.71398
## Aug 139       12.92631 10.423912 15.42871  9.099223 16.75340
## Sep 139       12.92422 10.393617 15.45482  9.053997 16.79444
## Oct 139       12.92719 10.368277 15.48610  9.013672 16.84070
## Nov 139       12.93036 10.343446 15.51727  8.974017 16.88670
## Dec 139       12.92876 10.314522 15.54300  8.930628 16.92689
## Jan 140       12.92507 10.283937 15.56619  8.885808 16.96432
## Feb 140       12.92514 10.257099 15.59318  8.844722 17.00556
## Mar 140       12.92872 10.233771 15.62367  8.807151 17.05029
## Apr 140       12.93007 10.208659 15.65149  8.768030 17.09212
## May 140       12.92716 10.179855 15.67447  8.725518 17.12881
## Jun 140       12.92471 10.151706 15.69771  8.683768 17.16565
## Jul 140       12.92653 10.127760 15.72531  8.646179 17.20689
## Aug 140       12.92963 10.105229 15.75402  8.610083 17.24917
## Sep 140       12.92909 10.079566 15.77861  8.571120 17.28706
## Oct 140       12.92590 10.051652 15.80015  8.530117 17.32168
## Nov 140       12.92515 10.026209 15.82409  8.491604 17.35869
## Dec 140       12.92792 10.004255 15.85159  8.456560 17.39928
## Jan 141       12.92975  9.981633 15.87786  8.420995 17.43850
## Feb 141       12.92780  9.955700 15.89990  8.382366 17.47323
## Mar 141       12.92527  9.929410 15.92113  8.343497 17.50704
## Apr 141       12.92614  9.906473 15.94581  8.307958 17.54432
## May 141       12.92892  9.885511 15.97233  8.274426 17.58342
## Jun 141       12.92919  9.862389 15.99599  8.238924 17.61945
## Jul 141       12.92662  9.836790 16.01644  8.201136 17.65209
## Aug 141       12.92534  9.812567 16.03811  8.164766 17.68591
## Sep 141       12.92731  9.791547 16.06307  8.131576 17.72304
## Oct 141       12.92932  9.770741 16.08790  8.098689 17.75995
## Nov 141       12.92822  9.747188 16.10926  8.063250 17.79319
## Dec 141       12.92585  9.722594 16.12910  8.026894 17.82480
## Jan 142       12.92596  9.700482 16.15144  7.993015 17.85891
## Feb 142       12.92830  9.680601 16.17599  7.961375 17.89522
## Mar 142       12.92911  9.659453 16.19877  7.928602 17.92962
## Apr 142       12.92719  9.635884 16.21849  7.893574 17.96080
## May 142       12.92564  9.612804 16.23848  7.859094 17.99218
## Jun 142       12.92688  9.592475 16.26128  7.827350 18.02640
## Jul 142       12.92886  9.572988 16.28473  7.796498 18.06122
## Aug 142       12.92845  9.551399 16.30551  7.763694 18.09321
## Sep 142       12.92638  9.528374 16.32439  7.729577 18.12319
## Oct 142       12.92595  9.507015 16.34489  7.697138 18.15477
## Nov 142       12.92777  9.487902 16.36765  7.666943 18.18861
## Dec 142       12.92891  9.468277 16.38955  7.636327 18.22150
## Jan 143       12.92761  9.446480 16.40874  7.603681 18.25154
## Feb 143       12.92600  9.424506 16.42749  7.570928 18.28107
## Mar 143       12.92661  9.404745 16.44848  7.540382 18.31284
## Apr 143       12.92841  9.386219 16.47060  7.511098 18.34572
## May 143       12.92853  9.366234 16.49083  7.480468 18.37659
## Jun 143       12.92685  9.344659 16.50904  7.448363 18.40534
## Jul 143       12.92606  9.324036 16.52809  7.417238 18.43489
## Aug 143       12.92737  9.305496 16.54925  7.388192 18.46655
## Sep 143       12.92865  9.287038 16.57025  7.359287 18.49801
## Oct 143       12.92789  9.266772 16.58902  7.328691 18.52710
## Nov 143       12.92637  9.245876 16.60686  7.297542 18.55519
## Dec 143       12.92649  9.226634 16.62634  7.268051 18.58492
## Jan 144       12.92800  9.208816 16.64719  7.239997 18.61601
## Feb 144       12.92849  9.190128 16.66685  7.211157 18.64582
## Mar 144       12.92722  9.169876 16.68457  7.180857 18.67359
## Apr 144       12.92625  9.149996 16.70250  7.150969 18.70153
## May 144       12.92708  9.131920 16.72225  7.122882 18.73129
## Jun 144       12.92835  9.114351 16.74235  7.095340 18.76137
## Jul 144       12.92805  9.095391 16.76071  7.066502 18.78960
## Aug 144       12.92671  9.075535 16.77789  7.036844 18.81658
## Sep 144       12.92647  9.056804 16.79613  7.008327 18.84461
## Oct 144       12.92766  9.039524 16.81580  6.981267 18.87406
## Nov 144       12.92837  9.021873 16.83487  6.953898 18.90285
## Dec 144       12.92750  9.002813 16.85219  6.925208 18.92980
## Jan 145       12.92648  8.983685 16.86927  6.896498 18.95645
## Feb 145       12.92690  8.966020 16.88779  6.869254 18.98456
## Mar 145       12.92806  8.949131 16.90700  6.842811 19.01332
## Apr 145       12.92811  8.931270 16.92495  6.815470 19.04075
## May 145       12.92701  8.912401 16.94163  6.787194 19.06683
## Jun 145       12.92653  8.894195 16.95887  6.759604 19.09346
## Jul 145       12.92740  8.877345 16.97745  6.733376 19.12142
## Aug 145       12.92821  8.860518 16.99589  6.707214 19.14920
## Sep 145       12.92769  8.842513 17.01287  6.679950 19.17543
## Oct 145       12.92671  8.824137 17.02928  6.652366 19.20106
## Nov 145       12.92682  8.806866 17.04677  6.625896 19.22774
## Dec 145       12.92780  8.790507 17.06510  6.600354 19.25525
## Jan 146       12.92809  8.773559 17.08263  6.574282 19.28190
## Feb 146       12.92726  8.755613 17.09890  6.547277 19.30724
## Mar 146       12.92665  8.737947 17.11535  6.520583 19.33271
## Apr 146       12.92721  8.721466 17.13295  6.495080 19.35934
## May 146       12.92802  8.705293 17.15075  6.469916 19.38612
## Jun 146       12.92780  8.688208 17.16739  6.443903 19.41170
## Jul 146       12.92693  8.670571 17.18330  6.417390 19.43648
## Aug 146       12.92680  8.653698 17.19990  6.391655 19.46194
## Sep 146       12.92758  8.637763 17.21740  6.366871 19.48829
## Oct 146       12.92802  8.621568 17.23447  6.341870 19.51417
## Nov 146       12.92744  8.604467 17.25042  6.316022 19.53886
## Dec 146       12.92679  8.587356 17.26622  6.290199 19.56338
## Jan 147       12.92709  8.571214 17.28296  6.265354 19.58882
## Feb 147       12.92783  8.555563 17.30011  6.241022 19.61465
## Mar 147       12.92784  8.539270 17.31642  6.216099 19.63959
## Apr 147       12.92713  8.522346 17.33191  6.190595 19.66366
## May 147       12.92683  8.505882 17.34779  6.165572 19.68810
plot(forecasted)

# Option 2 using prophet
library(prophet)

df <- data.frame(
  ds = seq.Date(from = as.Date("2000-01-01"), by = "month", length.out = length(na.omit(s02$Var03))),  # Replace the start date as needed
  y = na.omit(s02$Var03)
)

# Fit the Prophet model
model <- prophet(df)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
# Create future dataframe for 140 months ahead
future <- make_future_dataframe(model, periods = 140, freq = "month")

# Forecast
forecast <- predict(model, future)

# Print the forecasted values
print(tail(forecast[c('ds', 'yhat')], 140))
##              ds     yhat
## 1618 2134-10-01 12.67883
## 1619 2134-11-01 12.62738
## 1620 2134-12-01 12.59534
## 1621 2135-01-01 12.49938
## 1622 2135-02-01 12.59613
## 1623 2135-03-01 12.62154
## 1624 2135-04-01 12.50593
## 1625 2135-05-01 12.59142
## 1626 2135-06-01 12.60088
## 1627 2135-07-01 12.50719
## 1628 2135-08-01 12.51836
## 1629 2135-09-01 12.57739
## 1630 2135-10-01 12.58394
## 1631 2135-11-01 12.52361
## 1632 2135-12-01 12.48791
## 1633 2136-01-01 12.37695
## 1634 2136-02-01 12.50539
## 1635 2136-03-01 12.47842
## 1636 2136-04-01 12.43389
## 1637 2136-05-01 12.48381
## 1638 2136-06-01 12.49719
## 1639 2136-07-01 12.43633
## 1640 2136-08-01 12.41689
## 1641 2136-09-01 12.44781
## 1642 2136-10-01 12.45762
## 1643 2136-11-01 12.42363
## 1644 2136-12-01 12.39910
## 1645 2137-01-01 12.33487
## 1646 2137-02-01 12.36869
## 1647 2137-03-01 12.38961
## 1648 2137-04-01 12.32133
## 1649 2137-05-01 12.38289
## 1650 2137-06-01 12.39512
## 1651 2137-07-01 12.32307
## 1652 2137-08-01 12.31357
## 1653 2137-09-01 12.35405
## 1654 2137-10-01 12.36314
## 1655 2137-11-01 12.32047
## 1656 2137-12-01 12.29215
## 1657 2138-01-01 12.21178
## 1658 2138-02-01 12.27698
## 1659 2138-03-01 12.30053
## 1660 2138-04-01 12.20855
## 1661 2138-05-01 12.28197
## 1662 2138-06-01 12.29289
## 1663 2138-07-01 12.20989
## 1664 2138-08-01 12.21060
## 1665 2138-09-01 12.26045
## 1666 2138-10-01 12.26846
## 1667 2138-11-01 12.21701
## 1668 2138-12-01 12.18496
## 1669 2139-01-01 12.08900
## 1670 2139-02-01 12.18576
## 1671 2139-03-01 12.21116
## 1672 2139-04-01 12.09556
## 1673 2139-05-01 12.18104
## 1674 2139-06-01 12.19050
## 1675 2139-07-01 12.09682
## 1676 2139-08-01 12.10799
## 1677 2139-09-01 12.16702
## 1678 2139-10-01 12.17356
## 1679 2139-11-01 12.11324
## 1680 2139-12-01 12.07754
## 1681 2140-01-01 11.96657
## 1682 2140-02-01 12.09502
## 1683 2140-03-01 12.06805
## 1684 2140-04-01 12.02352
## 1685 2140-05-01 12.07344
## 1686 2140-06-01 12.08682
## 1687 2140-07-01 12.02596
## 1688 2140-08-01 12.00652
## 1689 2140-09-01 12.03743
## 1690 2140-10-01 12.04725
## 1691 2140-11-01 12.01326
## 1692 2140-12-01 11.98872
## 1693 2141-01-01 11.92450
## 1694 2141-02-01 11.95832
## 1695 2141-03-01 11.97924
## 1696 2141-04-01 11.91096
## 1697 2141-05-01 11.97252
## 1698 2141-06-01 11.98475
## 1699 2141-07-01 11.91270
## 1700 2141-08-01 11.90320
## 1701 2141-09-01 11.94367
## 1702 2141-10-01 11.95277
## 1703 2141-11-01 11.91010
## 1704 2141-12-01 11.88178
## 1705 2142-01-01 11.80140
## 1706 2142-02-01 11.86660
## 1707 2142-03-01 11.89016
## 1708 2142-04-01 11.79818
## 1709 2142-05-01 11.87160
## 1710 2142-06-01 11.88252
## 1711 2142-07-01 11.79952
## 1712 2142-08-01 11.80023
## 1713 2142-09-01 11.85008
## 1714 2142-10-01 11.85809
## 1715 2142-11-01 11.80663
## 1716 2142-12-01 11.77459
## 1717 2143-01-01 11.67863
## 1718 2143-02-01 11.77538
## 1719 2143-03-01 11.80079
## 1720 2143-04-01 11.68518
## 1721 2143-05-01 11.77067
## 1722 2143-06-01 11.78013
## 1723 2143-07-01 11.68645
## 1724 2143-08-01 11.69761
## 1725 2143-09-01 11.75665
## 1726 2143-10-01 11.76319
## 1727 2143-11-01 11.70286
## 1728 2143-12-01 11.66716
## 1729 2144-01-01 11.55620
## 1730 2144-02-01 11.68465
## 1731 2144-03-01 11.65767
## 1732 2144-04-01 11.61315
## 1733 2144-05-01 11.66306
## 1734 2144-06-01 11.67644
## 1735 2144-07-01 11.61559
## 1736 2144-08-01 11.59615
## 1737 2144-09-01 11.62706
## 1738 2144-10-01 11.63687
## 1739 2144-11-01 11.60288
## 1740 2144-12-01 11.57835
## 1741 2145-01-01 11.51412
## 1742 2145-02-01 11.54795
## 1743 2145-03-01 11.56887
## 1744 2145-04-01 11.50059
## 1745 2145-05-01 11.56215
## 1746 2145-06-01 11.57437
## 1747 2145-07-01 11.50232
## 1748 2145-08-01 11.49282
## 1749 2145-09-01 11.53330
## 1750 2145-10-01 11.54240
## 1751 2145-11-01 11.49972
## 1752 2145-12-01 11.47141
## 1753 2146-01-01 11.39103
## 1754 2146-02-01 11.45623
## 1755 2146-03-01 11.47979
## 1756 2146-04-01 11.38780
## 1757 2146-05-01 11.46122
# Plot the forecast
plot(model, forecast)

# Option 3 using BSTS
library(bsts)

ts_var03 <- ts(na.omit(s02$Var03), frequency = 12)

stateSpace <- AddLocalLevel(list(), ts_var03)

model <- bsts(ts_var03, state.specification = stateSpace, niter = 1000)
## =-=-=-=-= Iteration 0 Thu Jun 20 19:12:52 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 100 Thu Jun 20 19:12:54 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 200 Thu Jun 20 19:12:56 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 300 Thu Jun 20 19:12:58 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 400 Thu Jun 20 19:13:00 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 500 Thu Jun 20 19:13:02 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 600 Thu Jun 20 19:13:04 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 700 Thu Jun 20 19:13:05 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 800 Thu Jun 20 19:13:08 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 900 Thu Jun 20 19:13:09 2024
##  =-=-=-=-=
newForecast <- predict(model, horizon = 140)

forecasted_values <- as.numeric(newForecast$mean)

print(forecasted_values)
##   [1] 12.94748 12.95436 12.96082 12.95911 12.94756 12.95689 12.95557 12.95235
##   [9] 12.97278 12.99023 12.99326 12.99552 12.97628 12.96957 12.97184 12.96573
##  [17] 13.00286 12.98439 12.98271 12.99952 13.01166 13.00085 12.98231 12.97773
##  [25] 12.99462 12.99637 13.00560 12.99764 13.01127 12.99509 13.00313 13.01381
##  [33] 13.00187 12.99498 13.01212 13.00612 13.02699 13.00970 13.00426 12.99814
##  [41] 12.99466 12.99755 13.00038 12.99879 12.98737 12.99334 12.98706 12.99407
##  [49] 13.00267 13.00437 13.00380 13.01747 13.01820 13.01976 13.00797 13.00162
##  [57] 12.98576 12.98071 12.98888 12.99774 13.01416 13.00861 13.00382 13.00156
##  [65] 12.99001 12.98783 12.97539 12.96877 12.99007 13.00030 13.00261 13.02159
##  [73] 13.01785 13.01677 13.03785 13.03962 13.04189 13.04568 13.05106 13.04591
##  [81] 13.04183 13.03884 13.04605 13.05077 13.04439 13.02056 13.02066 13.02709
##  [89] 13.01783 12.99735 13.00432 13.00918 13.00813 13.01132 13.01961 13.00962
##  [97] 13.02185 13.02460 13.03762 13.04729 13.05228 13.05850 13.05721 13.05935
## [105] 13.04882 13.05752 13.04482 13.03101 13.03449 13.03518 13.03574 13.01659
## [113] 13.01300 13.00655 12.99392 13.00093 13.01516 13.01667 13.04328 13.03774
## [121] 13.03728 13.03062 13.02179 13.01616 13.03376 13.02533 13.01544 13.00562
## [129] 13.01589 13.01288 13.00159 13.01946 13.01544 12.99597 12.99411 12.99681
## [137] 12.98851 12.99346 12.99459 12.98215
library(ggplot2)

plot_data <- data.frame(
  Time = 1:(length(ts_var03) + 140),
  Value = c(ts_var03, forecasted_values),
  Type = c(rep("Original", length(ts_var03)), rep("Forecast", length(forecasted_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "blue"))  # Adjust colors if needed

# Option 4 using TBATS

model2 <- tbats(ts_var03)

forecastTBATS <- forecast(model2, h = 140)

new_values <- as.numeric(forecastTBATS$mean)

print(new_values)
##   [1] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##   [9] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [17] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [25] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [33] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [41] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [49] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [57] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [65] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [73] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [81] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [89] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
##  [97] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
## [105] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
## [113] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
## [121] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
## [129] 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698 12.95698
## [137] 12.95698 12.95698 12.95698 12.95698
plot_data <- data.frame(
  Time = 1:(length(ts_var03) + 140),
  Value = c(ts_var03, new_values),
  Type = c(rep("Original", length(ts_var03)), rep("Forecast", length(new_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "red"))  # Adjust colors if needed

S06 Var 05 appears to have a variable. That will be removed.

# Calculate Z-scores
z_scores <- abs(scale(s06$Var05))

# Identify outliers based on Z-score threshold (e.g., 3)
outliers <- which(z_scores > 3)

# Print outliers
print(outliers)
## [1] 320
s06$Var05[320] <- NA

Then predict S06 var 05. Prophet seems to be the most accurate from a big perspective.

# Option 1 using ARIMA
library(forecast)
ts_var05 <- ts(na.omit(s06$Var05), frequency = 12)

model <- auto.arima(ts_var05)

forecasted <- forecast(model, h = 140)

s06_part2 <- s06
s06_part2$Var05 <- c(s06_part2$Var05[1:1622], forecasted$mean)

print(forecasted)
##         Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## Sep 135       48.29364 47.57022 49.01706 47.18726 49.40002
## Oct 135       48.37144 47.41075 49.33213 46.90219 49.84069
## Nov 135       48.44030 47.29195 49.58866 46.68404 50.19656
## Dec 135       48.44055 47.15416 49.72694 46.47318 50.40792
## Jan 136       48.48930 47.07435 49.90426 46.32532 50.65329
## Feb 136       48.47098 46.94923 49.99273 46.14366 50.79830
## Mar 136       48.48484 46.85915 50.11052 45.99856 50.97111
## Apr 136       48.50686 46.78964 50.22408 45.88059 51.13312
## May 136       48.49411 46.68664 50.30157 45.72983 51.25839
## Jun 136       48.47157 46.58192 50.36122 45.58160 51.36154
## Jul 136       48.42956 46.45866 50.40046 45.41533 51.44379
## Aug 136       48.41537 46.36889 50.46185 45.28555 51.54519
## Sep 136       48.42141 46.29295 50.54986 45.16622 51.67660
## Oct 136       48.42395 46.21918 50.62873 45.05204 51.79587
## Nov 136       48.42669 46.14684 50.70654 44.93996 51.91342
## Dec 136       48.42746 46.07656 50.77837 44.83207 52.02286
## Jan 137       48.42879 46.00788 50.84969 44.72633 52.13124
## Feb 137       48.42891 45.94104 50.91678 44.62405 52.23377
## Mar 137       48.42961 45.87576 50.98345 44.52384 52.33538
## Apr 137       48.42952 45.81208 51.04695 44.42650 52.43254
## May 137       48.42992 45.74984 51.11000 44.33109 52.52875
## Jun 137       48.42978 45.68899 51.17057 44.23811 52.62145
## Jul 137       48.43003 45.62944 51.23062 44.14690 52.71316
## Aug 137       48.42990 45.57112 51.28868 44.05777 52.80203
## Sep 137       48.43007 45.51396 51.34618 43.97026 52.88987
## Oct 137       48.42995 45.45788 51.40203 43.88456 52.97535
## Nov 137       48.43007 45.40285 51.45730 43.80034 53.05981
## Dec 137       48.42998 45.34879 51.51118 43.71770 53.14227
## Jan 138       48.43007 45.29567 51.56447 43.63642 53.22372
## Feb 138       48.43000 45.24342 51.61658 43.55655 53.30346
## Mar 138       48.43006 45.19203 51.66810 43.47792 53.38221
## Apr 138       48.43001 45.14143 51.71860 43.40056 53.45947
## May 138       48.43006 45.09161 51.76851 43.32434 53.53578
## Jun 138       48.43002 45.04251 51.81753 43.24926 53.61078
## Jul 138       48.43005 44.99412 51.86599 43.17524 53.68486
## Aug 138       48.43002 44.94639 51.91366 43.10226 53.75779
## Sep 138       48.43005 44.89932 51.96078 43.03026 53.82984
## Oct 138       48.43003 44.85285 52.00720 42.95921 53.90084
## Nov 138       48.43005 44.80700 52.05310 42.88907 53.97103
## Dec 138       48.43003 44.76170 52.09836 42.81981 54.04025
## Jan 139       48.43004 44.71697 52.14312 42.75139 54.10870
## Feb 139       48.43003 44.67276 52.18730 42.68378 54.17628
## Mar 139       48.43004 44.62908 52.23101 42.61696 54.24312
## Apr 139       48.43003 44.58588 52.27419 42.55091 54.30916
## May 139       48.43004 44.54317 52.31692 42.48558 54.37450
## Jun 139       48.43004 44.50092 52.35915 42.42097 54.43910
## Jul 139       48.43004 44.45912 52.40096 42.35704 54.50304
## Aug 139       48.43004 44.41775 52.44232 42.29378 54.56629
## Sep 139       48.43004 44.37681 52.48327 42.23116 54.62892
## Oct 139       48.43004 44.33628 52.52380 42.16917 54.69090
## Nov 139       48.43004 44.29614 52.56394 42.10779 54.75229
## Dec 139       48.43004 44.25639 52.60368 42.04700 54.81307
## Jan 140       48.43004 44.21702 52.64306 41.98678 54.87330
## Feb 140       48.43004 44.17801 52.68207 41.92712 54.93295
## Mar 140       48.43004 44.13935 52.72072 41.86800 54.99207
## Apr 140       48.43004 44.10104 52.75903 41.80941 55.05066
## May 140       48.43004 44.06307 52.79701 41.75134 55.10874
## Jun 140       48.43004 44.02542 52.83465 41.69376 55.16631
## Jul 140       48.43004 43.98810 52.87198 41.63667 55.22340
## Aug 140       48.43004 43.95108 52.90900 41.58006 55.28001
## Sep 140       48.43004 43.91437 52.94571 41.52392 55.33616
## Oct 140       48.43004 43.87795 52.98213 41.46822 55.39186
## Nov 140       48.43004 43.84182 53.01825 41.41297 55.44711
## Dec 140       48.43004 43.80598 53.05410 41.35815 55.50193
## Jan 141       48.43004 43.77041 53.08967 41.30375 55.55633
## Feb 141       48.43004 43.73511 53.12497 41.24976 55.61032
## Mar 141       48.43004 43.70007 53.16001 41.19618 55.66390
## Apr 141       48.43004 43.66529 53.19479 41.14299 55.71709
## May 141       48.43004 43.63076 53.22931 41.09018 55.76990
## Jun 141       48.43004 43.59648 53.26359 41.03775 55.82232
## Jul 141       48.43004 43.56244 53.29763 40.98569 55.87438
## Aug 141       48.43004 43.52864 53.33144 40.93400 55.92608
## Sep 141       48.43004 43.49507 53.36501 40.88265 55.97742
## Oct 141       48.43004 43.46172 53.39835 40.83166 56.02842
## Nov 141       48.43004 43.42860 53.43147 40.78100 56.07907
## Dec 141       48.43004 43.39570 53.46438 40.73068 56.12940
## Jan 142       48.43004 43.36301 53.49707 40.68068 56.17939
## Feb 142       48.43004 43.33053 53.52955 40.63101 56.22907
## Mar 142       48.43004 43.29825 53.56183 40.58165 56.27843
## Apr 142       48.43004 43.26618 53.59390 40.53259 56.32748
## May 142       48.43004 43.23430 53.62578 40.48384 56.37623
## Jun 142       48.43004 43.20262 53.65746 40.43539 56.42468
## Jul 142       48.43004 43.17113 53.68895 40.38723 56.47285
## Aug 142       48.43004 43.13983 53.72025 40.33936 56.52072
## Sep 142       48.43004 43.10871 53.75137 40.29177 56.56831
## Oct 142       48.43004 43.07777 53.78231 40.24445 56.61563
## Nov 142       48.43004 43.04701 53.81307 40.19741 56.66267
## Dec 142       48.43004 43.01642 53.84365 40.15063 56.70945
## Jan 143       48.43004 42.98601 53.87407 40.10412 56.75596
## Feb 143       48.43004 42.95577 53.90431 40.05786 56.80222
## Mar 143       48.43004 42.92569 53.93439 40.01186 56.84822
## Apr 143       48.43004 42.89577 53.96430 39.96611 56.89397
## May 143       48.43004 42.86602 53.99406 39.92060 56.93947
## Jun 143       48.43004 42.83642 54.02365 39.87534 56.98474
## Jul 143       48.43004 42.80698 54.05309 39.83032 57.02976
## Aug 143       48.43004 42.77770 54.08238 39.78553 57.07455
## Sep 143       48.43004 42.74856 54.11152 39.74097 57.11911
## Oct 143       48.43004 42.71957 54.14050 39.69664 57.16344
## Nov 143       48.43004 42.69073 54.16934 39.65253 57.20755
## Dec 143       48.43004 42.66204 54.19804 39.60864 57.25144
## Jan 144       48.43004 42.63348 54.22660 39.56497 57.29511
## Feb 144       48.43004 42.60507 54.25501 39.52151 57.33856
## Mar 144       48.43004 42.57679 54.28329 39.47827 57.38181
## Apr 144       48.43004 42.54865 54.31143 39.43523 57.42485
## May 144       48.43004 42.52064 54.33943 39.39240 57.46768
## Jun 144       48.43004 42.49277 54.36731 39.34977 57.51031
## Jul 144       48.43004 42.46502 54.39505 39.30734 57.55274
## Aug 144       48.43004 42.43741 54.42267 39.26510 57.59498
## Sep 144       48.43004 42.40992 54.45016 39.22306 57.63702
## Oct 144       48.43004 42.38256 54.47752 39.18121 57.67887
## Nov 144       48.43004 42.35531 54.50476 39.13955 57.72053
## Dec 144       48.43004 42.32820 54.53188 39.09807 57.76200
## Jan 145       48.43004 42.30120 54.55888 39.05678 57.80329
## Feb 145       48.43004 42.27432 54.58576 39.01567 57.84441
## Mar 145       48.43004 42.24755 54.61253 38.97474 57.88534
## Apr 145       48.43004 42.22090 54.63917 38.93398 57.92609
## May 145       48.43004 42.19437 54.66571 38.89340 57.96667
## Jun 145       48.43004 42.16795 54.69213 38.85299 58.00708
## Jul 145       48.43004 42.14163 54.71844 38.81275 58.04732
## Aug 145       48.43004 42.11543 54.74464 38.77268 58.08740
## Sep 145       48.43004 42.08934 54.77074 38.73278 58.12730
## Oct 145       48.43004 42.06335 54.79672 38.69303 58.16704
## Nov 145       48.43004 42.03747 54.82261 38.65345 58.20663
## Dec 145       48.43004 42.01170 54.84838 38.61403 58.24605
## Jan 146       48.43004 41.98602 54.87406 38.57477 58.28531
## Feb 146       48.43004 41.96045 54.89963 38.53566 58.32442
## Mar 146       48.43004 41.93498 54.92510 38.49670 58.36337
## Apr 146       48.43004 41.90961 54.95047 38.45790 58.40218
## May 146       48.43004 41.88434 54.97574 38.41925 58.44083
## Jun 146       48.43004 41.85916 55.00092 38.38075 58.47933
## Jul 146       48.43004 41.83408 55.02600 38.34239 58.51769
## Aug 146       48.43004 41.80910 55.05098 38.30418 58.55590
## Sep 146       48.43004 41.78420 55.07587 38.26611 58.59396
## Oct 146       48.43004 41.75941 55.10067 38.22819 58.63189
## Nov 146       48.43004 41.73470 55.12538 38.19040 58.66967
## Dec 146       48.43004 41.71009 55.14999 38.15276 58.70732
## Jan 147       48.43004 41.68556 55.17452 38.11525 58.74483
## Feb 147       48.43004 41.66112 55.19895 38.07788 58.78220
## Mar 147       48.43004 41.63678 55.22330 38.04064 58.81944
## Apr 147       48.43004 41.61251 55.24756 38.00353 58.85654
plot(forecasted)

# Option 2 using prophet
library(prophet)

df <- data.frame(
  ds = seq.Date(from = as.Date("2000-01-01"), by = "month", length.out = length(na.omit(s06$Var05))),  # Replace the start date as needed
  y = na.omit(s06$Var05)
)

# Fit the Prophet model
model <- prophet(df)
## Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
# Create future dataframe for 140 months ahead
future <- make_future_dataframe(model, periods = 140, freq = "month")

# Forecast
forecast <- predict(model, future)

# Print the forecasted values
print(tail(forecast[c('ds', 'yhat')], 140))
##              ds     yhat
## 1617 2134-09-01 49.57315
## 1618 2134-10-01 49.44798
## 1619 2134-11-01 49.47277
## 1620 2134-12-01 49.33974
## 1621 2135-01-01 49.52868
## 1622 2135-02-01 49.22245
## 1623 2135-03-01 49.31411
## 1624 2135-04-01 49.42915
## 1625 2135-05-01 49.28170
## 1626 2135-06-01 49.20825
## 1627 2135-07-01 49.29528
## 1628 2135-08-01 49.17933
## 1629 2135-09-01 49.33839
## 1630 2135-10-01 49.18653
## 1631 2135-11-01 49.23506
## 1632 2135-12-01 49.08675
## 1633 2136-01-01 49.31360
## 1634 2136-02-01 48.95900
## 1635 2136-03-01 49.07526
## 1636 2136-04-01 49.15422
## 1637 2136-05-01 49.05929
## 1638 2136-06-01 48.98681
## 1639 2136-07-01 48.98645
## 1640 2136-08-01 48.97093
## 1641 2136-09-01 49.06380
## 1642 2136-10-01 48.99256
## 1643 2136-11-01 48.97038
## 1644 2136-12-01 48.86845
## 1645 2137-01-01 48.97978
## 1646 2137-02-01 48.76997
## 1647 2137-03-01 48.82849
## 1648 2137-04-01 48.91982
## 1649 2137-05-01 48.80743
## 1650 2137-06-01 48.73416
## 1651 2137-07-01 48.76333
## 1652 2137-08-01 48.71446
## 1653 2137-09-01 48.82940
## 1654 2137-10-01 48.73110
## 1655 2137-11-01 48.73232
## 1656 2137-12-01 48.61475
## 1657 2138-01-01 48.76517
## 1658 2138-02-01 48.50722
## 1659 2138-03-01 48.58200
## 1660 2138-04-01 48.68535
## 1661 2138-05-01 48.55545
## 1662 2138-06-01 48.48186
## 1663 2138-07-01 48.54017
## 1664 2138-08-01 48.45782
## 1665 2138-09-01 48.59482
## 1666 2138-10-01 48.46964
## 1667 2138-11-01 48.49443
## 1668 2138-12-01 48.36140
## 1669 2139-01-01 48.55034
## 1670 2139-02-01 48.24412
## 1671 2139-03-01 48.33577
## 1672 2139-04-01 48.45082
## 1673 2139-05-01 48.30336
## 1674 2139-06-01 48.22991
## 1675 2139-07-01 48.31694
## 1676 2139-08-01 48.20100
## 1677 2139-09-01 48.36005
## 1678 2139-10-01 48.20819
## 1679 2139-11-01 48.25673
## 1680 2139-12-01 48.10841
## 1681 2140-01-01 48.33526
## 1682 2140-02-01 47.98066
## 1683 2140-03-01 48.09692
## 1684 2140-04-01 48.17589
## 1685 2140-05-01 48.08095
## 1686 2140-06-01 48.00848
## 1687 2140-07-01 48.00812
## 1688 2140-08-01 47.99259
## 1689 2140-09-01 48.08546
## 1690 2140-10-01 48.01422
## 1691 2140-11-01 47.99204
## 1692 2140-12-01 47.89012
## 1693 2141-01-01 48.00144
## 1694 2141-02-01 47.79164
## 1695 2141-03-01 47.85016
## 1696 2141-04-01 47.94148
## 1697 2141-05-01 47.82909
## 1698 2141-06-01 47.75582
## 1699 2141-07-01 47.78500
## 1700 2141-08-01 47.73612
## 1701 2141-09-01 47.85106
## 1702 2141-10-01 47.75276
## 1703 2141-11-01 47.75398
## 1704 2141-12-01 47.63641
## 1705 2142-01-01 47.78684
## 1706 2142-02-01 47.52889
## 1707 2142-03-01 47.60366
## 1708 2142-04-01 47.70702
## 1709 2142-05-01 47.57711
## 1710 2142-06-01 47.50352
## 1711 2142-07-01 47.56183
## 1712 2142-08-01 47.47948
## 1713 2142-09-01 47.61648
## 1714 2142-10-01 47.49130
## 1715 2142-11-01 47.51610
## 1716 2142-12-01 47.38307
## 1717 2143-01-01 47.57201
## 1718 2143-02-01 47.26578
## 1719 2143-03-01 47.35744
## 1720 2143-04-01 47.47248
## 1721 2143-05-01 47.32502
## 1722 2143-06-01 47.25158
## 1723 2143-07-01 47.33860
## 1724 2143-08-01 47.22266
## 1725 2143-09-01 47.38171
## 1726 2143-10-01 47.22986
## 1727 2143-11-01 47.27839
## 1728 2143-12-01 47.13008
## 1729 2144-01-01 47.35693
## 1730 2144-02-01 47.00233
## 1731 2144-03-01 47.11858
## 1732 2144-04-01 47.19755
## 1733 2144-05-01 47.10262
## 1734 2144-06-01 47.03014
## 1735 2144-07-01 47.02978
## 1736 2144-08-01 47.01425
## 1737 2144-09-01 47.10713
## 1738 2144-10-01 47.03589
## 1739 2144-11-01 47.01371
## 1740 2144-12-01 46.91178
## 1741 2145-01-01 47.02311
## 1742 2145-02-01 46.81330
## 1743 2145-03-01 46.87182
## 1744 2145-04-01 46.96315
## 1745 2145-05-01 46.85075
## 1746 2145-06-01 46.77748
## 1747 2145-07-01 46.80666
## 1748 2145-08-01 46.75779
## 1749 2145-09-01 46.87273
## 1750 2145-10-01 46.77443
## 1751 2145-11-01 46.77565
## 1752 2145-12-01 46.65808
## 1753 2146-01-01 46.80850
## 1754 2146-02-01 46.55055
## 1755 2146-03-01 46.62532
## 1756 2146-04-01 46.72868
# Plot the forecast
plot(model, forecast)

# Option 3 using BSTS
library(bsts)

ts_var05 <- ts(na.omit(s06$Var05), frequency = 12)

stateSpace <- AddLocalLevel(list(), ts_var05)

model <- bsts(ts_var05, state.specification = stateSpace, niter = 1000)
## =-=-=-=-= Iteration 0 Thu Jun 20 19:13:51 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 100 Thu Jun 20 19:13:53 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 200 Thu Jun 20 19:13:55 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 300 Thu Jun 20 19:13:57 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 400 Thu Jun 20 19:13:59 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 500 Thu Jun 20 19:14:01 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 600 Thu Jun 20 19:14:03 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 700 Thu Jun 20 19:14:05 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 800 Thu Jun 20 19:14:07 2024
##  =-=-=-=-=
## =-=-=-=-= Iteration 900 Thu Jun 20 19:14:09 2024
##  =-=-=-=-=
newForecast <- predict(model, horizon = 140)

forecasted_values <- as.numeric(newForecast$mean)

print(forecasted_values)
##   [1] 48.18965 48.19882 48.20390 48.20567 48.17502 48.19329 48.18409 48.18772
##   [9] 48.22395 48.25835 48.25134 48.25337 48.22362 48.21451 48.22340 48.21012
##  [17] 48.26792 48.22634 48.21968 48.24498 48.27344 48.24807 48.20706 48.19698
##  [25] 48.22926 48.24608 48.25311 48.25841 48.26942 48.25420 48.25394 48.27813
##  [33] 48.27023 48.25694 48.28313 48.25172 48.29479 48.26220 48.24871 48.23553
##  [41] 48.22799 48.22645 48.22897 48.23768 48.20649 48.22916 48.21524 48.20601
##  [49] 48.24489 48.23504 48.22949 48.25435 48.25802 48.27938 48.25298 48.22907
##  [57] 48.22524 48.22372 48.22999 48.23899 48.27957 48.26629 48.25936 48.25748
##  [65] 48.24677 48.22336 48.21227 48.19447 48.21838 48.25141 48.24293 48.26671
##  [73] 48.27068 48.26849 48.30458 48.31225 48.31839 48.33385 48.32409 48.32444
##  [81] 48.32128 48.32040 48.35115 48.34146 48.32792 48.29023 48.30016 48.29435
##  [89] 48.28001 48.24975 48.27381 48.27935 48.29627 48.29516 48.31947 48.28757
##  [97] 48.32126 48.33003 48.34931 48.38110 48.38765 48.38701 48.39937 48.39599
## [105] 48.38871 48.39643 48.37742 48.35835 48.36421 48.36522 48.35044 48.31978
## [113] 48.31188 48.30186 48.28144 48.29947 48.30743 48.30394 48.34925 48.32849
## [121] 48.34388 48.32324 48.30055 48.27540 48.32698 48.29389 48.28900 48.27566
## [129] 48.29050 48.29081 48.27782 48.31398 48.30337 48.26620 48.25348 48.26074
## [137] 48.25245 48.26985 48.25672 48.23589
library(ggplot2)

plot_data <- data.frame(
  Time = 1:(length(ts_var05) + 140),
  Value = c(ts_var05, forecasted_values),
  Type = c(rep("Original", length(ts_var05)), rep("Forecast", length(forecasted_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "blue"))  # Adjust colors if needed

# Option 4 using TBATS

model2 <- tbats(ts_var05)

forecastTBATS <- forecast(model2, h = 140)

new_values <- as.numeric(forecastTBATS$mean)

print(new_values)
##   [1] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##   [9] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [17] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [25] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [33] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [41] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [49] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [57] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [65] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [73] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [81] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [89] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
##  [97] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
## [105] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
## [113] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
## [121] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
## [129] 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903 48.19903
## [137] 48.19903 48.19903 48.19903 48.19903
plot_data <- data.frame(
  Time = 1:(length(ts_var05) + 140),
  Value = c(ts_var05, new_values),
  Type = c(rep("Original", length(ts_var05)), rep("Forecast", length(new_values)))
)

ggplot(plot_data, aes(x = Time, y = Value, color = Type)) +
  geom_line() +
  labs(title = "Time Series Forecast",
       x = "Time",
       y = "Value") +
  scale_color_manual(values = c("black", "red"))  # Adjust colors if needed

Options:

  1. Use multiple models.
  2. Use other variables to predict. Forecast those variables and use those forecasts to predict my variables.