Packages

library(tidyverse)
library(rugarch)
library(rmgarch)
library(quantmod)
library(xts)

Loading Data

Creating a clean data frame from the given data

df <- read.csv("uber_data.csv")
df$Date <- as.Date(df$Date, format = "%d-%m-%Y")
glimpse(df)
## Rows: 463
## Columns: 7
## $ Date   <date> 2019-05-10, 2019-05-13, 2019-05-14, 2019-05-15, 2019-05-16,...
## $ Open   <dbl> 42.000, 38.790, 38.310, 39.370, 41.480, 41.980, 41.190, 42.0...
## $ High   <dbl> 45.000, 39.240, 39.960, 41.880, 44.060, 43.290, 41.680, 42.2...
## $ Low    <dbl> 41.060, 36.080, 36.850, 38.950, 41.250, 41.270, 39.460, 41.2...
## $ Close  <dbl> 41.57, 37.10, 39.96, 41.29, 43.00, 41.91, 41.59, 41.50, 41.2...
## $ Volume <int> 186322500, 79442400, 46661100, 36086100, 38115500, 20225700,...
## $ Adj    <dbl> 41.57, 37.10, 39.96, 41.29, 43.00, 41.91, 41.59, 41.50, 41.2...
head(df)
##         Date  Open  High   Low Close    Volume   Adj
## 1 2019-05-10 42.00 45.00 41.06 41.57 186322500 41.57
## 2 2019-05-13 38.79 39.24 36.08 37.10  79442400 37.10
## 3 2019-05-14 38.31 39.96 36.85 39.96  46661100 39.96
## 4 2019-05-15 39.37 41.88 38.95 41.29  36086100 41.29
## 5 2019-05-16 41.48 44.06 41.25 43.00  38115500 43.00
## 6 2019-05-17 41.98 43.29 41.27 41.91  20225700 41.91


Initial plotting

#Setting date column as row indexes
rownames(df) <- df[,1]

#converting data frame to xts object for R to know it's time series
df<- xts(df[,-1], df$Date)

chartSeries(df)


Checking daily returns

df.returns <- dailyReturn(df)
colnames(df.returns) <- c("return")

chartSeries(df.returns)


Specifying GARCH Model

It’s in this you can change your model specification like Mean Model from ARIMA(1,0,1) to something you prefer.

ug_spec = ugarchspec()

#If you want to change ARIMA spec. Remove the comment sign and change the parameters
#ug_spec <- ugarchspec(mean.model=list(armaOrder=c(1,0)))

ug_spec
## 
## *---------------------------------*
## *       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 :  norm 
## Includes Skew    :  FALSE 
## Includes Shape   :  FALSE 
## Includes Lambda  :  FALSE


Model fit

ugfit = ugarchfit(spec = ug_spec, data = df.returns)

ugfit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.002639    0.001472  1.79285 0.072998
## ar1     0.321605    0.901716  0.35666 0.721347
## ma1    -0.309773    0.905751 -0.34201 0.732346
## omega   0.000229    0.000068  3.38496 0.000712
## alpha1  0.236495    0.058670  4.03095 0.000056
## beta1   0.592553    0.083886  7.06381 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.002639    0.001467   1.7995 0.071944
## ar1     0.321605    0.205104   1.5680 0.116879
## ma1    -0.309773    0.192572  -1.6086 0.107701
## omega   0.000229    0.000071   3.2073 0.001340
## alpha1  0.236495    0.087062   2.7164 0.006600
## beta1   0.592553    0.089510   6.6200 0.000000
## 
## LogLikelihood : 909.5444 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -3.9030
## Bayes        -3.8494
## Shibata      -3.9033
## Hannan-Quinn -3.8819
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                   0.004495  0.9465
## Lag[2*(p+q)+(p+q)-1][5]  3.075429  0.4253
## Lag[4*(p+q)+(p+q)-1][9]  5.081392  0.4353
## d.o.f=2
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                  8.815e-05  0.9925
## Lag[2*(p+q)+(p+q)-1][5] 2.418e-01  0.9892
## Lag[4*(p+q)+(p+q)-1][9] 1.331e+00  0.9682
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.1585 0.500 2.000  0.6905
## ARCH Lag[5]    0.4715 1.440 1.667  0.8920
## ARCH Lag[7]    1.2863 2.315 1.543  0.8630
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.6926
## Individual Statistics:              
## mu     0.18993
## ar1    0.08423
## ma1    0.08404
## omega  0.19881
## alpha1 0.17949
## beta1  0.16224
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias            1.507 0.13260    
## Negative Sign Bias   2.557 0.01087  **
## Positive Sign Bias   1.568 0.11756    
## Joint Effect         9.348 0.02500  **
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     27.93      0.08481
## 2    30     40.26      0.07975
## 3    40     48.88      0.13347
## 4    50     71.67      0.01903
## 
## 
## Elapsed time : 0.3188


Data points that can be taken from the above model

names(ugfit@fit)
##  [1] "hessian"         "cvar"            "var"             "sigma"          
##  [5] "condH"           "z"               "LLH"             "log.likelihoods"
##  [9] "residuals"       "coef"            "robust.cvar"     "A"              
## [13] "B"               "scores"          "se.coef"         "tval"           
## [17] "matcoef"         "robust.se.coef"  "robust.tval"     "robust.matcoef" 
## [21] "fitted.values"   "convergence"     "kappa"           "persistence"    
## [25] "timer"           "ipars"           "solver"

Coefficients

ugfit@fit$coef
##            mu           ar1           ma1         omega        alpha1 
##  0.0026390092  0.3216047516 -0.3097728219  0.0002288351  0.2364945570 
##         beta1 
##  0.5925527160


Plotting residual plot

# save the estimated conditional variances
ug_var <- ugfit@fit$var   

# save the estimated squared residuals
ug_res2 <- (ugfit@fit$residuals)^2  

plot(ug_res2, type = "l")
lines(ug_var, col = "green")


Forecasting

ugfore <- ugarchforecast(ugfit, n.ahead = 10)
ugfore
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
## 
## 0-roll forecast [T0=2021-03-11]:
##        Series   Sigma
## T+1  0.003048 0.03499
## T+2  0.002771 0.03527
## T+3  0.002681 0.03550
## T+4  0.002653 0.03569
## T+5  0.002643 0.03584
## T+6  0.002640 0.03597
## T+7  0.002639 0.03608
## T+8  0.002639 0.03617
## T+9  0.002639 0.03624
## T+10 0.002639 0.03630
ug_f <- ugfore@forecast$sigmaFor
plot(ug_f, type = "l")

ug_var_t <- c(tail(ug_var,20),rep(NA,10))  # gets the last 20 observations
ug_res2_t <- c(tail(ug_res2,20),rep(NA,10))  # gets the last 20 observations
ug_f <- c(rep(NA,20),(ug_f)^2)
plot(ug_res2_t, type = "l")
lines(ug_f, col = "orange")
lines(ug_var_t, col = "green")




Example 2


Loading Data

Creating a clean data frame from the given data

df.2 <- read.csv("example2.csv")
df.2$Date <- as.Date(df.2$Date, format = "%d-%m-%Y")
colnames(df.2) <- c("Date", "adjusted")
glimpse(df.2)
## Rows: 465
## Columns: 2
## $ Date     <date> 2019-05-10, 2019-05-13, 2019-05-14, 2019-05-15, 2019-05-1...
## $ adjusted <dbl> 41.57, 37.10, 39.96, 41.29, 43.00, 41.91, 41.59, 41.50, 41...
#Removing NA values
df.2 <- na.omit(df.2)

#Setting date column as row indexes
rownames(df.2) <- df.2[,1]

#converting data frame to xts object for R to know it's time series
df.2<- xts(df.2[,-1], df.2$Date)

chartSeries(df.2)


df.returns.2 <- dailyReturn(df.2)
colnames(df.returns.2) <- c("return")

chartSeries(df.returns.2)


## Model fit

ugfit.2 = ugarchfit(spec = ug_spec, data = df.returns.2)

ugfit.2
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(1,0,1)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.002606    0.001447   1.8009 0.071725
## ar1    -0.794624    0.680320  -1.1680 0.242801
## ma1     0.785901    0.694128   1.1322 0.257544
## omega   0.000228    0.000067   3.4004 0.000673
## alpha1  0.233026    0.057624   4.0439 0.000053
## beta1   0.595774    0.082998   7.1781 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.002606    0.001465   1.7786 0.075313
## ar1    -0.794624    0.346383  -2.2941 0.021787
## ma1     0.785901    0.351437   2.2363 0.025335
## omega   0.000228    0.000071   3.2156 0.001302
## alpha1  0.233026    0.085402   2.7286 0.006361
## beta1   0.595774    0.087598   6.8012 0.000000
## 
## LogLikelihood : 909.4766 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -3.9027
## Bayes        -3.8491
## Shibata      -3.9030
## Hannan-Quinn -3.8816
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.1692  0.6808
## Lag[2*(p+q)+(p+q)-1][5]    3.2321  0.3344
## Lag[4*(p+q)+(p+q)-1][9]    5.3673  0.3739
## d.o.f=2
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                   0.002107  0.9634
## Lag[2*(p+q)+(p+q)-1][5]  0.240521  0.9894
## Lag[4*(p+q)+(p+q)-1][9]  1.315906  0.9692
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.2042 0.500 2.000  0.6513
## ARCH Lag[5]    0.4545 1.440 1.667  0.8970
## ARCH Lag[7]    1.2574 2.315 1.543  0.8684
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.719
## Individual Statistics:              
## mu     0.20656
## ar1    0.04110
## ma1    0.04014
## omega  0.20038
## alpha1 0.18320
## beta1  0.16218
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias            1.366 0.17263    
## Negative Sign Bias   2.414 0.01618  **
## Positive Sign Bias   1.542 0.12382    
## Joint Effect         8.629 0.03466  **
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     27.24      0.09924
## 2    30     33.13      0.27237
## 3    40     61.67      0.01183
## 4    50     70.59      0.02336
## 
## 
## Elapsed time : 0.3416889


#### Data points that can be taken from the above model

names(ugfit.2@fit)
##  [1] "hessian"         "cvar"            "var"             "sigma"          
##  [5] "condH"           "z"               "LLH"             "log.likelihoods"
##  [9] "residuals"       "coef"            "robust.cvar"     "A"              
## [13] "B"               "scores"          "se.coef"         "tval"           
## [17] "matcoef"         "robust.se.coef"  "robust.tval"     "robust.matcoef" 
## [21] "fitted.values"   "convergence"     "kappa"           "persistence"    
## [25] "timer"           "ipars"           "solver"


Coefficients

ugfit.2@fit$coef
##            mu           ar1           ma1         omega        alpha1 
##  0.0026061520 -0.7946241498  0.7859013778  0.0002277885  0.2330256877 
##         beta1 
##  0.5957740135


Plotting residual plot

# save the estimated conditional variances
ug_var2 <- ugfit.2@fit$var   

# save the estimated squared residuals
ug_res22 <- (ugfit.2@fit$residuals)^2  

plot(ug_res22, type = "l")
lines(ug_var2, col = "green")

Forecasting

ugfore2 <- ugarchforecast(ugfit.2, n.ahead = 10)
ugfore2
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
## 
## 0-roll forecast [T0=2021-03-11]:
##        Series   Sigma
## T+1  0.001913 0.03494
## T+2  0.003157 0.03521
## T+3  0.002169 0.03543
## T+4  0.002954 0.03561
## T+5  0.002330 0.03576
## T+6  0.002826 0.03589
## T+7  0.002432 0.03599
## T+8  0.002745 0.03607
## T+9  0.002496 0.03614
## T+10 0.002694 0.03620
ug_f2 <- ugfore2@forecast$sigmaFor
plot(ug_f2, type = "l")

ug_var_t2 <- c(tail(ug_var2,20),rep(NA,10))  # gets the last 20 observations
ug_res2_t2 <- c(tail(ug_res22,20),rep(NA,10))  # gets the last 20 observations
ug_f2 <- c(rep(NA,20),(ug_f2)^2)
plot(ug_res2_t2, type = "l")
lines(ug_f2, col = "orange")
lines(ug_var_t2, col = "green")