library(fpp3)
library(readxl)
# install and load any package necessary
tesla_stock <- read_excel("C:\\Users\\Zacha\\OneDrive\\Documents\\Tesla Stock.xls")
tesla_stock
## # A tibble: 253 x 2
## Date Close
## <dttm> <dbl>
## 1 2021-11-16 00:00:00 352.
## 2 2021-11-17 00:00:00 363.
## 3 2021-11-18 00:00:00 365.
## 4 2021-11-19 00:00:00 379.
## 5 2021-11-22 00:00:00 386.
## 6 2021-11-23 00:00:00 370.
## 7 2021-11-24 00:00:00 372
## 8 2021-11-26 00:00:00 361.
## 9 2021-11-29 00:00:00 379.
## 10 2021-11-30 00:00:00 382.
## # ... with 243 more rows
## # i Use `print(n = ...)` to see more rows
tesla.ts <- tesla_stock %>%
mutate(row = row_number()) %>%
as_tsibble(index = row) %>%
slice(210:253)
autoplot(tesla.ts)
## Plot variable not specified, automatically selected `.vars = Close`
#There's a clear downward trend to the data. Let's try taking the difference.
tesla.diff <- tesla.ts %>%
mutate(Close = difference(Close))
tesla.diff %>%
autoplot()
## Plot variable not specified, automatically selected `.vars = Close`
## Warning: Removed 1 row(s) containing missing values (geom_path).
#That looks a lot better. Let's check for white-noise.
tesla.ts %>%
features(Close,ljung_box, lag = 10)
## # A tibble: 1 x 2
## lb_stat lb_pvalue
## <dbl> <dbl>
## 1 153. 0
#ljung_box of 0 means our residuals are indistinguishable from white noise.
tesla.ts %>%
ACF(difference(Close)) %>%
autoplot()
#No autocorrelation.
tesla.ts %>%
PACF(difference(Close)) %>%
autoplot()
#No partial autocorrelation either.
#So for my ARIMA I'm looking at a (0,1,0)
tesla.fit <- tesla.ts %>%
model(ARIMA(Close~pdq(0,1,0)))
tesla.fc <- tesla.fit %>%
forecast(h=1)
tesla.fc
## # A fable: 1 x 4 [1]
## # Key: .model [1]
## .model row Close .mean
## <chr> <dbl> <dist> <dbl>
## 1 ARIMA(Close ~ pdq(0, 1, 0)) 254 N(184, 78) 184.
hilo(tesla.fc)
## # A tsibble: 1 x 6 [1]
## # Key: .model [1]
## .model row Close .mean `80%` `95%`
## <chr> <dbl> <dist> <dbl> <hilo> <hilo>
## 1 ARIMA(Cl~ 254 N(184, 78) 184. [172.8965, 195.5282]80 [166.9062, 201.5184]95
gg_tsresiduals(tesla.fit)
#Residuals look pretty good. I'm happy with this forecast. Now for the fun part.
#I might let the NAIVE model decide my destiny.
tesla.naive <- tesla.ts %>%
model('Simple' = ETS(Close ~ error("A") + trend("N") + season("N")))
tesla.naive %>%
forecast(h=1) %>%
autoplot(tesla.ts)
tesla.naive %>%
forecast(h=1) %>%
hilo()
## # A tsibble: 1 x 6 [1]
## # Key: .model [1]
## .model row Close .mean `80%` `95%`
## <chr> <dbl> <dist> <dbl> <hilo> <hilo>
## 1 Simple 254 N(187, 85) 187. [175.4789, 199.1269]80 [169.2196, 205.3861]95
#An official prediction of 187 from the NAIVE model.
THE GUESS: My ARIMA model predicted 184 as the closing price tomorrow and the NIAVE predicted 187. Everyone seems to think it’s going to decrease so I am going to just take a shot in the dark here and say $191 with a prediction interval of 169.2196 - 205.3861 which is from my ARIMA model.