For this week, I worked with the monthly data for the stock Jets (Personal Interest). This data was collected from Yahoo Finance: https://finance.yahoo.com/quote/JETS/history?p=JETS
Loading Data
library(readr)
JETS <- read_csv("Desktop/JETS.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()
## )
Understanding data
str(JETS)
## tibble [36 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Date : Date[1:36], format: "2017-08-01" "2017-09-01" ...
## $ Open : num [1:36] 30.2 28.8 29.8 29.6 31.9 ...
## $ High : num [1:36] 30.7 29.8 31.7 32 33.5 ...
## $ Low : num [1:36] 28 27.7 29.4 28.6 31.1 ...
## $ Close : num [1:36] 28.7 29.6 29.4 31.9 32.6 ...
## $ Adj Close: num [1:36] 27.7 28.6 28.4 30.8 31.5 ...
## $ Volume : num [1:36] 804400 1166100 1149900 855900 870500 ...
## - attr(*, "spec")=
## .. cols(
## .. Date = col_date(format = ""),
## .. Open = col_double(),
## .. High = col_double(),
## .. Low = col_double(),
## .. Close = col_double(),
## .. `Adj Close` = col_double(),
## .. Volume = col_double()
## .. )
Creating Time Series
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggplot2)
Jets_ts <- ts(data=JETS[,6],frequency = 12,start =c(2017,8))
Jets_ts
## Jan Feb Mar Apr May Jun Jul Aug
## 2017 27.73092
## 2018 32.32773 31.77797 31.60127 29.94217 29.76547 28.36456 30.75700 31.84669
## 2019 30.36752 30.46630 28.83136 30.94049 27.53229 29.86369 30.10079 28.37199
## 2020 29.84000 23.90000 14.73000 15.19000 15.09000 16.65000 16.30000
## Sep Oct Nov Dec
## 2017 28.64723 28.42685 30.80462 31.47156
## 2018 31.98413 28.94083 32.32773 27.37009
## 2019 28.99436 30.62437 31.53322 31.12028
## 2020
Focusing on the monthly adjusted closing prices from 08,2017 to present
Plot of Jets_ts
autoplot(Jets_ts)+ggtitle("Monthly Adjusted Closing Prices")+ xlab("Year")+ylab("Monthly Adjusted Closing Prices")
*Additive Decompostion
Jets_ad <-decompose(Jets_ts, type="additive")
plot(Jets_ad)
Multiplicative Decomposition
Jets_md <- decompose(Jets_ts, type = "multiplicative")
plot(Jets_md)
##Determining the best method Based on my results above, both methods seem to produce very similar graphs.In order to make the right assumption about what method works best for my chosen dataset, I believe conducting further analysis might help in determing the best method.
library(dplyr)
##
## 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
bestmethod <- function(fit, ts.obj){error<-as.vector(
na.omit(
fit$random
)
)
datavector<-ts.obj[1]
abserror<-abs(error)
sqerror<-error^2
pererror<-(abserror/datavector)*100
me <- round(mean(error),2)
mae <- round(mean(abserror),2)
mse <- round(mean(sqerror),2)
rmse <- round(sqrt(mse),2)
mape <- round(mean(pererror),2)
return(data.frame(
values = c(me, mae, mse, rmse, mape),
metrics = c('Mean Error','Mean Absolute Error','Mean Squared Error',
'Root Mean Squared Error','Mean Absolute Percent Error')
)
)
}
Unfortunately I have run into some issues with producing a table of metrics to determine which method works best. However, from looking at the graph produced above, I believe the Multiplicative method works best for my chosen dataset in my opinion.