library(forecast)
## Warning: package 'forecast' was built under R version 3.4.3
library(xts)
## Warning: package 'xts' was built under R version 3.4.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.4.2
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(TTR)
## Warning: package 'TTR' was built under R version 3.4.3
library(tseries)
## Warning: package 'tseries' was built under R version 3.4.3
g<-read.csv("D:/Boston College/MS AE Courses/Spring 2018 - Forecasting Predictive Analytics/Discussions - weekly/GOOG.csv")
# Alerternates of reading this data
# getSymbols("GOOG",from = "2015-03-14",to = "2018-03-14",src = "yahoo")
# g1<-get.hist.quote(instrument = "GOOG", start="2016-03-14", end=Sys.Date(),quote = "AdjClose")
# head(g1)
# g2<-read.csv(file.choose(),header=T)
# head(g2)
names(g)
## [1] "Date" "Open" "High" "Low" "Close" "Adj.Close"
## [7] "Volume"
head(g)
## Date Open High Low Close Adj.Close Volume
## 1 2016-03-16 726.37 737.47 724.510 736.09 736.09 1624400
## 2 2016-03-17 736.45 743.07 736.000 737.78 737.78 1860800
## 3 2016-03-18 741.86 742.00 731.830 737.60 737.60 2980700
## 4 2016-03-21 736.50 742.50 733.516 742.09 742.09 1836500
## 5 2016-03-22 737.46 745.00 737.460 740.75 740.75 1269700
## 6 2016-03-23 742.36 745.72 736.150 738.06 738.06 1432100
g<-data.frame(g,o=order(g$Date,decreasing=FALSE))
g=g[,1:7]
# Plot original series
plot(g$Adj.Close,type="l",xlab="time",ylab="closing price",main="historical data")
# additional plots
monthplot(g$Adj.Close)
#seasonplot(g$Adj.Close)
# Data is not seasonal - breaks into an error
g.ts<-ts(g, start=c(2016, 3), end=c(2018,3), frequency=252)
#plot(g.ts)
g.ts.f<-forecast(g$Adj.Close, 21)
plot(g.ts.f)
g.ts.f
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 505 1149.58 1132.987 1166.173 1124.203 1174.957
## 506 1149.58 1126.114 1173.046 1113.692 1185.468
## 507 1149.58 1120.840 1178.320 1105.626 1193.534
## 508 1149.58 1116.393 1182.767 1098.825 1200.335
## 509 1149.58 1112.475 1186.685 1092.832 1206.328
## 510 1149.58 1108.932 1190.228 1087.414 1211.746
## 511 1149.58 1105.674 1193.486 1082.431 1216.729
## 512 1149.58 1102.641 1196.519 1077.793 1221.367
## 513 1149.58 1099.792 1199.368 1073.436 1225.724
## 514 1149.58 1097.097 1202.063 1069.315 1229.845
## 515 1149.58 1094.534 1204.626 1065.394 1233.766
## 516 1149.58 1092.084 1207.075 1061.648 1237.512
## 517 1149.58 1089.735 1209.425 1058.055 1241.105
## 518 1149.58 1087.474 1211.686 1054.597 1244.563
## 519 1149.58 1085.292 1213.868 1051.260 1247.900
## 520 1149.58 1083.182 1215.978 1048.032 1251.127
## 521 1149.58 1081.136 1218.024 1044.904 1254.256
## 522 1149.58 1079.149 1220.010 1041.866 1257.294
## 523 1149.58 1077.217 1221.943 1038.911 1260.249
## 524 1149.58 1075.335 1223.825 1036.032 1263.128
## 525 1149.58 1073.499 1225.661 1033.224 1265.936
Flat forecasts instead were fixed with the exponential model
# Exponential Time Series
g.ts.ets<-ets(g$Adj.Close)
plot(g.ts.ets)
g.ts.ets
## ETS(M,N,N)
##
## Call:
## ets(y = g$Adj.Close)
##
## Smoothing parameters:
## alpha = 0.9999
##
## Initial states:
## l = 736.0291
##
## sigma: 0.0113
##
## AIC AICc BIC
## 5434.555 5434.603 5447.223
# Display the forecasts
g.ts.ets$fitted
## Time Series:
## Start = 1
## End = 504
## Frequency = 1
## [1] 736.0291 736.0900 737.7799 737.6000 742.0896 740.7501 738.0603
## [8] 735.3003 733.5302 744.7689 750.5294 744.9506 749.9095 745.2904
## [15] 737.8007 745.6892 740.2806 739.1501 736.1003 743.0893 751.7191
## [22] 753.1999 758.9994 766.6092 753.9313 752.6701 759.1394 718.7741
## [29] 723.1496 708.1415 705.8403 691.0215 693.0098 698.2095 692.3606
## [36] 695.6997 701.4294 711.1190 712.8998 723.1790 715.2908 713.3102
## [43] 710.8303 716.4894 706.2310 706.6300 700.3206 709.7390 704.2405
## [50] 720.0884 725.2695 724.1201 732.6591 735.7197 734.1502 730.4004
## [57] 722.3408 716.5506 716.6500 728.2789 728.5800 719.4109 718.3601
## [64] 718.2700 718.9199 710.3608 691.7218 693.7098 695.9398 697.4599
## [71] 701.8696 675.2227 668.2607 680.0388 684.1096 692.0992 699.2093
## [78] 694.9504 697.7697 695.3602 705.6290 715.0891 720.6395 716.9803
## [85] 720.9496 719.8501 733.7786 736.9597 741.1896 738.6303 742.7396
## [92] 739.7703 738.4201 741.7697 745.9096 768.7877 772.8796 771.0702
## [99] 773.1798 771.6101 782.2189 781.7601 784.2598 784.6800 784.8500
## [106] 783.2201 782.4401 777.1405 779.9097 777.5002 775.4202 772.1504
## [113] 772.0800 769.6403 769.4100 769.5400 772.1498 769.0903 767.0502
## [120] 768.7799 771.4598 780.0792 780.3499 775.3205 759.6615 769.0191
## [127] 759.6909 762.4897 771.7591 768.8803 765.7003 771.4094 776.2195
## [134] 787.2089 786.9001 774.2113 783.0091 781.5601 775.0107 777.2897
## [141] 772.5605 776.4296 776.4700 776.8599 775.0802 785.9389 783.0703
## [148] 786.1397 778.1908 778.5300 779.9599 795.2585 801.4994 796.9704
## [155] 799.3698 813.1086 807.6705 799.0709 795.3504 795.3700 784.5411
## [162] 783.6101 768.7015 762.1307 762.0200 782.5180 790.5092 785.3105
## [169] 762.5623 754.0209 736.0818 758.4877 764.4794 771.2293 760.5411
## [176] 769.1991 768.2701 760.9907 761.6799 768.2393 770.8398 758.0413
## [183] 747.9210 750.4997 762.5188 759.1103 771.1888 776.4195 789.2887
## [190] 789.2700 796.0993 797.0699 797.8499 790.8007 794.1997 796.4198
## [197] 794.5602 791.2603 789.9101 791.5498 785.0506 782.7902 771.8211
## [204] 786.1386 786.8999 794.0193 806.1488 806.6500 804.7902 807.9097
## [211] 806.3601 807.8799 804.6103 806.0699 802.1754 805.0197 819.3086
## [218] 823.8695 835.6688 832.1504 823.3109 802.3221 796.7905 795.6951
## [225] 798.5297 801.4897 801.3400 806.9694 808.3799 809.5599 813.6696
## [232] 819.2394 820.4499 818.9801 824.1595 828.0696 831.6596 830.7601
## [239] 831.3300 828.6403 829.2800 823.2106 835.2388 830.6305 829.0802
## [246] 827.7802 831.9096 835.3696 838.6797 843.2495 845.5397 845.6200
## [253] 847.1999 848.7799 852.1197 848.4004 830.4618 829.5901 817.5812
## [260] 814.4303 819.5095 820.9198 831.4089 831.5000 829.5602 838.5491
## [267] 834.5704 831.4103 827.8804 824.6703 824.7300 823.3501 824.3199
## [274] 823.5601 837.1686 836.8200 838.2099 841.6497 843.1898 862.7580
## [281] 872.2990 871.7300 874.2497 905.9568 912.5693 916.4396 927.0389
## [288] 931.6595 927.1305 934.2993 932.1702 928.7804 930.5998 932.2198
## [295] 937.0795 942.9994 919.6223 930.2389 934.0096 941.8592 948.8193
## [302] 954.9594 969.5385 971.4698 975.8796 964.8611 966.9498 975.5991
## [309] 983.6792 976.5707 980.9396 983.4097 949.8334 942.9007 953.3990
## [316] 950.7603 942.3108 939.7803 957.3682 950.6307 959.4491 957.0903
## [323] 965.5892 952.2714 927.3325 940.4887 917.7923 908.7309 898.7010
## [330] 911.7087 906.6905 918.5888 928.7990 930.0899 943.8286 947.1596
## [337] 955.9891 953.4202 965.3988 970.8895 968.1503 972.9195 980.3393
## [344] 950.7030 947.8003 934.0914 941.5293 930.5011 930.8300 930.3901
## [351] 923.6507 927.9596 929.3598 926.7902 922.9004 907.2416 914.3893
## [358] 922.6692 922.2200 926.9595 910.9816 910.6700 906.6604 924.6882
## [365] 926.9998 921.2806 915.8906 913.8102 921.2892 929.5692 939.3290
## [372] 937.3402 928.4509 927.8101 935.9492 926.5010 929.0798 932.0697
## [379] 935.0897 925.1110 920.2905 915.0005 921.8093 931.5790 932.4499
## [386] 928.5304 920.9707 924.8596 944.4880 949.4995 959.1090 953.2706
## [393] 957.7895 951.6806 969.9582 978.8891 977.0002 972.6004 989.2483
## [400] 987.8302 989.6798 991.9998 992.1800 992.8099 984.4509 988.1996
## [407] 968.4520 970.5398 973.3297 972.5601 1019.2653 1017.1102 1016.6401
## [414] 1025.4991 1025.5799 1032.4793 1025.9007 1033.3292 1039.8493 1031.2609
## [421] 1028.0703 1025.7502 1026.0000 1020.9105 1032.4988 1019.0914 1018.3801
## [428] 1034.4884 1035.9598 1040.6095 1054.2086 1047.4107 1021.6626 1021.4100
## [435] 1010.1711 998.6811 1005.1494 1018.3787 1030.9288 1037.0494 1041.0996
## [442] 1040.4800 1040.6100 1049.1492 1064.1884 1077.1387 1070.6807 1064.9505
## [449] 1063.6301 1060.1203 1056.7403 1049.3707 1048.1401 1046.4002 1064.9981
## [456] 1082.4782 1086.3996 1102.2284 1106.9395 1106.2601 1102.6104 1105.5197
## [463] 1122.2583 1121.7601 1131.9790 1129.7903 1137.5092 1155.8082 1169.9685
## [470] 1164.2406 1170.3694 1175.8394 1175.5800 1163.6911 1169.9393 1167.7002
## [477] 1111.9056 1055.8057 1080.5975 1048.5832 1001.5248 1037.7764 1051.9385
## [484] 1052.1000 1069.6982 1089.5180 1094.7995 1102.4592 1111.3391 1106.6305
## [491] 1126.7880 1143.7483 1118.2926 1104.7313 1069.5236 1078.9191 1090.9288
## [498] 1095.0596 1109.6385 1125.9984 1160.0366 1164.4996 1138.1727 1149.4889