This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
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
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.2
library(forecast)
## Warning: package 'forecast' was built under R version 3.5.2
library(seasonal)
## Warning: package 'seasonal' was built under R version 3.5.3
Assumptions
#stock price was the average of high, low, open, low
#holdings= average stock price * volume
#trade days per year: 253
#read the data into R
mydata<-read.csv(file="C:/Users/tuyan/Desktop/all_SPstocks_5yr.csv",header = TRUE,sep = ",",stringsAsFactors = TRUE)
View(mydata)
#Data cleaning
mydata[is.na(mydata)]<-0
mydata$date<-as.Date(mydata$date,format="%Y-%m-%d")
summary(mydata)
## date open high
## Min. :2013-02-08 Min. : 0.00 Min. : 0.00
## 1st Qu.:2014-05-20 1st Qu.: 40.22 1st Qu.: 40.62
## Median :2015-08-21 Median : 62.59 Median : 63.15
## Mean :2015-08-18 Mean : 83.02 Mean : 83.78
## 3rd Qu.:2016-11-15 3rd Qu.: 94.37 3rd Qu.: 95.18
## Max. :2018-02-07 Max. :2044.00 Max. :2067.99
##
## low close volume Name
## Min. : 0.00 Min. : 1.59 Min. : 0 A : 1259
## 1st Qu.: 39.83 1st Qu.: 40.24 1st Qu.: 1070320 AAL : 1259
## Median : 62.02 Median : 62.62 Median : 2082094 AAP : 1259
## Mean : 82.25 Mean : 83.04 Mean : 4321823 AAPL : 1259
## 3rd Qu.: 93.54 3rd Qu.: 94.41 3rd Qu.: 4284509 ABBV : 1259
## Max. :2035.11 Max. :2049.00 Max. :618237630 ABC : 1259
## (Other):611486
#identify the top 10 holdings by total assets
mydata %>% mutate(holdings=(open+high+low+close)/4*volume)%>%
group_by(Name) %>%
summarise(total_holdings=sum(holdings,na.rm = TRUE)) %>%
arrange(-total_holdings) ->df
df%>% slice(1:10)
## # A tibble: 10 x 2
## Name total_holdings
## <fct> <dbl>
## 1 AAPL 6624518918089.
## 2 FB 3441956216402.
## 3 AMZN 2760130275685.
## 4 MSFT 2042906775393.
## 5 BAC 2022860988742.
## 6 GOOGL 1951658047717.
## 7 NFLX 1582072569400.
## 8 GE 1377947689055.
## 9 JPM 1376246917801.
## 10 C 1362304798301.
mydata2<-data.frame(date=mydata[,1],Name=mydata[,7],Mean=rowMeans(mydata[,2:5]),volume=mydata[,6])
mydata2$holdings<-mydata2$Mean*mydata$volume
# Conduct an exploratory data analysis on the top 10 holdings data and comment on interesting findings and abnormal events.
top10<-subset(mydata2, Name==c("AAPL","FB","AMZN","MSFT","BAC","GOOGL","NFLX","GE","JPM","C"),select = Mean, index="date")
ts_top10<-ts(top10,start=c(2013,2),frequency=253)
autoplot(ts_top10)
# it seems that there are some yearly and seasonal patterns for the top 10 holdings’data. However, the trend is not consistent. I didn’t see any meaningful relatioship between these holdings’data.
View(mydata2)
AAPL<-subset(mydata2, Name=="AAPL",select = Mean, index="date")
FB<-subset(mydata2, Name=="FB",select = Mean, index="date")
AMZN<-subset(mydata2, Name=="AMZN",select = Mean, index="date")
MSFT<-subset(mydata2, Name=="MSFT",select = Mean, index="date")
BAC<-subset(mydata2, Name=="BAC",select = Mean, index="date")
GOOGL<-subset(mydata2, Name=="GOOGL",select = Mean, index="date")
NFLX<-subset(mydata2, Name=="NFLX",select = Mean, index="date")
GE<-subset(mydata2, Name=="GE",select = Mean, index="date")
JPM<-subset(mydata2, Name=="JPM",select = Mean, index="date")
C<-subset(mydata2, Name=="C",select = Mean, index="date")
#Define the ts objects for the top 10 holdings and analyze each time series. Comment on your results.
#AAPL Time Series
ts_AAPL<-ts(AAPL,start=c(2013,2),frequency=253)
autoplot(ts_AAPL)+ggtitle("AAPL Time Series")+ylab("Avgvalue")+xlab("Time")
#FB Time Series
ts_FB<-ts(FB,start=c(2013,2),frequency=253)
autoplot(ts_FB)+ggtitle("FB Time Series")+ylab("Avgvalue")+xlab("Time")
#AMZN Time Series
ts_AMZN<-ts(AMZN,start=c(2013,2),frequency=253)
autoplot(ts_AMZN)+ggtitle("AMZN Time Series")+ylab("Avgvalue")+xlab("Time")
#MSFT Time Series
ts_MSFT<-ts(MSFT,start=c(2013,2),frequency=253)
autoplot(ts_MSFT)+ggtitle("MSFT Time Series")+ylab("Avgvalue")+xlab("Time")
#BAC Time Series
ts_BAC<-ts(BAC,start=c(2013,2),frequency=253)
autoplot(ts_BAC)+ggtitle("BAC Time Series")+ylab("Avgvalue")+xlab("Time")
#GOOGL Time Series
ts_GOOGL<-ts(GOOGL,start=c(2013,2),frequency=253)
autoplot(ts_GOOGL)+ggtitle("GOOGL Time Series")+ylab("Avgvalue")+xlab("Time")
#NFLX Time Series
ts_NFLX<-ts(NFLX,start=c(2013,2),frequency=253)
autoplot(ts_NFLX)+ggtitle("NFLX Time Series")+ylab("Avgvalue")+xlab("Time")
#GE Time Series
ts_GE<-ts(GE,start=c(2013,2),frequency=253)
autoplot(ts_GE)+ggtitle("GE Time Series")+ylab("Avgvalue")+xlab("Time")
#JPM Time Series
ts_JPM<-ts(JPM,start=c(2013,2),frequency=253)
autoplot(ts_JPM)+ggtitle("JPM Time Series")+ylab("Avgvalue")+xlab("Time")
#C Time Series
ts_C<-ts(C,start=c(2013,2),frequency=253)
autoplot(ts_C)+ggtitle("C Time Series")+ylab("Avgvalue")+xlab("Time")
#AAPL: overall, there is an upward trend with seasonality of AAPL. There are some seasonal patterns but not very consistent. A major decrease happened between 2015 and 2017.
#FB: overall, there is an upward trend with seasonality of FB. The seasonality pattern is not very clear and there some minor decrease from 2013 to 2018.
#AMZN: overall, there is an upward trend with seasonality of AMZN.The seasonality pattern is not very clear and consistent.
#MSFT: overall, there is an upward trend with seasonality of MSFT. The seasonality patterns aggregate from 2015 to 2016.
#BAC: overall, there is an upward trend with seasonality of BAC. THe seasonality pattern is relatively consistent. A major decrease happened between 2016 and 2017.
#GOOGL: overall, there is an upward trend with seasonality of GOOGL. There are some seasonal pattern and it is pretty consistent.
#NFLX: overall, there is an upward trend with seasonality of NFLX. The seasonality pattern is not very clear and consitent. An obvious increase happened in 2015.
#GE: The yearly pattern is not very clear. There is an upward trend from 2013 to 2017 and experienced a steep decrease from 2017 to 2018. There are some seasonal pattern.
#JPM: overall, there is an upward trend with seasonality of JPM. There are some seasonal pattern each year.
#C:overall, there is an upward trend with seasonality of C. The yearly pattern is not very consistent. A major decrease happened between 2015 and 2017.
#Moving Average
autoplot(ts_AAPL,series = "Holdings")+
autolayer(ma(ts_AAPL,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("AAPL Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_AAPL,series = "Holdings")+
autolayer(ma(ts_AAPL,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("AAPL Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose AAPL
#Classical Decomposition
ts_AAPL %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of AAPL")
#STL Decomposition
ts_AAPL1<-ts(ts_AAPL,start=c(2013,2), end=c(2018,2),frequency=253)
ts_AAPL1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of AAPL")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data.
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion is better than classical decomposition because it decomposes trend and seasonal pattern more clearly.
#Moving Average
autoplot(ts_FB,series = "Holdings")+
autolayer(ma(ts_FB,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("FB Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_FB,series = "Holdings")+
autolayer(ma(ts_FB,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("FB Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose
#Classical Decomposition
ts_FB %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of FB")
#STL Decomposition
ts_FB1<-ts(ts_FB,start=c(2013,2), end=c(2018,2),frequency=253)
ts_FB1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of FB")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data.
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion does not show seasonal pattern and trend very well.
#Moving Average
autoplot(ts_AMZN,series = "Holdings")+
autolayer(ma(ts_AMZN,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("AMZN Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_AMZN,series = "Holdings")+
autolayer(ma(ts_AMZN,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("AMZN Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose AMZN
#Classical Decomposition
ts_AMZN %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of AMZN")
#STL Decomposition
ts_AMZN1<-ts(ts_AMZN,start=c(2013,2), end=c(2018,2),frequency=253)
ts_AMZN1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of AMZN")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data.
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion does not show seasonal pattern and trend very well.
#Moving Average
autoplot(ts_MSFT,series = "Holdings")+
autolayer(ma(ts_MSFT,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("MSFT Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_MSFT,series = "Holdings")+
autolayer(ma(ts_MSFT,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("MSFT Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose MSFT
#Classical Decomposition
ts_MSFT %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of MSFT")
#STL Decomposition
ts_MSFT1<-ts(ts_MSFT,start=c(2013,2), end=c(2018,2),frequency=253)
ts_MSFT1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of MSFT")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion does not show seasonal pattern and trend very well.
#Moving Average
autoplot(ts_BAC,series = "Holdings")+
autolayer(ma(ts_BAC,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("BAC Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_BAC,series = "Holdings")+
autolayer(ma(ts_BAC,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("BAC Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose BAC
#Classical Decomposition
ts_BAC %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of BAC")
#STL Decomposition
ts_BAC1<-ts(ts_BAC,start=c(2013,2), end=c(2018,2),frequency=253)
ts_BAC1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of BAC")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion does shows seasonal pattern and trend very well.
#Moving Average
autoplot(ts_GOOGL,series = "Holdings")+
autolayer(ma(ts_GOOGL,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("GOOGL Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_GOOGL,series = "Holdings")+
autolayer(ma(ts_GOOGL,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("GOOGL Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose GOOGL
#Classical Decomposition
ts_GOOGL %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of GOOGL")
#STL Decomposition
ts_GOOGL1<-ts(ts_GOOGL,start=c(2013,2), end=c(2018,2),frequency=253)
ts_GOOGL1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of GOOGL")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion shows seasonal pattern and trend very well.
#Moving Average
autoplot(ts_NFLX,series = "Holdings")+
autolayer(ma(ts_NFLX,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("NFLX Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_NFLX,series = "Holdings")+
autolayer(ma(ts_NFLX,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("NFLX Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose NFLX
#Classical Decomposition
ts_NFLX %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of NFLX")
#STL Decomposition
ts_NFLX1<-ts(ts_NFLX,start=c(2013,2), end=c(2018,2),frequency=253)
ts_NFLX1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of NFLX")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion does shows seasonal pattern and trend very well.
#Moving Average
autoplot(ts_GE,series = "Holdings")+
autolayer(ma(ts_GE,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("GE Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_GE,series = "Holdings")+
autolayer(ma(ts_GE,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("GE Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose GE
#Classical Decomposition
ts_GE %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of GE")
#STL Decomposition
ts_GE1<-ts(ts_GE,start=c(2013,2), end=c(2018,2),frequency=253)
ts_GE1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of GE")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion shows seasonal pattern and trend very well.
#Moving Average
autoplot(ts_JPM,series = "Holdings")+
autolayer(ma(ts_JPM,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("JPM Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_JPM,series = "Holdings")+
autolayer(ma(ts_JPM,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("JPM Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose JPM
#Classical Decomposition
ts_JPM %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of JPM")
#STL Decomposition
ts_JPM1<-ts(ts_JPM,start=c(2013,2), end=c(2018,2),frequency=253)
ts_JPM1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of JPM")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion shows seasonal pattern and trend very well.
#Moving Average
autoplot(ts_C,series = "Holdings")+
autolayer(ma(ts_C,5),series="5-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("C Moving Average")
## Warning: Removed 4 rows containing missing values (geom_path).
#Estimate the Trend-Cycle with Seasonal Data
autoplot(ts_C,series = "Holdings")+
autolayer(ma(ts_C,12),series="12-MA")+
xlab("Time")+ylab("holdings")+
ggtitle("C Trend-Cycle with Seasonal Data")
## Warning: Removed 12 rows containing missing values (geom_path).
#Decompose C
#Classical Decomposition
ts_C %>% decompose(type = "multiplicative") %>%
autoplot()+xlab("Time")+ ggtitle("Classical multiplicative decomposition of C")
#STL Decomposition
ts_C1<-ts(ts_C,start=c(2013,2), end=c(2018,2),frequency=253)
ts_C1 %>% stl(t.window = 13,s.window = "periodic", robust = TRUE) %>%
autoplot()+ ggtitle("STL decomposition of C")
#since stock index is daily-based data, X11 and SEATS are not able to decompose the data
#The classical decomposition shows clear seasonal pattern but the trend is very poor.
#STL decompostion shows seasonal pattern and trend very well.
#AAPL Seasonal Naive Method
naive_AAPL<-snaive(ts_AAPL,120)
autoplot(naive_AAPL) + autolayer(naive_AAPL,series = "snaive")
#Simple exponential smoothing
ses_AAPL<-ses(ts_AAPL,h=120)
autoplot(ses_AAPL)+
autolayer(fitted(ses_AAPL),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_AAPL<- stlf(ts_AAPL,h=120,lambda = BoxCox.lambda(ts_AAPL))
autoplot(stlf_AAPL)+
autolayer(stlf_AAPL$fitted)
#Holt's linear trend method
holt_AAPL<-holt(ts_AAPL,h=120)
autoplot(holt_AAPL)+
autolayer(holt_AAPL,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_AAPL<-holt(ts_AAPL,damped = TRUE,h=120)
autoplot(damp_AAPL)+
autolayer(damp_AAPL$fitted)
#ETS method
ets_AAPL<-ets(ts_AAPL)
## Warning in ets(ts_AAPL): I can't handle data with frequency greater than
## 24. Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_AAPL,h=120))
#FB Seasonal Naive Method
naive_FB<-snaive(ts_FB,120)
autoplot(naive_FB) + autolayer(naive_FB,series = "snaive")
ses_FB<-ses(ts_FB,h=120)
autoplot(ses_FB)+
autolayer(fitted(ses_FB),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_FB<- stlf(ts_FB,h=120,lambda = BoxCox.lambda(ts_FB))
autoplot(stlf_FB)+
autolayer(stlf_FB$fitted)
#Holt's linear trend method
holt_FB<-holt(ts_FB,h=120)
autoplot(holt_FB)+
autolayer(holt_FB,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_FB<-holt(ts_FB,damped = TRUE,h=120)
autoplot(damp_FB)+
autolayer(damp_FB$fitted)
#ETS method
ets_FB<-ets(ts_FB)
## Warning in ets(ts_FB): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_FB,h=120))
#AMZN Seasonal Naive Method
naive_AMZN<-snaive(ts_AMZN,120)
autoplot(naive_AMZN) + autolayer(naive_AMZN,series = "snaive")
#Simple exponential smoothing
ses_AMZN<-ses(ts_AMZN,h=120)
autoplot(ses_AMZN)+
autolayer(fitted(ses_AMZN),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_AMZN<- stlf(ts_AMZN,h=120,lambda = BoxCox.lambda(ts_AMZN))
autoplot(stlf_AMZN)+
autolayer(stlf_AMZN$fitted)
#Holt's linear trend method
holt_AMZN<-holt(ts_AMZN,h=120)
autoplot(holt_AMZN)+
autolayer(holt_AMZN,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_AMZN<-holt(ts_AMZN,damped = TRUE,h=120)
autoplot(damp_AMZN)+
autolayer(damp_AMZN$fitted)
#ETS method
ets_AMZN<-ets(ts_AMZN)
## Warning in ets(ts_AMZN): I can't handle data with frequency greater than
## 24. Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_AMZN,h=120))
#MSFT Seasonal Naive Method
naive_MSFT<-snaive(ts_MSFT,120)
autoplot(naive_MSFT) + autolayer(naive_MSFT,series = "snaive")
#Simple exponential smoothing
ses_MSFT<-ses(ts_MSFT,h=120)
autoplot(ses_MSFT)+
autolayer(fitted(ses_MSFT),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_MSFT<- stlf(ts_MSFT,h=120,lambda = BoxCox.lambda(ts_MSFT))
autoplot(stlf_MSFT)+
autolayer(stlf_MSFT$fitted)
#Holt's linear trend method
holt_MSFT<-holt(ts_MSFT,h=120)
autoplot(holt_MSFT)+
autolayer(holt_MSFT,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_MSFT<-holt(ts_MSFT,damped = TRUE,h=120)
autoplot(damp_MSFT)+
autolayer(damp_MSFT$fitted)
#ETS method
ets_MSFT<-ets(ts_MSFT)
## Warning in ets(ts_MSFT): I can't handle data with frequency greater than
## 24. Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_MSFT,h=120))
#BAC Seasonal Naive Method
naive_BAC<-snaive(ts_BAC,120)
autoplot(naive_BAC) + autolayer(naive_BAC,series = "snaive")
#Simple exponential smoothing
ses_BAC<-ses(ts_AMZN,h=120)
autoplot(ses_BAC)+
autolayer(fitted(ses_BAC),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_BAC<- stlf(ts_BAC,h=120,lambda = BoxCox.lambda(ts_BAC))
autoplot(stlf_BAC)+
autolayer(stlf_BAC$fitted)
#Holt's linear trend method
holt_BAC<-holt(ts_BAC,h=120)
autoplot(holt_BAC)+
autolayer(holt_BAC,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_BAC<-holt(ts_BAC,damped = TRUE,h=120)
autoplot(damp_BAC)+
autolayer(damp_BAC$fitted)
#ETS method
ets_BAC<-ets(ts_BAC)
## Warning in ets(ts_BAC): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_BAC,h=120))
#GOOGL Seasonal Naive Method
naive_GOOGL<-snaive(ts_GOOGL,120)
autoplot(naive_GOOGL) + autolayer(naive_GOOGL,series = "snaive")
#Simple exponential smoothing
ses_GOOGL<-ses(ts_GOOGL,h=120)
autoplot(ses_GOOGL)+
autolayer(fitted(ses_GOOGL),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_GOOGL<- stlf(ts_GOOGL,h=120,lambda = BoxCox.lambda(ts_GOOGL))
autoplot(stlf_GOOGL)+
autolayer(stlf_GOOGL$fitted)
#Holt's linear trend method
holt_GOOGL<-holt(ts_GOOGL,h=120)
autoplot(holt_GOOGL)+
autolayer(holt_GOOGL,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_GOOGL<-holt(ts_GOOGL,damped = TRUE,h=120)
autoplot(damp_GOOGL)+
autolayer(damp_GOOGL$fitted)
#ETS method
ets_GOOGL<-ets(ts_GOOGL)
## Warning in ets(ts_GOOGL): I can't handle data with frequency greater than
## 24. Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_GOOGL,h=120))
#NFLX Seasonal Naive Method
naive_NFLX<-snaive(ts_NFLX,120)
autoplot(naive_NFLX) + autolayer(naive_NFLX,series = "snaive")
#Simple exponential smoothing
ses_NFLX<-ses(ts_NFLX,h=120)
autoplot(ses_NFLX)+
autolayer(fitted(ses_NFLX),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_NFLX<- stlf(ts_NFLX,h=120,lambda = BoxCox.lambda(ts_NFLX))
autoplot(stlf_NFLX)+
autolayer(stlf_NFLX$fitted)
#Holt's linear trend method
holt_NFLX<-holt(ts_NFLX,h=120)
autoplot(holt_NFLX)+
autolayer(holt_NFLX,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_NFLX<-holt(ts_NFLX,damped = TRUE,h=120)
autoplot(damp_NFLX)+
autolayer(damp_NFLX$fitted)
#ETS method
ets_NFLX<-ets(ts_NFLX)
## Warning in ets(ts_NFLX): I can't handle data with frequency greater than
## 24. Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_NFLX,h=120))
#GE Seasonal Naive Method
naive_GE<-snaive(ts_GE,120)
autoplot(naive_GE) + autolayer(naive_GE,series = "snaive")
#Simple exponential smoothing
ses_GE<-ses(ts_GE,h=120)
autoplot(ses_GE)+
autolayer(fitted(ses_GE),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_GE<- stlf(ts_GE,h=120,lambda = BoxCox.lambda(ts_GE))
autoplot(stlf_GE)+
autolayer(stlf_GE$fitted)
#Holt's linear trend method
holt_GE<-holt(ts_GE,h=120)
autoplot(holt_GE)+
autolayer(holt_GE,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_GE<-holt(ts_GE,damped = TRUE,h=120)
autoplot(damp_GE)+
autolayer(damp_GE$fitted)
#ETS method
ets_GE<-ets(ts_GE)
## Warning in ets(ts_GE): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_GE,h=120))
#JPM Seasonal Naive Method
naive_JPM<-snaive(ts_JPM,120)
autoplot(naive_JPM) + autolayer(naive_JPM,series = "snaive")
#Simple exponential smoothing
ses_JPM<-ses(ts_JPM,h=120)
autoplot(ses_JPM)+
autolayer(fitted(ses_JPM),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_JPM<- stlf(ts_JPM,h=120,lambda = BoxCox.lambda(ts_JPM))
autoplot(stlf_JPM)+
autolayer(stlf_JPM$fitted)
#Holt's linear trend method
holt_JPM<-holt(ts_JPM,h=120)
autoplot(holt_JPM)+
autolayer(holt_JPM,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_JPM<-holt(ts_JPM,damped = TRUE,h=120)
autoplot(damp_JPM)+
autolayer(damp_JPM$fitted)
#ETS method
ets_JPM<-ets(ts_JPM)
## Warning in ets(ts_JPM): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_JPM,h=120))
#C Seasonal Naive Method
naive_C<-snaive(ts_C,120)
autoplot(naive_C) + autolayer(naive_C,series = "snaive")
#Simple exponential smoothing
ses_C<-ses(ts_C,h=120)
autoplot(ses_C)+
autolayer(fitted(ses_C),series="Fitted")+
ylab("Average Stock Value")+ xlab("Time")
#STLF
stlf_C<- stlf(ts_C,h=120,lambda = BoxCox.lambda(ts_C))
autoplot(stlf_C)+
autolayer(stlf_C$fitted)
#Holt's linear trend method
holt_C<-holt(ts_C,h=120)
autoplot(holt_C)+
autolayer(holt_C,series="Holt's method",PI=FALSE)
#Damped Holt's Trend Method
damp_C<-holt(ts_C,damped = TRUE,h=120)
autoplot(damp_C)+
autolayer(damp_C$fitted)
#ETS method
ets_C<-ets(ts_C)
## Warning in ets(ts_C): I can't handle data with frequency greater than 24.
## Seasonality will be ignored. Try stlf() if you need seasonal forecasts.
autoplot(forecast(ets_C,h=120))
# AAPL comparative analysis
round(accuracy(naive_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 21.952 34.613 30.1916 16.2752 24.5709 1 0.9972
round(accuracy(ses_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0743 1.3429 0.909 0.0611 0.857 0.0301 0.1957
round(accuracy(stlf_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0581 1.1803 0.8273 0.0504 0.7894 0.0274 0.1144
round(accuracy(holt_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0041 1.3409 0.9027 -0.017 0.8521 0.0299 0.1957
round(accuracy(damp_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0533 1.3345 0.8938 0.0451 0.842 0.0296 0.123
round(accuracy(ets_AAPL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0548 1.3343 0.8933 0.0471 0.8413 0.0296 0.124
#FB comparative analysis
round(accuracy(naive_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 31.1111 33.2617 31.1111 29.7346 29.7346 1 0.9848
round(accuracy(ses_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.122 1.372 0.9059 0.1329 1.0569 0.0291 0.2004
round(accuracy(stlf_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0191 1.2849 0.8639 -0.0106 0.9706 0.0278 0.1958
round(accuracy(holt_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 9e-04 1.3666 0.8989 -0.0346 1.0519 0.0289 0.2005
round(accuracy(damp_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.1028 1.3712 0.9036 0.115 1.0544 0.029 0.1742
round(accuracy(ets_FB),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.001 1.3666 0.8989 -0.0343 1.0519 0.0289 0.2005
#AMZN comparative analysis
round(accuracy(naive_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 178.4567 224.7925 188.3245 23.9954 27.2204 1 0.9901
round(accuracy(ses_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.9319 9.0969 5.4768 0.1228 0.9868 0.0291 0.1441
round(accuracy(stlf_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.6864 8.6014 5.2368 0.0925 0.9265 0.0278 0.0871
round(accuracy(holt_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.2978 9.0384 5.4233 0.0256 0.9821 0.0288 0.1359
round(accuracy(damp_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.706 9.05 5.4282 0.0933 0.9805 0.0288 0.0835
round(accuracy(ets_AMZN),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.3952 9.0571 5.4396 0.0071 0.9825 0.0289 0.1441
#MSFT comparative analysis
round(accuracy(naive_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 10.3736 12.1618 10.5282 17.8445 18.202 1 0.9879
round(accuracy(ses_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0499 0.5894 0.3794 0.0872 0.7723 0.036 0.1792
round(accuracy(stlf_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0487 0.5452 0.3727 0.0849 0.737 0.0354 0.1231
round(accuracy(holt_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 8e-04 0.5877 0.3782 -0.0181 0.7714 0.0359 0.1785
round(accuracy(damp_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0389 0.5878 0.3776 0.0686 0.7698 0.0359 0.1323
round(accuracy(ets_MSFT),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 8e-04 0.5877 0.3782 -0.0181 0.7714 0.0359 0.1785
#BAC comparative analysis
round(accuracy(naive_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 2.915 5.3596 4.0119 11.5669 19.3832 1 0.9965
round(accuracy(ses_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.9319 9.0969 5.4768 0.1228 0.9868 0.0291 0.1441
round(accuracy(stlf_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0096 0.2074 0.1526 0.046 0.8895 0.038 0.1143
round(accuracy(holt_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0045 0.2351 0.1671 0.0114 0.9664 0.0416 0.2219
round(accuracy(damp_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0093 0.2337 0.1666 0.0428 0.963 0.0415 0.1075
round(accuracy(ets_BAC),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0105 0.2335 0.1662 0.0477 0.9608 0.0414 0.1416
#GOOGL comparative analysis
round(accuracy(naive_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 127.2788 156.4696 134.9752 16.2554 17.6854 1 0.9908
round(accuracy(ses_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.539 8.0198 5.1798 0.073 0.7686 0.0384 0.2595
round(accuracy(stlf_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.3456 7.0371 4.7826 0.0477 0.7091 0.0354 0.1564
round(accuracy(holt_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0175 8.0034 5.1614 -0.0148 0.7674 0.0382 0.26
round(accuracy(damp_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.3111 7.9362 5.1192 0.0437 0.7618 0.0379 0.1475
round(accuracy(ets_GOOGL),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.3223 7.9368 5.1189 0.0451 0.7617 0.0379 0.1562
#NFLX comparative analysis
round(accuracy(naive_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 34.3773 45.6635 36.5827 29.0547 31.6061 1 0.9895
round(accuracy(ses_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.1915 2.2705 1.2722 0.1574 1.4098 0.0348 0.2473
round(accuracy(stlf_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0855 2.1564 1.3052 0.0766 1.4015 0.0357 0.0154
round(accuracy(holt_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0666 2.2619 1.2701 0.0126 1.4128 0.0347 0.2429
round(accuracy(damp_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.1101 2.2264 1.2469 0.0946 1.3992 0.0341 0.1019
round(accuracy(ets_NFLX),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.1338 2.233 1.2508 0.1127 1.3965 0.0342 0.1583
#GE comparative analysis
round(accuracy(naive_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.041 4.6726 3.3704 -2.2226 13.9551 1 0.9919
round(accuracy(ses_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0057 0.2625 0.1779 -0.036 0.692 0.0528 0.2105
round(accuracy(stlf_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0037 0.2313 0.1636 -0.0227 0.6378 0.0485 0.1594
round(accuracy(holt_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0085 0.262 0.1779 -0.0399 0.6913 0.0528 0.2022
round(accuracy(damp_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0044 0.2609 0.1763 -0.0271 0.6852 0.0523 0.143
round(accuracy(ets_GE),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set -0.0045 0.2609 0.1763 -0.0271 0.6852 0.0523 0.1432
#JPM comparative analysis
round(accuracy(naive_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 10.4925 15.3085 11.3637 12.6736 14.0972 1 0.9944
round(accuracy(ses_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0509 0.7476 0.517 0.0606 0.7826 0.0455 0.1629
round(accuracy(stlf_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0514 0.6862 0.4973 0.0616 0.7389 0.0438 0.1694
round(accuracy(holt_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0319 0.7478 0.5183 0.0429 0.786 0.0456 0.16
round(accuracy(damp_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0392 0.7451 0.5155 0.0468 0.7807 0.0454 0.1111
round(accuracy(ets_JPM),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0509 0.7476 0.517 0.0606 0.7826 0.0455 0.1629
#C comparative analysis
round(accuracy(naive_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 4.6273 11.7403 9.0343 5.7545 15.755 1 0.9955
round(accuracy(ses_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0256 0.6583 0.4817 0.0361 0.928 0.0533 0.2048
round(accuracy(stlf_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.019 0.5726 0.436 0.0266 0.8385 0.0483 0.1276
round(accuracy(holt_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 5e-04 0.6578 0.4814 -0.0119 0.9277 0.0533 0.2048
round(accuracy(damp_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0179 0.6556 0.4807 0.0257 0.9264 0.0532 0.1366
round(accuracy(ets_C),4)
## ME RMSE MAE MPE MAPE MASE ACF1
## Training set 0.0179 0.6556 0.4807 0.0257 0.9264 0.0532 0.1366
#According to the comparative analysis, in general,stfl is best when we compare RMSE and MAE. Holt's method is better when compare MAE or MSE.
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.