library(fpp)
## Warning: package 'fpp' was built under R version 3.5.3
## Loading required package: forecast
## Warning: package 'forecast' was built under R version 3.5.2
## Loading required package: fma
## Warning: package 'fma' was built under R version 3.5.2
## Loading required package: expsmooth
## Warning: package 'expsmooth' was built under R version 3.5.2
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.5.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.5.2
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: tseries
## Warning: package 'tseries' was built under R version 3.5.2
library(forecast)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data <- read.csv(file="C:/Users/tuyan/Desktop/all_SPstocks_5yr.csv",header=TRUE, sep = ",", stringsAsFactors = TRUE)
View(data)
data %>% mutate(holding=(open+high+low+close)/4*volume)%>%
    group_by(Name) %>%
    summarise(total_holding=sum(holding,na.rm = TRUE)) %>%
    arrange(-total_holding) ->df
df %>% slice(1:10)
## # A tibble: 10 x 2
##    Name  total_holding
##    <fct>         <dbl>
##  1 AAPL        6.62e12
##  2 FB          3.44e12
##  3 AMZN        2.76e12
##  4 MSFT        2.04e12
##  5 BAC         2.02e12
##  6 GOOGL       1.95e12
##  7 NFLX        1.58e12
##  8 GE          1.38e12
##  9 JPM         1.38e12
## 10 C           1.36e12
data_mean<-data.frame(ID=data[,1], Mean=rowMeans(data[, 2:5]))
data_pd<- data.frame(date=data[,1], Name=data[,"Name"])
data_pd$mean<- data_mean$Mean
AAPL<- data_pd[data_pd[,"Name"] == "AAPL",]
FB<- data_pd[data_pd[,"Name"] == "FB",]
AMZN<- data_pd[data_pd[,"Name"] == "AMZN",]
MSFT<- data_pd[data_pd[,"Name"] == "MSFT",]
BAC<- data_pd[data_pd[,"Name"] == "BAC",]
GOOGL<- data_pd[data_pd[,"Name"] == "GOOGL",]
NFLX<- data_pd[data_pd[,"Name"] == "NFLX",]
GE<- data_pd[data_pd[,"Name"] == "GE",]
JPM<- data_pd[data_pd[,"Name"] == "JPM",]
C<- data_pd[data_pd[,"Name"] == "C",]
 ts <- ts(AAPL$mean, start = c(2013,2), frequency = 240)
 autoplot(ts) +
   xlab('date') +
   ylab('mean')

Moving average

autoplot(ts,series="mean")+
  autolayer(ma(ts,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

Estimating the Trend-Cycle with Seasonal Data

autoplot(ts, series="Data") +
  autolayer(ma(ts, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.

ts%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.

# Forcasting Methods
tss<-snaive(ts, 120)
autoplot(ts)+
  autolayer(tss,series="snavie")

tsholt<- holt(ts,120)
autoplot(ts)+
  autolayer(tsholt,series="holt's linear trend method")

tsdamped<- holt(ts,damped=TRUE,120) 
autoplot(tsdamped)+
  autolayer(tsdamped$fitted)

tsets<- ets(ts)
## Warning in ets(ts): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts)+
  autolayer(forecast(tsets,120))

ts1 <- ts(FB$mean, start = c(2013,2), frequency = 240)
 autoplot(ts1) +
   xlab('date') +
   ylab('mean')

autoplot(ts1,series="mean")+
  autolayer(ma(ts1,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts1, series="Data") +
  autolayer(ma(ts1, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not #only monthly and quarterly data.

ts1%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

#The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.

tss1<-snaive(ts1, 120)
autoplot(ts1)+
  autolayer(tss1,series="snavie")

tsn1<-naive(ts1, 120)
autoplot(ts1)+
  autolayer(tsn1,series="navie")

tsholt1<- holt(ts1,120)
autoplot(ts1)+
  autolayer(tsholt1)

tsdamped1<- holt(ts1,damped=TRUE,120) 
autoplot(tsdamped1)+
  autolayer(tsdamped1$fitted)

tsets1<- ets(ts1)
## Warning in ets(ts1): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts1)+
  autolayer(forecast(tsets1,120))

ts2 <- ts(AMZN$mean, start = c(2013,2), frequency = 240)
 autoplot(ts2) +
   xlab('date') +
   ylab('mean')

autoplot(ts2,series="mean")+
  autolayer(ma(ts2,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts2, series="Data") +
  autolayer(ma(ts2, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts2%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss2<-snaive(ts2, 120)
autoplot(ts2)+
  autolayer(tss2,series="snavie")

tsn2<-naive(ts2, 120)
autoplot(ts2)+
  autolayer(tsn2,series="navie")

tsholt2<- holt(ts2,120)
autoplot(ts2)+
  autolayer(tsholt2)

tsdamped2<- holt(ts2,damped=TRUE,120) 
autoplot(tsdamped2)+
  autolayer(tsdamped2$fitted)

ts3 <- ts(MSFT$mean, start = c(2013,2), frequency = 240)
 autoplot(ts3) +
   xlab('date') +
   ylab('mean')

 autoplot(ts3,series="mean")+
  autolayer(ma(ts3,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts3, series="Data") +
  autolayer(ma(ts3, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts3%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss3<-snaive(ts3, 120)
autoplot(ts3)+
  autolayer(tss3,series="snavie")

tsn3<-naive(ts3, 120)
autoplot(ts3)+
  autolayer(tsn3,series="navie")

tsholt3<- holt(ts3,120)
autoplot(ts3)+
  autolayer(tsholt3)

tsdamped3<- holt(ts3,damped=TRUE,120) 
autoplot(tsdamped3)+
  autolayer(tsdamped3$fitted)

tsets3<- ets(ts3)
## Warning in ets(ts3): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts3)+
  autolayer(forecast(tsets3,120))

ts4 <- ts(BAC$mean, start = c(2013,2), frequency = 240)
 autoplot(ts4) +
   xlab('date') +
   ylab('mean')

autoplot(ts4,series="mean")+
  autolayer(ma(ts4,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts4, series="Data") +
  autolayer(ma(ts4, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts4%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss4<-snaive(ts4, 120)
autoplot(ts4)+
  autolayer(tss4,series="snavie")

tsn4<-naive(ts4, 120)
autoplot(ts4)+
  autolayer(tsn4,series="navie")

tsholt4<- holt(ts4,120)
autoplot(ts4)+
  autolayer(tsholt4)

tsdamped4<- holt(ts4,damped=TRUE,120) 
autoplot(tsdamped4)+
  autolayer(tsdamped4$fitted)

tsets4<- ets(ts4)
## Warning in ets(ts4): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts4)+
  autolayer(forecast(tsets4,120))

ts5 <- ts(GOOGL$mean, start = c(2013,2), frequency = 240)
 autoplot(ts5) +
   xlab('date') +
   ylab('mean')

 autoplot(ts5,series="mean")+
  autolayer(ma(ts5,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts5, series="Data") +
  autolayer(ma(ts5, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts5%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss5<-snaive(ts5, 120)
autoplot(ts5)+
  autolayer(tss5,series="snavie")

tsn5<-naive(ts5, 120)
autoplot(ts5)+
  autolayer(tsn5,series="navie")

tsholt5<- holt(ts5,120)
autoplot(ts5)+
  autolayer(tsholt5)

tsdamped5<- holt(ts5,damped=TRUE,120) 
autoplot(tsdamped5)+
  autolayer(tsdamped5$fitted)

tsets5<- ets(ts5)
## Warning in ets(ts5): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts5)+
  autolayer(forecast(tsets5,120))

ts6 <- ts(NFLX$mean, start = c(2013,2), frequency = 240)
 autoplot(ts6) +
   xlab('date') +
   ylab('mean')

autoplot(ts6,series="mean")+
  autolayer(ma(ts6,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts6, series="Data") +
  autolayer(ma(ts6, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts6%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss6<-snaive(ts6, 120)
autoplot(ts6)+
  autolayer(tss6,series="snavie")

tsn6<-naive(ts6, 120)
autoplot(ts6)+
  autolayer(tsn6,series="navie")

tsholt6<- holt(ts6,120)
autoplot(ts6)+
  autolayer(tsholt6,serise="Holt")
## Warning: Ignoring unknown parameters: serise

tsdamped6<- holt(ts6,damped=TRUE,120) 
autoplot(tsdamped6)+
  autolayer(tsdamped6$fitted)

tsets6<- ets(ts6)
## Warning in ets(ts6): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts6)+
  autolayer(forecast(tsets6,120))

ts7 <- ts(GE$mean, start = c(2013,2), frequency = 240)
 autoplot(ts7) +
   xlab('date') +
   ylab('mean')

 autoplot(ts7,series="mean")+
  autolayer(ma(ts7,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts7, series="Data") +
  autolayer(ma(ts7, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts7%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss7<-snaive(ts7, 120)
autoplot(ts7)+
  autolayer(tss7,series="snavie")

tsn7<-naive(ts7, 120)
autoplot(ts7)+
  autolayer(tsn7,series="navie")

tsholt7<- holt(ts7,120)
autoplot(ts7)+
  autolayer(tsholt7,serise="Holt")
## Warning: Ignoring unknown parameters: serise

tsdamped7<- holt(ts7,damped=TRUE,120) 
autoplot(tsdamped7)+
  autolayer(tsdamped7$fitted)

tsets7<- ets(ts7)
## Warning in ets(ts7): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts7)+
  autolayer(forecast(tsets7,120))

ts8 <- ts(JPM$mean, start = c(2013,2), frequency = 240)
 autoplot(ts8) +
   xlab('date') +
   ylab('mean')

 autoplot(ts8,series="mean")+
  autolayer(ma(ts8,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts8, series="Data") +
  autolayer(ma(ts8, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts8%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss8<-snaive(ts8, 120)
autoplot(ts8)+
  autolayer(tss8,series="snavie")

tsn8<-naive(ts8, 120)
autoplot(ts8)+
  autolayer(tsn8,series="navie")

tsholt8<- holt(ts8,120)
autoplot(ts8)+
  autolayer(tsholt8,serise="Holt")
## Warning: Ignoring unknown parameters: serise

tsdamped8<- holt(ts8,damped=TRUE,120) 
autoplot(tsdamped8)+
  autolayer(tsdamped8$fitted)

tsets8<- ets(ts8)
## Warning in ets(ts8): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts8)+
  autolayer(forecast(tsets8,120))

ts9 <- ts(C$mean, start = c(2013,2), frequency = 240)
 autoplot(ts9) +
   xlab('date') +
   ylab('mean')

 autoplot(ts9,series="mean")+
  autolayer(ma(ts9,5), series="5-MA")
## Warning: Removed 4 rows containing missing values (geom_path).

autoplot(ts9, series="Data") +
  autolayer(ma(ts9, 12), series="12-MA")
## Warning: Removed 12 rows containing missing values (geom_path).

#There is a sesonality.I think it is best to use STL decomposingbecause STL will handle any type of seasonality, not only monthly and quarterly data.


ts9%>%
  stl(t.window=13, s.window="periodic", robust=TRUE) %>%
  autoplot()

##The data has a yearly cycle. There is no patterns in the remainder that means STL is good enough for this data.


tss9<-snaive(ts9, 120)
autoplot(ts9)+
  autolayer(tss9,series="snavie")

tsn9<-naive(ts9, 120)
autoplot(ts9)+
  autolayer(tsn9,series="navie")

tsholt9<- holt(ts9,120)
autoplot(ts9)+
  autolayer(tsholt9,serise="Holt")
## Warning: Ignoring unknown parameters: serise

tsdamped9<- holt(ts9,damped=TRUE,120) 
autoplot(tsdamped9)+
  autolayer(tsdamped9$fitted)

tsets9<- ets(ts9)
## Warning in ets(ts9): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(ts9)+
  autolayer(forecast(tsets9,120))