Load packages and data

library(fpp3)
library(readxl)

Finding the best value

tlsa <- read_excel("~/Downloads/tlsa.xlsx")


stocky <- tlsa %>%
  mutate(Date = row_number()) %>%
  as_tsibble(index = Date) %>%
  select(Date, Close)


tlsa %>%
  ggplot(aes(x = Date)) +
  geom_line(aes(y = Close))

stocky %>% ACF(Close) %>% autoplot()

stocky %>%
  autoplot(difference(Close)) +
  labs(y = "Change in Tesla closing stock price ($USD)")
## Warning: Removed 1 row(s) containing missing values (geom_path).

stocky %>% ACF(difference(Close)) %>% autoplot()

stocky %>% PACF(difference(Close)) %>% autoplot()

stocky %>% 
  gg_tsdisplay(difference(Close), plot_type='partial')
## Warning: Removed 1 row(s) containing missing values (geom_path).
## Warning: Removed 1 rows containing missing values (geom_point).

fitagain <- stocky %>%
  model(arima010 = ARIMA(Close ~ pdq(0,1,0)),
        arima020 = ARIMA(Close ~ pdq(0,2,0)),
        stepwise = ARIMA(Close),
        search = ARIMA(Close, stepwise=FALSE))

glance(fitagain) %>% arrange(AICc) %>% select(.model:BIC)
## # A tibble: 4 × 6
##   .model   sigma2 log_lik   AIC  AICc   BIC
##   <chr>     <dbl>   <dbl> <dbl> <dbl> <dbl>
## 1 arima010   136.   -977. 1955. 1955. 1959.
## 2 stepwise   136.   -977. 1955. 1955. 1959.
## 3 search     136.   -977. 1955. 1955. 1959.
## 4 arima020   288.  -1067. 2136. 2136. 2140.
fitagain %>% select(arima010) %>% gg_tsresiduals()

augment(fitagain) %>%
  features(.innov, ljung_box, lag = 10, dof = 4)
## # A tibble: 4 × 3
##   .model   lb_stat lb_pvalue
##   <chr>      <dbl>     <dbl>
## 1 arima010    24.8  0.000365
## 2 arima020   140.   0       
## 3 search      24.8  0.000365
## 4 stepwise    24.8  0.000365
checkem <- stocky %>%
  model('Auto' = ETS(Close),
        'Simple' = ETS(Close ~ error("A") + trend("N") + season("N")),
        'Holt-Winters' = ETS(Close ~ error("A") + trend("A") + season("N")),
        'Damped Holt-Winters' = ETS(Close ~ error("A") + trend("Ad") + season("N")),
        'arima010' = ARIMA(Close ~ pdq(0,1,0)))

checkem %>%
  glance()
## # A tibble: 5 × 11
##   .model    sigma2 log_lik   AIC  AICc   BIC   MSE  AMSE     MAE ar_ro…¹ ma_ro…²
##   <chr>      <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl> <list>  <list> 
## 1 Auto     1.64e-3  -1316. 2637. 2638. 2648.  135.  257.  0.0310 <NULL>  <NULL> 
## 2 Simple   1.36e+2  -1321. 2647. 2647. 2658.  135.  257.  8.82   <NULL>  <NULL> 
## 3 Holt-Wi… 1.38e+2  -1321. 2653. 2653. 2670.  136.  255.  8.93   <NULL>  <NULL> 
## 4 Damped … 1.39e+2  -1322. 2655. 2655. 2676.  136.  258.  8.90   <NULL>  <NULL> 
## 5 arima010 1.36e+2   -977. 1955. 1955. 1959.   NA    NA  NA      <cpl>   <cpl>  
## # … with abbreviated variable names ¹​ar_roots, ²​ma_roots
choochoo <- stocky %>% filter(row_number() < 177)
testies <- stocky %>% filter(row_number() > 176)


vallygirl <- choochoo %>%
  slice(1:(n() - 3)) %>%
  stretch_tsibble(.init = 10, .step = 1)

gotime <- vallygirl %>%
  model('Auto' = ETS(Close),
        'Simple' = ETS(Close ~ error("A") + trend("N") + season("N")),
        'Holt-Winters' = ETS(Close ~ error("A") + trend("A") + season("N")),
        'Damped Holt-Winters' = ETS(Close ~ error("A") + trend("Ad") + season("N")),
        'arima010' = ARIMA(Close ~ pdq(0,1,0)))

gotime %>% forecast(h = 1) %>%
  accuracy(stocky)
## # A tibble: 5 × 10
##   .model            .type      ME  RMSE   MAE     MPE  MAPE  MASE RMSSE     ACF1
##   <chr>             <chr>   <dbl> <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl>    <dbl>
## 1 arima010          Test  -0.651   12.9  9.89 -0.297   3.40  1.01  1.01 -0.0590 
## 2 Auto              Test  -0.742   13.0  9.90 -0.330   3.41  1.01  1.02 -0.0141 
## 3 Damped Holt-Wint… Test  -0.802   13.2 10.1  -0.345   3.46  1.03  1.03  0.0174 
## 4 Holt-Winters      Test  -0.0850  13.4 10.2  -0.0790  3.50  1.04  1.04  0.0307 
## 5 Simple            Test  -0.752   13.0  9.92 -0.333   3.41  1.01  1.02 -0.00876
Tom <- fitagain %>% select(arima010) %>% forecast(h=1) %>%
  hilo()


Tom
## # A tsibble: 1 x 6 [1]
## # Key:       .model [1]
##   .model    Date       Close .mean                  `80%`                  `95%`
##   <chr>    <dbl>      <dist> <dbl>                 <hilo>                 <hilo>
## 1 arima010   254 N(188, 136)  188. [172.8512, 202.7488]80 [164.9377, 210.6623]95

I found that the model with the lowest AICc and RMSE was the ARIMA (0,1,0). This model forecasted that the closing price of Tesla was 188. This had a 95% prediction interval of [164.9377, 210.6623]. I somewhat believe this forecast however, I don’t believe it will be exactly 188, largely due to the recent controversy surrounding Elon Musk. Knowing this I believe that the closing price tomorrow will be closer to the upper bound of my prediction interval. My final guess, given these forecasting methods and my own knowledge, is that the closing price tomorrow is $193.69.