Here, I load the libraries I will need to run the forecast.
library(readr)
library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
## ✔ tibble 3.1.8 ✔ tsibble 1.1.2
## ✔ dplyr 1.0.9 ✔ tsibbledata 0.4.0
## ✔ 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()
Here, I load the dataset
tesla <- readr::read_csv("C:\\Users\\PythonAcct\\Downloads\\TSLA (2).csv")
## Rows: 253 Columns: 7
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (6): Open, High, Low, Close, Adj Close, Volume
## date (1): Date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
I change the time series to row number since the trading days thing will mess up the tsibble. I select Close and n and then reformat to a tsibble.
newt <- tesla %>%
mutate(n=row_number()) %>%
select(Close,n) %>%
as_tsibble(index=n)
newt
## # A tsibble: 253 x 2 [1]
## Close n
## <dbl> <int>
## 1 352. 1
## 2 363. 2
## 3 365. 3
## 4 379. 4
## 5 386. 5
## 6 370. 6
## 7 372 7
## 8 361. 8
## 9 379. 9
## 10 382. 10
## # … with 243 more rows
## # ℹ Use `print(n = ...)` to see more rows
newt %>%
autoplot(Close)
As befits the stock market, the data is extremely random and hard to predict. It has a trend but resembles a random walk.
newt %>%
ACF(Close) %>%
autoplot()
This has SIGNIFICANT autocorrelation. It decays somewhat exponentially.
newt %>%
autoplot(difference(Close))
## Warning: Removed 1 row(s) containing missing values (geom_path).
The first difference looks stationary.
newt %>%
ACF(difference(Close)) %>%
autoplot()
By doing the first difference, the ACF is significantly amended.
newt %>%
gg_tsdisplay(Close, plot_type='partial')
The acf decays exponentially and the pcf has a significant spike at the first lag.
fitt <- (newt %>% filter(n>200)) %>%
model(arima010 = ARIMA(Close ~ pdq(0,1,0)),
arima110=ARIMA(Close~pdq(1,1,0)),
stepwise = ARIMA(Close),
search = ARIMA(Close, stepwise=FALSE),
naive=NAIVE(Close))
glance(fitt) %>% arrange(AICc) %>% select(.model:BIC)
## # A tibble: 5 × 6
## .model sigma2 log_lik AIC AICc BIC
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 arima010 79.2 -187. 377. 377. 379.
## 2 stepwise 79.2 -187. 377. 377. 379.
## 3 search 79.2 -187. 377. 377. 379.
## 4 arima110 80.4 -187. 379. 379. 383.
## 5 naive 78.1 NA NA NA NA
My specified, the two automatic, and the NAIVE all have the same lowest AICc.
fct <- fitt %>%
forecast(h=1)
fct %>%
filter(n==254)
## # A fable: 5 x 4 [1]
## # Key: .model [5]
## .model n Close .mean
## <chr> <dbl> <dist> <dbl>
## 1 arima010 254 N(187, 79) 187.
## 2 arima110 254 N(187, 80) 187.
## 3 stepwise 254 N(187, 79) 187.
## 4 search 254 N(187, 79) 187.
## 5 naive 254 N(187, 79) 187.
They also have the same value for the forecast.
fct%>%
autoplot(newt,level=NULL)
fitt <- (newt %>% filter(n>200)) %>%
model(arima010 = ARIMA(Close ~ pdq(0,1,0)))
fctt <- fitt %>%
forecast(h=1)
hilo(fctt) %>%
filter(n==254) %>%
select("95%")
## # A tsibble: 1 x 2 [1]
## `95%` n
## <hilo> <dbl>
## 1 [169.4784, 204.3616]95 254
My official point forecast is 186.92 and my prediction intervals are seen in the table above.