Grab stock file from quantmod and install forecast package. Print first few observations and plot overall timeseries.
library("quantmod", lib.loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library("forecast")
## Loading required package: timeDate
## This is forecast 6.1
#Get Valeant (symbol VRX) from yahoo
getSymbols(Symbols='VRX', src='yahoo', from='2013-09-28')
## As of 0.4-0, 'getSymbols' uses env=parent.frame() and
## auto.assign=TRUE by default.
##
## This behavior will be phased out in 0.5-0 when the call will
## default to use auto.assign=FALSE. getOption("getSymbols.env") and
## getOptions("getSymbols.auto.assign") are now checked for alternate defaults
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
## [1] "VRX"
VRX.sample <- window(VRX, end='2013-10-10')
print(VRX.sample)
## VRX.Open VRX.High VRX.Low VRX.Close VRX.Volume VRX.Adjusted
## 2013-09-30 103.57 105.12 103.22 104.33 782100 104.33
## 2013-10-01 104.04 108.17 104.04 108.16 1171500 108.16
## 2013-10-02 106.92 109.84 106.66 109.80 1529100 109.80
## 2013-10-03 110.67 113.79 110.12 111.40 2856000 111.40
## 2013-10-04 111.22 112.81 110.39 111.01 1743100 111.01
## 2013-10-07 110.08 112.21 110.01 110.32 1248300 110.32
## 2013-10-08 110.52 110.52 105.88 105.92 1727700 105.92
## 2013-10-09 105.80 107.36 104.00 107.03 1307800 107.03
## 2013-10-10 108.16 110.06 108.13 110.00 871100 110.00
plot.ts(VRX$VRX.Adjusted)
Create train set
VRX_Train <- window(VRX, end='2015-07-01')
VRX_Test <- window(VRX, start='2015-07-02')
Auto Forecast
VRX_forecast <- forecast(VRX_Train$VRX.Adjusted, h=61)
plot(VRX_forecast)
print(VRX_forecast)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 443 228.2394 222.2141 234.2647 219.0245 237.4543
## 444 228.2394 219.7178 236.7609 215.2068 241.2720
## 445 228.2394 217.8017 238.6771 212.2764 244.2024
## 446 228.2394 216.1859 240.2929 209.8051 246.6737
## 447 228.2394 214.7618 241.7170 207.6271 248.8517
## 448 228.2394 213.4739 243.0049 205.6575 250.8213
## 449 228.2394 212.2892 244.1896 203.8456 252.6332
## 450 228.2394 211.1861 245.2927 202.1586 254.3202
## 451 228.2394 210.1497 246.3291 200.5736 255.9051
## 452 228.2394 209.1692 247.3096 199.0741 257.4047
## 453 228.2394 208.2363 248.2425 197.6473 258.8315
## 454 228.2394 207.3447 249.1341 196.2837 260.1951
## 455 228.2394 206.4892 249.9896 194.9753 261.5035
## 456 228.2394 205.6657 250.8130 193.7160 262.7628
## 457 228.2394 204.8710 251.6078 192.5005 263.9783
## 458 228.2394 204.1020 252.3767 191.3245 265.1543
## 459 228.2394 203.3565 253.1222 190.1844 266.2944
## 460 228.2394 202.6324 253.8463 189.0769 267.4018
## 461 228.2394 201.9280 254.5508 187.9995 268.4792
## 462 228.2394 201.2416 255.2372 186.9498 269.5290
## 463 228.2394 200.5719 255.9069 185.9257 270.5531
## 464 228.2394 199.9178 256.5609 184.9253 271.5535
## 465 228.2394 199.2783 257.2005 183.9472 272.5316
## 466 228.2394 198.6522 257.8266 182.9897 273.4891
## 467 228.2394 198.0389 258.4399 182.0518 274.4270
## 468 228.2394 197.4376 259.0412 181.1321 275.3467
## 469 228.2394 196.8475 259.6313 180.2296 276.2492
## 470 228.2394 196.2680 260.2107 179.3434 277.1354
## 471 228.2394 195.6987 260.7801 178.4727 278.0061
## 472 228.2394 195.1389 261.3399 177.6165 278.8623
## 473 228.2394 194.5882 261.8906 176.7743 279.7045
## 474 228.2394 194.0461 262.4327 175.9452 280.5336
## 475 228.2394 193.5122 262.9666 175.1288 281.3500
## 476 228.2394 192.9862 263.4926 174.3243 282.1545
## 477 228.2394 192.4678 264.0110 173.5314 282.9474
## 478 228.2394 191.9565 264.5223 172.7495 283.7293
## 479 228.2394 191.4521 265.0267 171.9781 284.5007
## 480 228.2394 190.9543 265.5245 171.2168 285.2620
## 481 228.2394 190.4629 266.0159 170.4652 286.0136
## 482 228.2394 189.9776 266.5012 169.7230 286.7558
## 483 228.2394 189.4982 266.9806 168.9898 287.4890
## 484 228.2394 189.0244 267.4544 168.2652 288.2136
## 485 228.2394 188.5561 267.9227 167.5490 288.9298
## 486 228.2394 188.0930 268.3858 166.8408 289.6380
## 487 228.2394 187.6350 268.8437 166.1404 290.3384
## 488 228.2394 187.1820 269.2968 165.4475 291.0313
## 489 228.2394 186.7337 269.7451 164.7619 291.7169
## 490 228.2394 186.2900 270.1888 164.0834 292.3954
## 491 228.2394 185.8508 270.6280 163.4116 293.0672
## 492 228.2394 185.4159 271.0629 162.7465 293.7323
## 493 228.2394 184.9852 271.4936 162.0878 294.3910
## 494 228.2394 184.5585 271.9203 161.4353 295.0435
## 495 228.2394 184.1358 272.3430 160.7888 295.6900
## 496 228.2394 183.7170 272.7618 160.1482 296.3306
## 497 228.2394 183.3018 273.1770 159.5133 296.9655
## 498 228.2394 182.8903 273.5885 158.8840 297.5948
## 499 228.2394 182.4823 273.9965 158.2600 298.2188
## 500 228.2394 182.0778 274.4010 157.6413 298.8375
## 501 228.2394 181.6766 274.8022 157.0277 299.4511
## 502 228.2394 181.2786 275.2001 156.4191 300.0597
## 503 228.2394 180.8839 275.5949 155.8154 300.6634
Drift Forecast
VRX.drift <- rwf(VRX_Train$VRX.Adjusted, drift=TRUE, h=61)
plot(VRX.drift)
print(VRX.drift)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 443 228.5210 224.6582 232.3838 222.6134 234.4286
## 444 228.8020 223.3330 234.2709 220.4379 237.1660
## 445 229.0829 222.3773 235.7886 218.8275 239.3384
## 446 229.3639 221.6122 237.1157 217.5086 241.2192
## 447 229.6449 220.9684 238.3213 216.3754 242.9144
## 448 229.9259 220.4106 239.4411 215.3736 244.4781
## 449 230.2068 219.9177 240.4959 214.4710 245.9427
## 450 230.4878 219.4760 241.4996 213.6467 247.3289
## 451 230.7688 219.0760 242.4615 212.8862 248.6513
## 452 231.0498 218.7108 243.3887 212.1790 249.9205
## 453 231.3307 218.3752 244.2863 211.5169 251.1445
## 454 231.6117 218.0651 245.1583 210.8940 252.3294
## 455 231.8927 217.7774 246.0080 210.3052 253.4801
## 456 232.1737 217.5094 246.8379 209.7466 254.6007
## 457 232.4546 217.2590 247.6502 209.2150 255.6943
## 458 232.7356 217.0245 248.4468 208.7075 256.7637
## 459 233.0166 216.8042 249.2290 208.2219 257.8113
## 460 233.2976 216.5969 249.9982 207.7562 258.8389
## 461 233.5785 216.4016 250.7555 207.3087 259.8484
## 462 233.8595 216.2172 251.5018 206.8779 260.8411
## 463 234.1405 216.0429 252.2381 206.4626 261.8183
## 464 234.4215 215.8780 252.9650 206.0616 262.7813
## 465 234.7024 215.7217 253.6832 205.6739 263.7309
## 466 234.9834 215.5736 254.3932 205.2986 264.6682
## 467 235.2644 215.4330 255.0958 204.9349 265.5938
## 468 235.5454 215.2996 255.7912 204.5821 266.5086
## 469 235.8263 215.1728 256.4799 204.2395 267.4132
## 470 236.1073 215.0523 257.1623 203.9065 268.3082
## 471 236.3883 214.9378 257.8388 203.5825 269.1940
## 472 236.6693 214.8288 258.5097 203.2672 270.0713
## 473 236.9502 214.7252 259.1752 202.9600 270.9404
## 474 237.2312 214.6267 259.8357 202.6606 271.8018
## 475 237.5122 214.5329 260.4914 202.3684 272.6559
## 476 237.7932 214.4437 261.1426 202.0833 273.5030
## 477 238.0741 214.3589 261.7894 201.8048 274.3434
## 478 238.3551 214.2782 262.4320 201.5327 275.1775
## 479 238.6361 214.2015 263.0706 201.2666 276.0055
## 480 238.9171 214.1286 263.7055 201.0064 276.8277
## 481 239.1980 214.0593 264.3367 200.7517 277.6443
## 482 239.4790 213.9936 264.9645 200.5024 278.4556
## 483 239.7600 213.9311 265.5888 200.2581 279.2618
## 484 240.0410 213.8719 266.2100 200.0188 280.0631
## 485 240.3219 213.8158 266.8281 199.7843 280.8596
## 486 240.6029 213.7626 267.4432 199.5542 281.6516
## 487 240.8839 213.7123 268.0554 199.3286 282.4392
## 488 241.1649 213.6648 268.6649 199.1072 283.2225
## 489 241.4458 213.6200 269.2717 198.8898 284.0018
## 490 241.7268 213.5777 269.8759 198.6764 284.7772
## 491 242.0078 213.5379 270.4777 198.4668 285.5487
## 492 242.2888 213.5005 271.0770 198.2609 286.3166
## 493 242.5697 213.4654 271.6741 198.0585 287.0809
## 494 242.8507 213.4326 272.2688 197.8596 287.8418
## 495 243.1317 213.4019 272.8614 197.6640 288.5994
## 496 243.4127 213.3734 273.4519 197.4716 289.3537
## 497 243.6936 213.3469 274.0404 197.2823 290.1050
## 498 243.9746 213.3224 274.6268 197.0961 290.8531
## 499 244.2556 213.2998 275.2114 196.9128 291.5984
## 500 244.5366 213.2791 275.7940 196.7324 292.3408
## 501 244.8175 213.2602 276.3749 196.5547 293.0804
## 502 245.0985 213.2430 276.9540 196.3797 293.8173
## 503 245.3795 213.2276 277.5314 196.2074 294.5516
Naive Forecast
VRX.naive <- naive(VRX_Train$VRX.Adjusted, h=61)
plot(VRX.naive)
print(VRX.naive)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 443 228.24 224.3692 232.1108 222.3201 234.1599
## 444 228.24 222.7658 233.7142 219.8680 236.6120
## 445 228.24 221.5355 234.9445 217.9864 238.4936
## 446 228.24 220.4984 235.9817 216.4002 240.0798
## 447 228.24 219.5846 236.8954 215.0027 241.4773
## 448 228.24 218.7585 237.7215 213.7392 242.7408
## 449 228.24 217.9988 238.4812 212.5774 243.9026
## 450 228.24 217.2917 239.1883 211.4960 244.9841
## 451 228.24 216.6275 239.8525 210.4803 245.9997
## 452 228.24 215.9994 240.4806 209.5196 246.9604
## 453 228.24 215.4019 241.0781 208.6059 247.8741
## 454 228.24 214.8311 241.6489 207.7328 248.7472
## 455 228.24 214.2835 242.1965 206.8954 249.5846
## 456 228.24 213.7567 242.7233 206.0897 250.3903
## 457 228.24 213.2484 243.2316 205.3123 251.1677
## 458 228.24 212.7567 243.7233 204.5603 251.9197
## 459 228.24 212.2802 244.1998 203.8316 252.6484
## 460 228.24 211.8175 244.6625 203.1239 253.3561
## 461 228.24 211.3675 245.1125 202.4357 254.0443
## 462 228.24 210.9292 245.5509 201.7653 254.7147
## 463 228.24 210.5017 245.9784 201.1115 255.3685
## 464 228.24 210.0842 246.3958 200.4731 256.0069
## 465 228.24 209.6762 246.8038 199.8491 256.6309
## 466 228.24 209.2769 247.2031 199.2385 257.2415
## 467 228.24 208.8859 247.5941 198.6404 257.8396
## 468 228.24 208.5026 247.9774 198.0542 258.4258
## 469 228.24 208.1266 248.3534 197.4792 259.0008
## 470 228.24 207.7575 248.7225 196.9148 259.5653
## 471 228.24 207.3950 249.0850 196.3603 260.1197
## 472 228.24 207.0386 249.4414 195.8153 260.6647
## 473 228.24 206.6882 249.7918 195.2793 261.2007
## 474 228.24 206.3433 250.1367 194.7519 261.7281
## 475 228.24 206.0038 250.4762 194.2327 262.2473
## 476 228.24 205.6694 250.8106 193.7213 262.7587
## 477 228.24 205.3399 251.1401 193.2173 263.2627
## 478 228.24 205.0151 251.4650 192.7205 263.7595
## 479 228.24 204.6947 251.7853 192.2306 264.2494
## 480 228.24 204.3786 252.1014 191.7472 264.7328
## 481 228.24 204.0667 252.4133 191.2701 265.2099
## 482 228.24 203.7588 252.7212 190.7992 265.6808
## 483 228.24 203.4546 253.0254 190.3341 266.1460
## 484 228.24 203.1542 253.3258 189.8746 266.6054
## 485 228.24 202.8573 253.6227 189.4205 267.0595
## 486 228.24 202.5639 253.9161 188.9717 267.5083
## 487 228.24 202.2737 254.2063 188.5280 267.9520
## 488 228.24 201.9868 254.4932 188.0892 268.3908
## 489 228.24 201.7030 254.7770 187.6551 268.8249
## 490 228.24 201.4221 255.0579 187.2256 269.2544
## 491 228.24 201.1442 255.3358 186.8006 269.6794
## 492 228.24 200.8691 255.6109 186.3799 270.1001
## 493 228.24 200.5968 255.8832 185.9634 270.5167
## 494 228.24 200.3271 256.1529 185.5509 270.9291
## 495 228.24 200.0600 256.4200 185.1424 271.3376
## 496 228.24 199.7954 256.6846 184.7377 271.7423
## 497 228.24 199.5332 256.9468 184.3367 272.1433
## 498 228.24 199.2734 257.2066 183.9394 272.5406
## 499 228.24 199.0159 257.4641 183.5456 272.9344
## 500 228.24 198.7607 257.7193 183.1553 273.3247
## 501 228.24 198.5076 257.9724 182.7683 273.7117
## 502 228.24 198.2567 258.2233 182.3845 274.0955
## 503 228.24 198.0079 258.4721 182.0040 274.4760
Mean Forecast
VRX.meanf <- meanf(VRX_Train$VRX.Adjusted, h=61)
plot(VRX.meanf)
print(VRX.meanf)
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1 146.4881 97.30885 195.6674 71.18091 221.7953
## 2 146.4881 97.30885 195.6674 71.18091 221.7953
## 3 146.4881 97.30885 195.6674 71.18091 221.7953
## 4 146.4881 97.30885 195.6674 71.18091 221.7953
## 5 146.4881 97.30885 195.6674 71.18091 221.7953
## 6 146.4881 97.30885 195.6674 71.18091 221.7953
## 7 146.4881 97.30885 195.6674 71.18091 221.7953
## 8 146.4881 97.30885 195.6674 71.18091 221.7953
## 9 146.4881 97.30885 195.6674 71.18091 221.7953
## 10 146.4881 97.30885 195.6674 71.18091 221.7953
## 11 146.4881 97.30885 195.6674 71.18091 221.7953
## 12 146.4881 97.30885 195.6674 71.18091 221.7953
## 13 146.4881 97.30885 195.6674 71.18091 221.7953
## 14 146.4881 97.30885 195.6674 71.18091 221.7953
## 15 146.4881 97.30885 195.6674 71.18091 221.7953
## 16 146.4881 97.30885 195.6674 71.18091 221.7953
## 17 146.4881 97.30885 195.6674 71.18091 221.7953
## 18 146.4881 97.30885 195.6674 71.18091 221.7953
## 19 146.4881 97.30885 195.6674 71.18091 221.7953
## 20 146.4881 97.30885 195.6674 71.18091 221.7953
## 21 146.4881 97.30885 195.6674 71.18091 221.7953
## 22 146.4881 97.30885 195.6674 71.18091 221.7953
## 23 146.4881 97.30885 195.6674 71.18091 221.7953
## 24 146.4881 97.30885 195.6674 71.18091 221.7953
## 25 146.4881 97.30885 195.6674 71.18091 221.7953
## 26 146.4881 97.30885 195.6674 71.18091 221.7953
## 27 146.4881 97.30885 195.6674 71.18091 221.7953
## 28 146.4881 97.30885 195.6674 71.18091 221.7953
## 29 146.4881 97.30885 195.6674 71.18091 221.7953
## 30 146.4881 97.30885 195.6674 71.18091 221.7953
## 31 146.4881 97.30885 195.6674 71.18091 221.7953
## 32 146.4881 97.30885 195.6674 71.18091 221.7953
## 33 146.4881 97.30885 195.6674 71.18091 221.7953
## 34 146.4881 97.30885 195.6674 71.18091 221.7953
## 35 146.4881 97.30885 195.6674 71.18091 221.7953
## 36 146.4881 97.30885 195.6674 71.18091 221.7953
## 37 146.4881 97.30885 195.6674 71.18091 221.7953
## 38 146.4881 97.30885 195.6674 71.18091 221.7953
## 39 146.4881 97.30885 195.6674 71.18091 221.7953
## 40 146.4881 97.30885 195.6674 71.18091 221.7953
## 41 146.4881 97.30885 195.6674 71.18091 221.7953
## 42 146.4881 97.30885 195.6674 71.18091 221.7953
## 43 146.4881 97.30885 195.6674 71.18091 221.7953
## 44 146.4881 97.30885 195.6674 71.18091 221.7953
## 45 146.4881 97.30885 195.6674 71.18091 221.7953
## 46 146.4881 97.30885 195.6674 71.18091 221.7953
## 47 146.4881 97.30885 195.6674 71.18091 221.7953
## 48 146.4881 97.30885 195.6674 71.18091 221.7953
## 49 146.4881 97.30885 195.6674 71.18091 221.7953
## 50 146.4881 97.30885 195.6674 71.18091 221.7953
## 51 146.4881 97.30885 195.6674 71.18091 221.7953
## 52 146.4881 97.30885 195.6674 71.18091 221.7953
## 53 146.4881 97.30885 195.6674 71.18091 221.7953
## 54 146.4881 97.30885 195.6674 71.18091 221.7953
## 55 146.4881 97.30885 195.6674 71.18091 221.7953
## 56 146.4881 97.30885 195.6674 71.18091 221.7953
## 57 146.4881 97.30885 195.6674 71.18091 221.7953
## 58 146.4881 97.30885 195.6674 71.18091 221.7953
## 59 146.4881 97.30885 195.6674 71.18091 221.7953
## 60 146.4881 97.30885 195.6674 71.18091 221.7953
## 61 146.4881 97.30885 195.6674 71.18091 221.7953
Accuracy test for all four models
accuracy(VRX_forecast, VRX_Test$VRX.Adjusted)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.280470 3.017006 2.109338 0.1567461 1.446801 0.9977775
## Test set 7.164375 17.476944 13.029263 2.5425913 5.558579 6.1632162
## ACF1
## Training set 0.002802568
## Test set NA
accuracy(VRX.drift, VRX_Test$VRX.Adjusted)
## ME RMSE MAE MPE MAPE
## Training set -1.160453e-15 3.007323 2.111299 -0.04570567 1.452016
## Test set -1.546461e+00 18.808721 14.365291 -1.25113438 6.347196
## MASE ACF1
## Training set 0.998705 0.002915599
## Test set 6.795196 NA
accuracy(VRX.naive, VRX_Test$VRX.Adjusted)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.2809751 3.02042 2.114036 0.1569866 1.449993 1.000000
## Test set 7.1637655 17.47669 13.029014 2.5423311 5.558484 6.163098
## ACF1
## Training set 0.002915599
## Test set NA
accuracy(VRX.meanf, VRX_Test$VRX.Adjusted)
## ME RMSE MAE MPE MAPE MASE
## Training set 4.828151e-15 38.23070 30.39671 -5.753473 20.03921 1.000000
## Test set 8.891567e+01 90.33334 88.91567 37.450103 37.45010 2.925174