#INTRODUCTION This week i will be working on the same data, tesla inc dataset to forecast the closing stock using ETS, ARIMA and GARCH models.

#removing all the variables in the working space 
rm(list = ls())

#loading the libraries 
library(fpp2)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## -- Attaching packages ---------------------------------------------- fpp2 2.4 --
## v ggplot2   3.3.3     v fma       2.4  
## v forecast  8.14      v expsmooth 2.3
## 
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.3     v dplyr   1.0.1
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(readr)
library(psych)
## 
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(caret)
## Loading required package: lattice
## 
## Attaching package: 'caret'
## The following object is masked from 'package:purrr':
## 
##     lift
library(readr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(rugarch)
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:purrr':
## 
##     reduce
## The following object is masked from 'package:stats':
## 
##     sigma
#Loading the tesla dataset
TSLA <- read_csv("C:/Users/HP/Desktop/TSLA.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   Date = col_date(format = ""),
##   Open = col_double(),
##   High = col_double(),
##   Low = col_double(),
##   Close = col_double(),
##   `Adj Close` = col_double(),
##   Volume = col_double()
## )
View(TSLA)
 
##attaching the variables in the TSLA dataset 
attach(TSLA)

##describing the closing prices 
describe(Close)
##    vars   n  mean     sd median trimmed   mad  min    max  range skew kurtosis
## X1    1 121 95.58 167.49  48.01   49.38 23.76 4.88 793.53 788.65 2.99     7.92
##       se
## X1 15.23
#selecting only important variables to be used in forecasting tesla closing stock prices 
tesla <-  select(TSLA,  Date, Close)
tesla
## # A tibble: 121 x 2
##    Date       Close
##    <date>     <dbl>
##  1 2011-05-01  6.03
##  2 2011-06-01  5.83
##  3 2011-07-01  5.63
##  4 2011-08-01  4.95
##  5 2011-09-01  4.88
##  6 2011-10-01  5.87
##  7 2011-11-01  6.55
##  8 2011-12-01  5.71
##  9 2012-01-01  5.81
## 10 2012-02-01  6.68
## # ... with 111 more rows
view(tesla)
#Declare the tesla dataset as a time series data 
tesla_data <- ts(tesla [, 2], start = c(2011,1), frequency = 12)
tesla_data
##          Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep
## 2011   6.028   5.826   5.634   4.948   4.878   5.874   6.548   5.712   5.814
## 2012   5.900   6.258   5.484   5.704   5.856   5.626   6.764   6.774   7.502
## 2013  19.552  21.472  26.856  33.800  38.674  31.988  25.456  30.086  36.282
## 2014  41.554  48.012  44.660  53.940  48.536  48.340  48.904  44.482  40.720
## 2015  50.160  53.652  53.230  49.812  49.680  41.386  46.052  48.002  38.240
## 2016  44.646  42.456  46.958  42.402  40.806  39.546  37.880  42.738  50.386
## 2017  68.202  72.322  64.694  71.180  68.220  66.306  61.770  62.270  70.862
## 2018  56.946  68.590  59.628  60.332  52.954  67.464  70.096  66.560  61.404
## 2019  37.032  44.692  48.322  45.122  48.174  62.984  65.988  83.666 130.114
## 2020 167.000 215.962 286.152 498.320 429.010 388.040 567.600 705.670 793.530
## 2021 739.780                                                                
##          Oct     Nov     Dec
## 2011   6.682   7.448   6.626
## 2012   6.966   7.578  10.798
## 2013  48.962  41.690  41.578
## 2014  40.668  37.754  45.210
## 2015  38.386  45.954  48.152
## 2016  49.998  55.660  62.814
## 2017  68.612  53.226  58.780
## 2018  63.976  55.972  47.738
## 2019 133.598 104.800 156.376
## 2020 675.500 667.930 739.780
## 2021
#time series plot 
autoplot(tesla_data)+
  ggtitle("Tesla Closing Prices")+
  ylab("Closing prices")+
  xlab("Time") +
  theme_grey()

#Training and testing sets for modelling 
tesla_train <- ts(tesla_data[1:97], start =2011 , frequency = 12)
tesla_train
##         Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct
## 2011  6.028  5.826  5.634  4.948  4.878  5.874  6.548  5.712  5.814  6.682
## 2012  5.900  6.258  5.484  5.704  5.856  5.626  6.764  6.774  7.502  6.966
## 2013 19.552 21.472 26.856 33.800 38.674 31.988 25.456 30.086 36.282 48.962
## 2014 41.554 48.012 44.660 53.940 48.536 48.340 48.904 44.482 40.720 40.668
## 2015 50.160 53.652 53.230 49.812 49.680 41.386 46.052 48.002 38.240 38.386
## 2016 44.646 42.456 46.958 42.402 40.806 39.546 37.880 42.738 50.386 49.998
## 2017 68.202 72.322 64.694 71.180 68.220 66.306 61.770 62.270 70.862 68.612
## 2018 56.946 68.590 59.628 60.332 52.954 67.464 70.096 66.560 61.404 63.976
## 2019 37.032                                                               
##         Nov    Dec
## 2011  7.448  6.626
## 2012  7.578 10.798
## 2013 41.690 41.578
## 2014 37.754 45.210
## 2015 45.954 48.152
## 2016 55.660 62.814
## 2017 53.226 58.780
## 2018 55.972 47.738
## 2019
tesla_test <- ts(tesla_data[98: 121], start = 2019 , frequency = 12)
tesla_test
##          Jan     Feb     Mar     Apr     May     Jun     Jul     Aug     Sep
## 2019  44.692  48.322  45.122  48.174  62.984  65.988  83.666 130.114 133.598
## 2020 215.962 286.152 498.320 429.010 388.040 567.600 705.670 793.530 675.500
##          Oct     Nov     Dec
## 2019 104.800 156.376 167.000
## 2020 667.930 739.780 739.780
#Modelling 
####ETS Model
ets_tesla_model <- ets(tesla_train)
ets_tesla_model
## ETS(M,A,N) 
## 
## Call:
##  ets(y = tesla_train) 
## 
##   Smoothing parameters:
##     alpha = 0.9999 
##     beta  = 0.1633 
## 
##   Initial states:
##     l = 5.3083 
##     b = 0.0628 
## 
##   sigma:  0.1504
## 
##      AIC     AICc      BIC 
## 733.9797 734.6391 746.8533
#modelling 
###auto.arima model 
arima_tesla_model <- auto.arima(tesla_train)
arima_tesla_model
## Series: tesla_train 
## ARIMA(0,1,0) 
## 
## sigma^2 estimated as 27.79:  log likelihood=-295.81
## AIC=593.62   AICc=593.66   BIC=596.18
#Forecasting the closing prices for the next 6 months using the ETS model
forecast_ets <- forecast(ets_tesla_model)
forecast_ets
##          Point Forecast       Lo 80    Hi 80         Lo 95    Hi 95
## Feb 2019    33.33149479   26.906975 39.75601  2.350604e+01 43.15695
## Mar 2019    29.63015128   20.158002 39.10230  1.514375e+01 44.11655
## Apr 2019    25.92880778   13.841162 38.01645  7.442348e+00 44.41527
## May 2019    22.22746427    7.693368 36.76156 -5.184806e-04 44.45545
## Jun 2019    18.52612076    1.614210 35.43803 -7.338415e+00 44.39066
## Jul 2019    14.82477725   -4.449993 34.09955 -1.465344e+01 44.30300
## Aug 2019    11.12343375  -10.535298 32.78217 -2.200074e+01 44.24761
## Sep 2019     7.42209024  -16.670606 31.51479 -2.942451e+01 44.26869
## Oct 2019     3.72074673  -22.882217 30.32371 -3.696498e+01 44.40647
## Nov 2019     0.01940322  -29.196030 29.23484 -4.466175e+01 44.70055
## Dec 2019    -3.68194029  -35.638649 28.27477 -5.255551e+01 45.19163
## Jan 2020    -7.38328379  -42.237920 27.47135 -6.068885e+01 45.92228
## Feb 2020   -11.08462730  -49.023143 26.85389 -6.910658e+01 46.93732
## Mar 2020   -14.78597081  -56.025102 26.45316 -7.785578e+01 48.28384
## Apr 2020   -18.48731432  -63.275978 26.30135 -8.698566e+01 50.01104
## May 2020   -22.18865782  -70.809214 26.43190 -9.654738e+01 52.17007
## Jun 2020   -25.89000133  -78.659359 26.87936 -1.065938e+02 54.81377
## Jul 2020   -29.59134484  -86.861936 27.67925 -1.171792e+02 57.99647
## Aug 2020   -33.29268835  -95.453348 28.86797 -1.283592e+02 61.77384
## Sep 2020   -36.99403186 -104.470828 30.48276 -1.401909e+02 66.20282
## Oct 2020   -40.69537536 -113.952457 32.56171 -1.527324e+02 71.34166
## Nov 2020   -44.39671887 -123.937238 35.14380 -1.660434e+02 77.25001
## Dec 2020   -48.09806238 -134.465223 38.26910 -1.801852e+02 83.98911
## Jan 2021   -51.79940589 -145.577690 41.97888 -1.952209e+02 91.62210
autoplot(forecast_ets, 
         main = "Tesla ETS Model",
         ylab = " Closing prices")

#Forecasting the closing prices for the next 6 months using the ARIMA model
forecast_arima <- forecast(arima_tesla_model)
forecast_arima
##          Point Forecast     Lo 80    Hi 80       Lo 95    Hi 95
## Feb 2019         37.032 30.275676 43.78833  26.6990939 47.36491
## Mar 2019         37.032 27.477115 46.58689  22.4190637 51.64494
## Apr 2019         37.032 25.329703 48.73430  19.1348810 54.92912
## May 2019         37.032 23.519351 50.54465  16.3661869 57.69782
## Jun 2019         37.032 21.924400 52.13960  13.9269184 60.13708
## Jul 2019         37.032 20.482453 53.58155  11.7216511 62.34235
## Aug 2019         37.032 19.156446 54.90756   9.6936986 64.37030
## Sep 2019         37.032 17.922229 56.14177   7.8061264 66.25788
## Oct 2019         37.032 16.763027 57.30098   6.0332798 68.03072
## Nov 2019         37.032 15.666626 58.39738   4.3564798 69.70752
## Dec 2019         37.032 14.623807 59.44020   2.7616252 71.30238
## Jan 2020         37.032 13.627405 60.43660   1.2377609 72.82624
## Feb 2020         37.032 12.671726 61.39228  -0.2238253 74.28783
## Mar 2020         37.032 11.752148 62.31185  -1.6301971 75.69420
## Apr 2020         37.032 10.864868 63.19913  -2.9871760 77.05118
## May 2020         37.032 10.006702 64.05730  -4.2996273 78.36363
## Jun 2020         37.032  9.174960 64.88904  -5.5716663 79.63567
## Jul 2020         37.032  8.367343 65.69666  -6.8068110 80.87081
## Aug 2020         37.032  7.581864 66.48214  -8.0080967 82.07210
## Sep 2020         37.032  6.816798 67.24720  -9.1781642 83.24217
## Oct 2020         37.032  6.070631 67.99337 -10.3193278 84.38333
## Nov 2020         37.032  5.342029 68.72197 -11.4336292 85.49763
## Dec 2020         37.032  4.629806 69.43420 -12.5228805 86.58688
## Jan 2021         37.032  3.932905 70.13110 -13.5886988 87.65270
autoplot(forecast_arima, 
         main = "Tesla ARIMA Model",
         ylab = " Closing prices")

#Forecasting the closing stock for the next 6 month using GARCH model 
spec_garch<- ugarchspec(variance.model=list(model="sGARCH",garchOrder=c(1,1)),mean.model=list(armaOrder=c(1,1)),distribution.model = "std")
spec_garch
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : sGARCH(1,1)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(1,0,1)
## Include Mean     : TRUE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  std 
## Includes Skew    :  FALSE 
## Includes Shape   :  TRUE 
## Includes Lambda  :  FALSE
garch_tesla <- ugarchfit(spec=spec_garch,data=tesla_train)
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
garch_tesla
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : std 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      6.044815    0.940892  6.42456  0.00000
## ar1     0.991136    0.014801 66.96407  0.00000
## ma1    -0.017795    0.127691 -0.13936  0.88916
## omega   5.388075    4.383856  1.22907  0.21904
## alpha1  0.000000    0.372543  0.00000  1.00000
## beta1   0.999000    0.016898 59.12046  0.00000
## shape   2.100000    0.029416 71.38860  0.00000
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      6.044815    0.050883 118.79936  0.00000
## ar1     0.991136    0.016496  60.08382  0.00000
## ma1    -0.017795    0.216227  -0.08230  0.93441
## omega   5.388075    6.574466   0.81955  0.41247
## alpha1  0.000000    1.114629   0.00000  1.00000
## beta1   0.999000    0.130116   7.67778  0.00000
## shape   2.100000    0.071748  29.26892  0.00000
## 
## LogLikelihood : -286.9664 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       6.0612
## Bayes        6.2470
## Shibata      6.0517
## Hannan-Quinn 6.1363
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.02865  0.8656
## Lag[2*(p+q)+(p+q)-1][5]   3.75538  0.1199
## Lag[4*(p+q)+(p+q)-1][9]   6.81016  0.1473
## d.o.f=2
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      2.356 0.12477
## Lag[2*(p+q)+(p+q)-1][5]     6.310 0.07598
## Lag[4*(p+q)+(p+q)-1][9]    12.454 0.01428
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale  P-Value
## ARCH Lag[3]     3.572 0.500 2.000 0.058769
## ARCH Lag[5]     6.104 1.440 1.667 0.056772
## ARCH Lag[7]    11.150 2.315 1.543 0.009892
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  13.7886
## Individual Statistics:              
## mu     0.18822
## ar1    0.08645
## ma1    0.10311
## omega  0.46046
## alpha1 0.14415
## beta1  0.07253
## shape  1.54094
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.69 1.9 2.35
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.5595 0.5772    
## Negative Sign Bias  0.9341 0.3527    
## Positive Sign Bias  1.0851 0.2807    
## Joint Effect        2.6923 0.4415    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     31.45     0.035976
## 2    30     55.47     0.002180
## 3    40     67.54     0.003069
## 4    50     65.49     0.057667
## 
## 
## Elapsed time : 0.3626781
fc.garch1 <- ugarchforecast(garch_tesla,n.ahead=6,method=c("Partial","Full")[1])
fc.garch1
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: sGARCH
## Horizon: 6
## Roll Steps: 0
## Out of Sample: 0
## 
## 0-roll forecast [T0=Jan 2019]:
##     Series Sigma
## T+1  36.94 22.87
## T+2  36.67 22.98
## T+3  36.40 23.09
## T+4  36.13 23.19
## T+5  35.86 23.30
## T+6  35.60 23.40

#MODEL COMPARISON BASED ON PERFORMANCE The ETS, ARIMA and GARCH model shows different results in regards to forecasting the tesla inc. closing stocks. The results reveals that the ARIMA model is the best model since it has the lowest AIC value and hence it is statistically significant.