library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
## ✔ tibble 3.1.8 ✔ tsibble 1.1.2
## ✔ dplyr 1.0.9 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.2.0 ✔ feasts 0.2.2
## ✔ lubridate 1.8.0 ✔ fable 0.3.1
## ✔ ggplot2 3.3.6
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
library(readxl)
TSLA <- read_excel("C:/Users/ryanf/Downloads/TSLA.xlsx")
## New names:
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
tsla <- TSLA %>%
mutate(Day = row_number()) %>%
as_tsibble(index = Day) %>%
select(Day, Close)
tsla %>%
ggplot(aes(x = Day)) +
geom_line(aes(y = Close)) +
labs(title = "Tesla 1 year Stock Price", y = "Close Price", x = "trading day")
I downloaded 1 year data from yahoo finance and cleaned it up only to include closing price and then converted it to tsibble. I selected one year worth of data because Tesla stock was almost at all time highs a year ago and has since been in a consistent down-trend.
tsla_naive <- tsla %>%
model('Simple' = ETS(Close ~ error("A") + trend("N") + season("N")))
naive_fc <- tsla_naive %>%
forecast(h = 1)
naive_fc %>%
hilo(80)
## # A tsibble: 1 x 5 [1]
## # Key: .model [1]
## .model Day Close .mean `80%`
## <chr> <dbl> <dist> <dbl> <hilo>
## 1 Simple 253 N(187, 136) 187. [172.3989, 202.3115]80
naive_fc%>%
autoplot(tsla)
I chose the naive forecast because this method is usually ideal for stock prices. Naive forecasts use the most recent observation. Stock prices are pretty unpredictable which means the most recent price is usually a good proxy for a forecast 1 day in the future due to the seemingly random movements in price.
tsla_naive %>%
gg_tsresiduals()
augment(tsla_naive) %>%
features(.resid, ljung_box, lag = 10, dof = 0)
## # A tibble: 1 × 3
## .model lb_stat lb_pvalue
## <chr> <dbl> <dbl>
## 1 Simple 20.5 0.0249
I checked the residuals of my chosen method and everything looks good.
tsla_arima <- tsla %>%
model(arima210 = ARIMA(Close ~ pdq(2,1,0))) %>%
forecast(h = 1)
tsla_arima%>%
hilo(80)
## # A tsibble: 1 x 5 [1]
## # Key: .model [1]
## .model Day Close .mean `80%`
## <chr> <dbl> <dist> <dbl> <hilo>
## 1 arima210 253 N(187, 137) 187. [172.4047, 202.3757]80
tsla_arima%>%
autoplot(tsla)
I just tried arima for the hell of it and basically got the same values. My point forecast comes out to 187.39 and a 80% PI of [172.4 , 202.31]. However you said we can just guess the prices, so based nothing off my forecast, I think the stock will increase by 2.5% tomorrow and close at a price of 191.53.