library(readr)
library(fpp2)
## Loading required package: ggplot2
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## Loading required package: forecast
## Registered S3 method overwritten by 'xts':
##   method     from
##   as.zoo.xts zoo
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Registered S3 methods overwritten by 'forecast':
##   method             from    
##   fitted.fracdiff    fracdiff
##   residuals.fracdiff fracdiff
## Loading required package: fma
## Loading required package: expsmooth
library(forecast)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
SPY <- read_csv("C:/Users/Joseph/Downloads/SPY.csv")
## Parsed with column specification:
## cols(
##   Date = col_date(format = ""),
##   Open = col_double(),
##   High = col_double(),
##   Low = col_double(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_double()
## )
SPY.ts <- ts(SPY$`Adj Close`, frequency = 30, start = min(SPY$Date))
plot(SPY.ts)

fit <- tslm(SPY.ts ~ trend + season)

CV(fit)
##           CV          AIC         AICc          BIC        AdjR2 
##  110.8281042 2368.8019979 2373.2956150 2503.8608834    0.5936367
forecast(fit,h = 30)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 17373.77       288.6207 275.0824 302.1591 267.8914 309.3501
## 17373.80       288.0194 274.4811 301.5578 267.2901 308.7487
## 17373.83       286.9485 273.4102 300.4869 266.2192 307.6779
## 17373.87       286.5437 273.0053 300.0821 265.8144 307.2730
## 17373.90       285.2728 271.7344 298.8111 264.5434 306.0021
## 17373.93       284.8677 271.3293 298.4060 264.1383 305.5970
## 17373.97       286.1340 272.5956 299.6723 265.4047 306.8633
## 17374.00       285.6366 272.1168 299.1563 264.9357 306.3374
## 17374.03       286.4388 272.9190 299.9586 265.7379 307.1397
## 17374.07       287.3819 273.8621 300.9017 266.6811 308.0828
## 17374.10       287.5637 274.0440 301.0835 266.8629 308.2646
## 17374.13       287.5814 274.0616 301.1012 266.8805 308.2822
## 17374.17       287.7891 274.2693 301.3089 267.0882 308.4900
## 17374.20       287.6497 274.1299 301.1694 266.9488 308.3505
## 17374.23       287.9528 274.4330 301.4726 267.2519 308.6536
## 17374.27       287.5695 274.0497 301.0892 266.8686 308.2703
## 17374.30       288.7893 275.2695 302.3090 268.0884 309.4901
## 17374.33       288.8838 275.3641 302.4036 268.1830 309.5847
## 17374.37       289.1504 275.6307 302.6702 268.4496 309.8513
## 17374.40       289.2663 275.7465 302.7861 268.5654 309.9671
## 17374.43       288.6850 275.1653 302.2048 267.9842 309.3859
## 17374.47       289.5517 276.0319 303.0714 268.8508 310.2525
## 17374.50       290.2195 276.6997 303.7393 269.5187 310.9204
## 17374.53       290.5490 277.0292 304.0688 269.8481 311.2499
## 17374.57       290.4367 276.9169 303.9565 269.7359 311.1376
## 17374.60       290.7344 277.2146 304.2542 270.0335 311.4352
## 17374.63       291.5691 278.0493 305.0888 270.8682 312.2699
## 17374.67       291.2348 277.7151 304.7546 270.5340 311.9357
## 17374.70       291.4799 277.9602 304.9997 270.7791 312.1808
## 17374.73       291.7999 278.2801 305.3197 271.0991 312.5008
plot(forecast(fit,h=30))

#this model is realtively inaccurate as there the forecasts have >30$ spread which is ~10%

#HoltWinters 

fit2 <- HoltWinters(SPY.ts, beta = F, gamma = F)

forecast(fit2,h = 30)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 17373.77       297.1882 294.2199 300.1565 292.6485 301.7278
## 17373.80       297.1882 293.0010 301.3754 290.7844 303.5920
## 17373.83       297.1882 292.0642 302.3121 289.3518 305.0246
## 17373.87       297.1882 291.2741 303.1023 288.1433 306.2330
## 17373.90       297.1882 290.5777 303.7987 287.0783 307.2981
## 17373.93       297.1882 289.9480 304.4284 286.1152 308.2611
## 17373.97       297.1882 289.3688 305.0076 285.2295 309.1469
## 17374.00       297.1882 288.8297 305.5467 284.4049 309.9714
## 17374.03       297.1882 288.3233 306.0531 283.6305 310.7459
## 17374.07       297.1882 287.8443 306.5321 282.8979 311.4784
## 17374.10       297.1882 287.3887 306.9877 282.2011 312.1752
## 17374.13       297.1882 286.9533 307.4230 281.5353 312.8411
## 17374.17       297.1882 286.5357 307.8406 280.8967 313.4797
## 17374.20       297.1882 286.1339 308.2424 280.2822 314.0942
## 17374.23       297.1882 285.7462 308.6301 279.6892 314.6871
## 17374.27       297.1882 285.3712 309.0051 279.1157 315.2606
## 17374.30       297.1882 285.0078 309.3686 278.5598 315.8165
## 17374.33       297.1882 284.6548 309.7215 278.0201 316.3562
## 17374.37       297.1882 284.3116 310.0648 277.4951 316.8812
## 17374.40       297.1882 283.9773 310.3991 276.9838 317.3925
## 17374.43       297.1882 283.6512 310.7252 276.4851 317.8912
## 17374.47       297.1882 283.3328 311.0436 275.9982 318.3782
## 17374.50       297.1882 283.0215 311.3548 275.5221 318.8542
## 17374.53       297.1882 282.7170 311.6594 275.0564 319.3200
## 17374.57       297.1882 282.4187 311.9577 274.6002 319.7762
## 17374.60       297.1882 282.1263 312.2500 274.1530 320.2233
## 17374.63       297.1882 281.8395 312.5369 273.7144 320.6620
## 17374.67       297.1882 281.5579 312.8184 273.2838 321.0925
## 17374.70       297.1882 281.2814 313.0950 272.8608 321.5155
## 17374.73       297.1882 281.0095 313.3668 272.4451 321.9312
plot(forecast(fit2,h=30))