library(readxl)
library(tidyquant)
library(fpp3)
library(moments)
library(tsibble)
library(tsibbledata)
library(ggplot2)
library(dplyr)
library(seasonal)
library(seasonalview)
library(fabletools)
library(fable)
library(ggfortify)
library(quantmod)
library(GGally)
library(feasts)
tlsa_data <- read_excel("/Users/Peter Cook/Documents/Economics and Finance/Business Forecasting/Forecasting Data/tesla.xlsx")

tlsa_tsibble <- tlsa_data %>%
  mutate(Date = DATEVALUE(Date)) %>%
  as_tsibble(index = Date)

autoplot(tlsa_tsibble) +
  labs(title = "Tesla Stock Price Over the Last Month")
## Plot variable not specified, automatically selected `.vars = Close`

#Downloaded the historical (1 year) Tesla stock price data from Yahoo Finance. Cleaned the data in excel and converted it into a tsibble. The above autplot shows the Tesla stock price over a year.

#The data appears to not have much seasonality, but has a clear downward trend and is highly cyclic. #Because we are using stock data, which tends to follow a random walk, so it may be worth considering a NAIVE model. Because of the trend, I’d also like to try a Linear Trend model.

tlsa_tsibble %>%
  model(NAIVE(Close)) %>%
  forecast(h = 1) %>%
  autoplot(tlsa_tsibble) +
  labs(title = "2 Day Forecast of Tesla Stock Closing Price")+
  labs(subtitle = "Using the NAIVE Model")
## Warning: 1 error encountered for NAIVE(Close)
## [1] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.
## Warning: Removed 1 rows containing missing values (geom_segment).
## Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values.

tlsa_tsibble %>%
  model(
    `Holt's method` = ETS(Close ~ error("A") +  trend("A") + season("N"))) %>%
  forecast(h = 1) %>%
  autoplot(tlsa_tsibble)
## Warning: 1 error encountered for Holt's method
## [1] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.
## Warning: Removed 1 rows containing missing values (geom_segment).
## Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values.

tlsa_tsibble %>%
   model(arima210 = ARIMA(Close ~ pdq(2,1,0))) %>%
  forecast(h = 1) %>%
  autoplot(tlsa_tsibble)
## Warning: 1 error encountered for arima210
## [1] .data contains implicit gaps in time. You should check your data and convert implicit gaps into explicit missing values using `tsibble::fill_gaps()` if required.
## Warning: Removed 1 rows containing missing values (geom_segment).
## Removed 1 rows containing missing values (geom_segment).
## Warning: Removed 1 rows containing missing values.

#I also tried an Arima model as well.

#I predict that the closing Tesla Stock price tomorrow will be $190.2.