library(quantmod)
library(tseries)
library(timeSeries)
library(forecast)
library(xts)
Here we have used WIPRO as our stock for forecasting using ETS models with the last 10 years of stock data.
options("getSymbols.warning4.0"=FALSE)
options("getSymbols.yahoo.warning"=FALSE)
getSymbols("WIPRO.NS",src = 'yahoo', from='2011-05-01', to='2021-06-20')
## [1] "WIPRO.NS"
class(WIPRO.NS)
## [1] "xts" "zoo"
Firstly we will remove the null values from our dataset abd then convert it into a time-series. Then have a look at this time series
WIPRO.NS1 <- na.locf(WIPRO.NS,fromLast = FALSE)
DAta <- ts(WIPRO.NS1[,4], start = c(2011,1),end = c(2020,1),frequency = 252)
autoplot(DAta, main ="Closing Price chart")
class(DAta)
## [1] "ts"
Before going further, lets have a look at the decomposition of our time series:
plot(decompose(DAta))
We will start by simply using Holt linear method, with E,T,S values as A,A,N respectively:
hlin<- holt(DAta,h=30)
autoplot(hlin,main = "Holt Method with ETS(A,A,N) Linear Method",ylab = "Price",xlab = "Days",col = "brown")
Now we will use Holt Winter’s Additive Seasonality method of ETS modelling to capture the additive nature of the seasonality present in the time series:
windata<- ts(DAta, frequency = 21)
fit1 <-hw(windata,seasonal = "additive")
summary(fit1)
##
## Forecast method: Holt-Winters' additive method
##
## Model Information:
## Holt-Winters' additive method
##
## Call:
## hw(y = windata, seasonal = "additive")
##
## Smoothing parameters:
## alpha = 0.9527
## beta = 2e-04
## gamma = 0.0041
##
## Initial states:
## l = 181.9243
## b = -0.0472
## s = 0.4236 0.5253 0.2224 0.3959 -0.4545 -0.3555
## 0.0625 0.384 -0.8122 -0.4356 -0.5555 -1.1911 -0.5821 -0.1023 0.1359 0.5357 0.4247 0.0341 0.8349 0.5109 -0.0011
##
## sigma: 3.131
##
## AIC AICc BIC
## 22739.04 22739.67 22887.94
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.05940975 3.113664 2.245715 0.01840932 1.151514 0.2100068
## ACF1
## Training set 0.01094994
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 109.0476 227.8399 223.8274 231.8524 221.7033 233.9765
## 109.0952 228.0810 222.5387 233.6233 219.6048 236.5572
## 109.1429 227.3107 220.5773 234.0441 217.0129 237.6085
## 109.1905 227.6303 219.8866 235.3740 215.7873 239.4733
## 109.2381 227.8148 219.1779 236.4517 214.6057 241.0238
## 109.2857 227.3952 217.9488 236.8415 212.9483 241.8421
## 109.3333 227.2053 217.0134 237.3971 211.6181 242.7924
## 109.3810 226.5670 215.6803 237.4537 209.9172 243.2167
## 109.4286 226.0929 214.5529 237.6329 208.4440 243.7418
## 109.4762 226.5376 214.3792 238.6961 207.9429 245.1323
## 109.5238 226.5772 213.8301 239.3243 207.0822 246.0722
## 109.5714 226.4352 213.1253 239.7451 206.0795 246.7910
## 109.6190 227.0991 213.2490 240.9492 205.9173 248.2809
## 109.6667 227.1409 212.7708 241.5110 205.1638 249.1181
## 109.7143 226.7506 211.8785 241.6227 204.0057 249.4956
## 109.7619 226.6032 211.2453 241.9611 203.1154 250.0911
## 109.8095 227.2876 211.4587 243.1166 203.0794 251.4959
## 109.8571 227.1773 210.8908 243.4638 202.2693 252.0854
## 109.9048 227.2758 210.5441 244.0075 201.6869 252.8648
## 109.9524 227.4080 210.2425 244.5735 201.1556 253.6604
## 110.0000 226.7922 209.2035 244.3810 199.8926 253.6919
## 110.0476 227.2982 209.2925 245.3039 199.7609 254.8356
## 110.0952 227.5393 209.1294 245.9492 199.3838 255.6948
## 110.1429 226.7690 207.9634 245.5746 198.0084 255.5296
## 110.1905 227.0886 207.8954 246.2818 197.7352 256.4420
## 110.2381 227.2731 207.6998 246.8463 197.3384 257.2078
## 110.2857 226.8535 206.9073 246.7997 196.3484 257.3586
## 110.3333 226.6636 206.3511 246.9760 195.5984 257.7288
## 110.3810 226.0253 205.3530 246.6976 194.4097 257.6408
## 110.4286 225.5512 204.5251 246.5773 193.3946 257.7079
## 110.4762 225.9959 204.6217 247.3701 193.3069 258.6849
## 110.5238 226.0355 204.3187 247.7523 192.8225 259.2485
## 110.5714 225.8935 203.8393 247.9477 192.1645 259.6225
## 110.6190 226.5574 204.1708 248.9440 192.3200 260.7948
## 110.6667 226.5992 203.8849 249.3135 191.8607 261.3378
## 110.7143 226.2089 203.1715 249.2463 190.9762 261.4416
## 110.7619 226.0615 202.7054 249.4177 190.3414 261.7817
## 110.8095 226.7459 203.0752 250.4167 190.5447 262.9472
## 110.8571 226.6356 202.6544 250.6169 189.9594 263.3118
## 110.9048 226.7341 202.4462 251.0221 189.5889 263.8793
## 110.9524 226.8663 202.2754 251.4572 189.2578 264.4748
## 111.0000 226.2505 201.3603 251.1408 188.1842 264.3169
autoplot(forecast(fit1))
# Holt Winter’s (Multiplicative) Now we will use Holt Winter’s Multiplicative Seasonality method of ETS modelling to capture the Multiplicative nature of the seasonality present in the time series:
# with multiplicative seasonality
fitm <-hw(windata,seasonal = "multiplicative")
summary(fitm)
##
## Forecast method: Holt-Winters' multiplicative method
##
## Model Information:
## Holt-Winters' multiplicative method
##
## Call:
## hw(y = windata, seasonal = "multiplicative")
##
## Smoothing parameters:
## alpha = 0.5388
## beta = 0.001
## gamma = 0.0862
##
## Initial states:
## l = 181.7936
## b = -0.2527
## s = 1.024 0.9923 1.0091 0.9722 0.9975 1.0144
## 0.984 0.9941 0.9806 0.9982 1.006 0.9642 1.0205 1.0283 1.0389 0.9946 1.0242 0.9878 0.9981 1.0052 0.9657
##
## sigma: 0.019
##
## AIC AICc BIC
## 23489.97 23490.59 23638.87
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.1054765 3.625824 2.679219 0.05079596 1.38776 0.2505457 0.397025
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 109.0476 224.2716 218.8069 229.7362 215.9141 232.6290
## 109.0952 223.7028 217.5083 229.8973 214.2291 233.1765
## 109.1429 222.8480 216.0138 229.6823 212.3960 233.3001
## 109.1905 223.2180 215.7647 230.6712 211.8192 234.6167
## 109.2381 222.7227 214.7235 230.7220 210.4889 234.9565
## 109.2857 222.1470 213.6423 230.6516 209.1402 235.1537
## 109.3333 223.6263 214.5645 232.6881 209.7675 237.4851
## 109.3810 223.5049 213.9723 233.0375 208.9260 238.0838
## 109.4286 225.7871 215.6977 235.8765 210.3567 241.2175
## 109.4762 225.6606 215.1361 236.1851 209.5648 241.7565
## 109.5238 225.8492 214.8909 236.8074 209.0900 242.6084
## 109.5714 225.5170 214.1649 236.8691 208.1554 242.8786
## 109.6190 225.6208 213.8662 237.3755 207.6436 243.5980
## 109.6667 224.3841 212.3103 236.4580 205.9188 242.8495
## 109.7143 223.5691 211.1673 235.9710 204.6022 242.5361
## 109.7619 224.3202 211.5132 237.1272 204.7336 243.9068
## 109.8095 224.4275 211.2594 237.5956 204.2887 244.5663
## 109.8571 223.2638 209.8189 236.7087 202.7016 243.8260
## 109.9048 225.0645 211.1708 238.9583 203.8159 246.3132
## 109.9524 225.6274 211.3646 239.8903 203.8143 247.4406
## 110.0000 225.4329 210.8548 240.0110 203.1376 247.7282
## 110.0476 224.2032 209.2804 239.1261 201.3807 247.0258
## 110.0952 223.6346 208.4384 238.8309 200.3939 246.8753
## 110.1429 222.7801 207.3370 238.2232 199.1619 246.3983
## 110.1905 223.1500 207.3806 238.9193 199.0328 247.2671
## 110.2381 222.6549 206.6251 238.6846 198.1394 247.1703
## 110.2857 222.0793 205.8006 238.3580 197.1832 246.9754
## 110.3333 223.5582 206.8829 240.2335 198.0555 249.0608
## 110.3810 223.4368 206.4864 240.3872 197.5134 249.3602
## 110.4286 225.7183 208.3115 243.1251 199.0969 252.3398
## 110.4762 225.5919 207.9151 243.2686 198.5576 252.6261
## 110.5238 225.7803 207.8123 243.7484 198.3006 253.2601
## 110.5714 225.4483 207.2337 243.6628 197.5916 253.3050
## 110.6190 225.5521 207.0592 244.0450 197.2697 253.8345
## 110.6667 224.3158 205.6587 242.9728 195.7822 252.8493
## 110.7143 223.5010 204.6499 242.3522 194.6707 252.3313
## 110.7619 224.2518 205.0773 243.4263 194.9270 253.5767
## 110.8095 224.3591 204.9178 243.8004 194.6262 254.0919
## 110.8571 223.1958 203.6015 242.7900 193.2289 253.1626
## 110.9048 224.9959 204.9902 245.0017 194.3997 255.5921
## 110.9524 225.5587 205.2510 245.8663 194.5008 256.6165
## 111.0000 225.3642 204.8247 245.9037 193.9517 256.7767
autoplot(fitm)
Damped means to have flattening effect of trend over a period of time, so we use Holt’s Damped multiplicative seasonality.
fitdm<- hw(windata,damped = TRUE,seasonal = "multiplicative")
summary(fitdm)
##
## Forecast method: Damped Holt-Winters' multiplicative method
##
## Model Information:
## Damped Holt-Winters' multiplicative method
##
## Call:
## hw(y = windata, seasonal = "multiplicative", damped = TRUE)
##
## Smoothing parameters:
## alpha = 0.8363
## beta = 5e-04
## gamma = 0.035
## phi = 0.9799
##
## Initial states:
## l = 173.0678
## b = -0.2781
## s = 1 1.0045 1.0055 0.983 1.009 1.0059
## 1.0031 0.9987 0.9961 0.9992 1.0031 1.0002 0.9996 1.0063 1 1.0023 1.0006 0.9918 0.9977 1.0023 0.9912
##
## sigma: 0.0164
##
## AIC AICc BIC
## 22824.96 22825.63 22979.59
##
## Error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.03302854 3.1742 2.312224 0.003124961 1.188107 0.2162263
## ACF1
## Training set 0.1164474
##
## Forecasts:
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 109.0476 226.8788 222.1077 231.6499 219.5820 234.1756
## 109.0952 226.9791 220.7548 233.2033 217.4599 236.4983
## 109.1429 225.9630 218.5988 233.3272 214.7004 237.2256
## 109.1905 226.0567 217.6813 234.4321 213.2477 238.8658
## 109.2381 225.8564 216.5890 235.1238 211.6831 240.0297
## 109.2857 225.7027 215.6215 235.7838 210.2849 241.1204
## 109.3333 226.7288 215.8394 237.6183 210.0748 243.3828
## 109.3810 226.0574 214.4890 237.6259 208.3650 243.7499
## 109.4286 226.4623 214.2012 238.7234 207.7105 245.2141
## 109.4762 226.2628 213.3754 239.1503 206.5531 245.9726
## 109.5238 226.5046 212.9949 240.0144 205.8433 247.1660
## 109.5714 226.7176 212.6118 240.8235 205.1447 248.2906
## 109.6190 226.8033 212.1311 241.4755 204.3641 249.2424
## 109.6667 226.6185 211.4177 241.8192 203.3709 249.8660
## 109.7143 226.7766 211.0424 242.5108 202.7132 250.8400
## 109.7619 227.5136 211.2200 243.8071 202.5947 252.4324
## 109.8095 227.9004 211.0850 244.7159 202.1834 253.6175
## 109.8571 227.3884 210.1312 244.6455 200.9958 253.7809
## 109.9048 227.7549 210.0021 245.5076 200.6044 254.9053
## 109.9524 227.5834 209.3882 245.7785 199.7562 255.4105
## 110.0000 226.9345 208.3474 245.5217 198.5079 255.3611
## 110.0476 227.0316 207.9661 246.0971 197.8735 256.1897
## 110.0952 227.1289 207.6310 246.6268 197.3095 256.9483
## 110.1429 226.1092 206.2852 245.9332 195.7909 256.4275
## 110.1905 226.2000 205.9623 246.4378 195.2490 257.1511
## 110.2381 225.9968 205.3794 246.6142 194.4652 257.5284
## 110.2857 225.8401 204.8467 246.8336 193.7334 257.9469
## 110.3333 226.8642 205.3902 248.3382 194.0226 259.7058
## 110.3810 226.1897 204.4018 247.9776 192.8680 259.5113
## 110.4286 226.5922 204.3933 248.7911 192.6419 260.5425
## 110.4762 226.3900 203.8449 248.9352 191.9102 260.8699
## 110.5238 226.6294 203.6995 249.5594 191.5611 261.6978
## 110.5714 226.8401 203.5329 250.1473 191.1948 262.4854
## 110.6190 226.9233 203.2565 250.5901 190.7281 263.1185
## 110.6667 226.7360 202.7429 250.7291 190.0417 263.4303
## 110.7143 226.8919 202.5408 251.2429 189.6502 264.1336
## 110.7619 227.6270 202.8589 252.3950 189.7475 265.5064
## 110.8095 228.0118 202.8675 253.1560 189.5570 266.4666
## 110.8571 227.4972 202.0803 252.9142 188.6253 266.3691
## 110.9048 227.8617 202.0780 253.6455 188.4290 267.2945
## 110.9524 227.6881 201.6021 253.7740 187.7930 267.5831
## 111.0000 227.0369 200.7082 253.3655 186.7706 267.3031
autoplot(fitdm)
Thank you