#Loading Libraries
library(fGarch)
## Warning: package 'fGarch' was built under R version 4.4.3
## NOTE: Packages 'fBasics', 'timeDate', and 'timeSeries' are no longer
## attached to the search() path when 'fGarch' is attached.
## 
## If needed attach them yourself in your R script by e.g.,
##         require("timeSeries")
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.4.3
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
library(timeSeries)
## Loading required package: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:rugarch':
## 
##     quantile
## The following objects are masked from 'package:graphics':
## 
##     lines, points
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
#Setting Working Directory and Importing Dataset
setwd("C:\\Users\\derek\\OneDrive\\Desktop\\R Class")
data= read.csv("bills.csv");data
##     Month    BONDS BILLS
## 1     Jul  9.77600 16.91
## 2     Mar  9.88250 17.73
## 3     Mar  8.71850 17.87
## 4     Feb  8.38350 17.84
## 5     Mar 23.73200 20.34
## 6     Jun  8.26625 16.06
## 7     Feb 10.63040 17.91
## 8     Jun  7.39000 18.18
## 9     Jul 10.56575 15.75
## 10    Jan  8.58650 15.93
## 11    Apr  8.91927 18.04
## 12    Feb  8.38350 17.84
## 13    Jun  7.39000 18.18
## 14    Jan  9.25900 17.03
## 15    Feb  9.16000 17.06
## 16    Mar  9.00700 16.91
## 17    May 10.44000 20.12
## 18    Mar  8.71850 17.87
## 19    Mar  9.00700 16.91
## 20    Apr  8.80300 16.70
## 21    Sep  7.77200 19.73
## 22    May  8.82100 16.97
## 23    Nov  8.64300 15.94
## 24    Aug 10.92900 20.13
## 25    Feb  9.16000 17.06
## 26    Jul 11.95420 20.15
## 27    Jun  8.26625 16.06
## 28    Sep  7.77200 19.73
## 29    Jul  5.91720 17.02
## 30    Dec  8.57800 15.99
## 31    Feb  8.38350 17.84
## 32    May  9.46200 17.45
## 33    Jul 10.56575 15.75
## 34    Mar 10.17980 15.46
## 35    May  8.05750 18.22
## 36    Dec  8.24760 18.15
## 37    Mar  9.88250 17.73
## 38    Aug  8.41060 16.26
## 39    Jun  8.26625 16.06
## 40    Sep 14.61320 16.82
## 41    Jul 11.95420 20.15
## 42    Sep  7.77200 19.73
## 43    May  8.25650 15.26
## 44    Oct  9.19600 19.04
## 45    Nov  9.94500 16.89
## 46    Mar  9.00700 16.91
## 47    May  8.05750 18.22
## 48    Jul 11.95420 20.15
## 49    May  8.05750 18.22
## 50    Mar  9.00700 16.91
## 51    Aug  8.41060 16.26
## 52    Feb  8.59000 15.47
## 53    Jan  9.25900 17.03
## 54    Oct  8.68400 16.00
## 55    May  8.82100 16.97
## 56    May 10.44000 20.12
## 57    Aug 10.92900 20.13
## 58    Dec  8.24760 18.15
## 59    Jan  8.07880 18.13
## 60    Jun  6.20600 16.97
## 61    Sep  8.47900 16.04
## 62    Mar  9.00700 16.91
## 63    Jan  9.25900 17.03
## 64    Feb  8.59000 15.47
## 65    Sep  9.68650 16.86
## 66    Mar  9.88250 17.73
## 67    May  8.25650 15.26
## 68    Nov 12.33340 17.16
## 69    Feb  9.16000 17.06
## 70    Jul  9.77600 16.91
## 71    Apr 10.37900 17.87
## 72    Apr  8.91927 18.04
## 73    Jun  9.80960 16.36
## 74    Jun 10.18300 20.30
## 75    Jul 10.56575 15.75
## 76    Aug 10.92900 20.13
## 77    Oct  9.71700 17.00
## 78    Oct  9.71700 17.00
## 79    Mar 10.17980 15.46
## 80    Jul  9.77600 16.91
## 81    Dec  9.90890 18.30
## 82    May  9.46200 17.45
## 83    Jun  7.39000 18.18
## 84    Feb 10.63040 17.91
## 85    Jan 11.35820 18.00
## 86    Oct  8.68400 16.00
## 87    Apr 20.01700 20.22
## 88    Jan  8.07880 18.13
## 89    May 10.44000 20.12
## 90    Jun  7.39000 18.18
## 91    Mar  8.71850 17.87
## 92    Jan  9.25900 17.03
## 93    Apr  8.91927 18.04
## 94    Oct  9.71700 17.00
## 95    Jan  9.25900 17.03
## 96    Aug  8.41060 16.26
## 97    Oct 21.65400 16.58
## 98    Jan  9.25900 17.03
## 99    Nov  9.94500 16.89
## 100   Nov  8.64300 15.94
## 101   Apr  8.42250 15.40
## 102   Dec  9.90890 18.30
## 103   Jun  9.80960 16.36
## 104   Dec  8.24760 18.15
## 105   Jul  9.77600 16.91
## 106   Oct 21.65400 16.58
## 107   Jun  9.80960 16.36
## 108   Nov  8.64300 15.94
## 109   Feb  8.38350 17.84
## 110   May  8.82100 16.97
#Selecting the Treasury Bills Column
D1=data$BILLS;D1
##   [1] 16.91 17.73 17.87 17.84 20.34 16.06 17.91 18.18 15.75 15.93 18.04 17.84
##  [13] 18.18 17.03 17.06 16.91 20.12 17.87 16.91 16.70 19.73 16.97 15.94 20.13
##  [25] 17.06 20.15 16.06 19.73 17.02 15.99 17.84 17.45 15.75 15.46 18.22 18.15
##  [37] 17.73 16.26 16.06 16.82 20.15 19.73 15.26 19.04 16.89 16.91 18.22 20.15
##  [49] 18.22 16.91 16.26 15.47 17.03 16.00 16.97 20.12 20.13 18.15 18.13 16.97
##  [61] 16.04 16.91 17.03 15.47 16.86 17.73 15.26 17.16 17.06 16.91 17.87 18.04
##  [73] 16.36 20.30 15.75 20.13 17.00 17.00 15.46 16.91 18.30 17.45 18.18 17.91
##  [85] 18.00 16.00 20.22 18.13 20.12 18.18 17.87 17.03 18.04 17.00 17.03 16.26
##  [97] 16.58 17.03 16.89 15.94 15.40 18.30 16.36 18.15 16.91 16.58 16.36 15.94
## [109] 17.84 16.97
#(I) GARCH MODEL
#Time Series Plot
ts.plot(D1,
        main = "Time Series of Treasury Bills Rate of Return",
        ylab = "Amount (KSH)",
        xlab = "Observation Index",
        col = "darkblue",
        lwd = 2,
        lty = 1,  # Solid line
        ylim = c(min(D1) - 1, max(D1) + 1))  # Adjust y-axis range

#Differencing and Plotting The Differenced Series
log.D1=diff(D1);log.D1
##   [1]  0.82  0.14 -0.03  2.50 -4.28  1.85  0.27 -2.43  0.18  2.11 -0.20  0.34
##  [13] -1.15  0.03 -0.15  3.21 -2.25 -0.96 -0.21  3.03 -2.76 -1.03  4.19 -3.07
##  [25]  3.09 -4.09  3.67 -2.71 -1.03  1.85 -0.39 -1.70 -0.29  2.76 -0.07 -0.42
##  [37] -1.47 -0.20  0.76  3.33 -0.42 -4.47  3.78 -2.15  0.02  1.31  1.93 -1.93
##  [49] -1.31 -0.65 -0.79  1.56 -1.03  0.97  3.15  0.01 -1.98 -0.02 -1.16 -0.93
##  [61]  0.87  0.12 -1.56  1.39  0.87 -2.47  1.90 -0.10 -0.15  0.96  0.17 -1.68
##  [73]  3.94 -4.55  4.38 -3.13  0.00 -1.54  1.45  1.39 -0.85  0.73 -0.27  0.09
##  [85] -2.00  4.22 -2.09  1.99 -1.94 -0.31 -0.84  1.01 -1.04  0.03 -0.77  0.32
##  [97]  0.45 -0.14 -0.95 -0.54  2.90 -1.94  1.79 -1.24 -0.33 -0.22 -0.42  1.90
## [109] -0.87
plot(log.D1, 
     type = "l",  # Line plot
     col = "darkred", 
     lwd = 2,
     main = "First Differences of Treasury Bills Rate Of Return",
     xlab = "Time",
     ylab = "Difference in Rate of Return (%)")

#Autocorrelation Analysis (ACF/PACF)
par(mfrow = c(1, 2)) 
acf(log.D1, main = "ACF of Differenced Series", col = "blue")
pacf(log.D1, main = "PACF of Differenced Series", col = "darkgreen")

#Removing Negatives
S=log.D1^2;S
##   [1]  0.6724  0.0196  0.0009  6.2500 18.3184  3.4225  0.0729  5.9049  0.0324
##  [10]  4.4521  0.0400  0.1156  1.3225  0.0009  0.0225 10.3041  5.0625  0.9216
##  [19]  0.0441  9.1809  7.6176  1.0609 17.5561  9.4249  9.5481 16.7281 13.4689
##  [28]  7.3441  1.0609  3.4225  0.1521  2.8900  0.0841  7.6176  0.0049  0.1764
##  [37]  2.1609  0.0400  0.5776 11.0889  0.1764 19.9809 14.2884  4.6225  0.0004
##  [46]  1.7161  3.7249  3.7249  1.7161  0.4225  0.6241  2.4336  1.0609  0.9409
##  [55]  9.9225  0.0001  3.9204  0.0004  1.3456  0.8649  0.7569  0.0144  2.4336
##  [64]  1.9321  0.7569  6.1009  3.6100  0.0100  0.0225  0.9216  0.0289  2.8224
##  [73] 15.5236 20.7025 19.1844  9.7969  0.0000  2.3716  2.1025  1.9321  0.7225
##  [82]  0.5329  0.0729  0.0081  4.0000 17.8084  4.3681  3.9601  3.7636  0.0961
##  [91]  0.7056  1.0201  1.0816  0.0009  0.5929  0.1024  0.2025  0.0196  0.9025
## [100]  0.2916  8.4100  3.7636  3.2041  1.5376  0.1089  0.0484  0.1764  3.6100
## [109]  0.7569
summary(S)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.1156  1.0816  3.6932  4.3681 20.7025
shapiro.test(D1)
## 
##  Shapiro-Wilk normality test
## 
## data:  D1
## W = 0.92318, p-value = 8.65e-06
M1=garchFit(~garch(1,1),data = log.D1,trace = F);M1
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x00000277a2923eb0>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##        mu      omega     alpha1      beta1  
## 0.0055046  1.7887270  0.2972967  0.2046660  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu      0.005505    0.147082    0.037   0.9701  
## omega   1.788727    0.768708    2.327   0.0200 *
## alpha1  0.297297    0.133754    2.223   0.0262 *
## beta1   0.204666    0.219461    0.933   0.3510  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.414    normalized:  -2.022147 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek
M2=garchFit(~garch(1,2),data = log.D1,trace = F);M2
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000277a4309808>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1       beta2  
## 0.00550459  1.79245128  0.28948183  0.21003982  0.00000001  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu     5.505e-03   1.520e-01    0.036   0.9711  
## omega  1.792e+00   8.412e-01    2.131   0.0331 *
## alpha1 2.895e-01   1.339e-01    2.162   0.0306 *
## beta1  2.100e-01   6.394e-01    0.329   0.7425  
## beta2  1.000e-08   5.383e-01    0.000   1.0000  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.5755    normalized:  -2.023628 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek
M3=garchFit(~garch(2,1),data = log.D1,trace = F);M3
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(2, 1), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(2, 1)
## <environment: 0x000002779304cbd8>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1      alpha2       beta1  
## 0.00550459  2.21807069  0.27678708  0.10274595  0.00000001  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu     5.505e-03   1.467e-01    0.038   0.9701  
## omega  2.218e+00   1.422e+00    1.559   0.1189  
## alpha1 2.768e-01   1.322e-01    2.093   0.0363 *
## alpha2 1.027e-01   2.150e-01    0.478   0.6328  
## beta1  1.000e-08   5.621e-01    0.000   1.0000  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.4115    normalized:  -2.022124 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek
#Checking And Comparing AIC/BIC
comparison <- data.frame(
  Model = c("GARCH(1,1)", "GARCH(1,2)", "GARCH(2,1)"),
  AIC = c(M1@fit$ics["AIC"], M2@fit$ics["AIC"], M3@fit$ics["AIC"]),
  BIC = c(M1@fit$ics["BIC"], M2@fit$ics["BIC"], M3@fit$ics["BIC"])
) 
print(comparison)
##        Model      AIC      BIC
## 1 GARCH(1,1) 4.117688 4.216453
## 2 GARCH(1,2) 4.139000 4.262456
## 3 GARCH(2,1) 4.135991 4.259448
#Significance of Coefficients
summary(M1) 
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x00000277a2923eb0>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##        mu      omega     alpha1      beta1  
## 0.0055046  1.7887270  0.2972967  0.2046660  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu      0.005505    0.147082    0.037   0.9701  
## omega   1.788727    0.768708    2.327   0.0200 *
## alpha1  0.297297    0.133754    2.223   0.0262 *
## beta1   0.204666    0.219461    0.933   0.3510  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.414    normalized:  -2.022147 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2   1.9705271 0.3733408172
##  Shapiro-Wilk Test  R    W       0.9779825 0.0677367869
##  Ljung-Box Test     R    Q(10)  33.3740420 0.0002356307
##  Ljung-Box Test     R    Q(15)  43.5270458 0.0001303603
##  Ljung-Box Test     R    Q(20)  44.2494630 0.0013933457
##  Ljung-Box Test     R^2  Q(10)   5.7710271 0.8341196225
##  Ljung-Box Test     R^2  Q(15)   9.3529708 0.8583505082
##  Ljung-Box Test     R^2  Q(20)  11.1563062 0.9420678282
##  LM Arch Test       R    TR^2   10.2125898 0.5973163147
## 
## Information Criterion Statistics:
##      AIC      BIC      SIC     HQIC 
## 4.117688 4.216453 4.115120 4.157741
summary(M2) 
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000277a4309808>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1       beta2  
## 0.00550459  1.79245128  0.28948183  0.21003982  0.00000001  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu     5.505e-03   1.520e-01    0.036   0.9711  
## omega  1.792e+00   8.412e-01    2.131   0.0331 *
## alpha1 2.895e-01   1.339e-01    2.162   0.0306 *
## beta1  2.100e-01   6.394e-01    0.329   0.7425  
## beta2  1.000e-08   5.383e-01    0.000   1.0000  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.5755    normalized:  -2.023628 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2   1.9405147 0.3789854922
##  Shapiro-Wilk Test  R    W       0.9782288 0.0710801820
##  Ljung-Box Test     R    Q(10)  33.5889540 0.0002167586
##  Ljung-Box Test     R    Q(15)  43.8519796 0.0001159868
##  Ljung-Box Test     R    Q(20)  44.5687624 0.0012620585
##  Ljung-Box Test     R^2  Q(10)   5.8807540 0.8251802569
##  Ljung-Box Test     R^2  Q(15)   9.4543594 0.8525884717
##  Ljung-Box Test     R^2  Q(20)  11.2154316 0.9404431795
##  LM Arch Test       R    TR^2   10.2404177 0.5948779555
## 
## Information Criterion Statistics:
##      AIC      BIC      SIC     HQIC 
## 4.139000 4.262456 4.135033 4.189066
summary(M3) 
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(2, 1), data = log.D1, trace = F) 
## 
## Mean and Variance Equation:
##  data ~ garch(2, 1)
## <environment: 0x000002779304cbd8>
##  [data = log.D1]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1      alpha2       beta1  
## 0.00550459  2.21807069  0.27678708  0.10274595  0.00000001  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)  
## mu     5.505e-03   1.467e-01    0.038   0.9701  
## omega  2.218e+00   1.422e+00    1.559   0.1189  
## alpha1 2.768e-01   1.322e-01    2.093   0.0363 *
## alpha2 1.027e-01   2.150e-01    0.478   0.6328  
## beta1  1.000e-08   5.621e-01    0.000   1.0000  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -220.4115    normalized:  -2.022124 
## 
## Description:
##  Sun May 18 15:18:36 2025 by user: derek 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2   2.3235348 0.3129326182
##  Shapiro-Wilk Test  R    W       0.9764039 0.0497583829
##  Ljung-Box Test     R    Q(10)  33.5772536 0.0002177468
##  Ljung-Box Test     R    Q(15)  43.4813182 0.0001325178
##  Ljung-Box Test     R    Q(20)  44.2115063 0.0014097968
##  Ljung-Box Test     R^2  Q(10)   6.0591424 0.8102701112
##  Ljung-Box Test     R^2  Q(15)   9.7042261 0.8379209427
##  Ljung-Box Test     R^2  Q(20)  11.0814459 0.9440829710
##  LM Arch Test       R    TR^2   10.3761132 0.5830010032
## 
## Information Criterion Statistics:
##      AIC      BIC      SIC     HQIC 
## 4.135991 4.259448 4.132024 4.186057
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------

#(II) Exponential GARCH (EGARCH) MODEL

# Import libraries
library(rugarch)
library(forecast)

# Set working directory and import dataset
setwd("C:/Users/derek/OneDrive/Desktop/R Class")
data <- read.csv("bills.csv")

# Select the Treasury Bills column
d2 <- data$BILLS

# Define the eGARCH model specification
egm2 <- ugarchspec(
  variance.model = list(model = "eGARCH", garchOrder = c(1, 1)),
  mean.model = list(armaOrder = c(0, 0), include.mean = TRUE),  # <- change here
  distribution.model = "norm"
)

# Fit the eGARCH model
egm2_fit <- ugarchfit(spec = egm2, data = d2)
egm2_fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     17.420734    0.164916 105.63409  0.00000
## omega   0.068730    0.083592   0.82220  0.41096
## alpha1 -0.065874    0.103935  -0.63379  0.52621
## beta1   0.889915    0.138093   6.44434  0.00000
## gamma1 -0.070927    0.118264  -0.59974  0.54868
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     17.420734    0.425703 40.92230  0.00000
## omega   0.068730    0.067817  1.01347  0.31084
## alpha1 -0.065874    0.283103 -0.23268  0.81601
## beta1   0.889915    0.093713  9.49620  0.00000
## gamma1 -0.070927    0.136008 -0.52149  0.60202
## 
## LogLikelihood : -187.6314 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       3.5024
## Bayes        3.6251
## Shibata      3.4985
## Hannan-Quinn 3.5522
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.04092  0.8397
## Lag[2*(p+q)+(p+q)-1][2]   0.48513  0.7006
## Lag[4*(p+q)+(p+q)-1][5]   1.26482  0.7974
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.1831  0.6687
## Lag[2*(p+q)+(p+q)-1][5]    1.2283  0.8062
## Lag[4*(p+q)+(p+q)-1][9]    2.3875  0.8541
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.6457 0.500 2.000  0.4217
## ARCH Lag[5]    0.7079 1.440 1.667  0.8211
## ARCH Lag[7]    1.7794 2.315 1.543  0.7639
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.7115
## Individual Statistics:              
## mu     0.17004
## omega  0.35284
## alpha1 0.07106
## beta1  0.37865
## gamma1 0.07738
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.90461 0.3677    
## Negative Sign Bias 0.07789 0.9381    
## Positive Sign Bias 0.85743 0.3932    
## Joint Effect       1.51995 0.6777    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     111.5    4.297e-15
## 2    30     128.9    1.476e-14
## 3    40     134.4    2.095e-12
## 4    50     139.1    1.460e-10
## 
## 
## Elapsed time : 0.3751609
# Display the fit summary
egm2_fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu     17.420734    0.164916 105.63409  0.00000
## omega   0.068730    0.083592   0.82220  0.41096
## alpha1 -0.065874    0.103935  -0.63379  0.52621
## beta1   0.889915    0.138093   6.44434  0.00000
## gamma1 -0.070927    0.118264  -0.59974  0.54868
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     17.420734    0.425703 40.92230  0.00000
## omega   0.068730    0.067817  1.01347  0.31084
## alpha1 -0.065874    0.283103 -0.23268  0.81601
## beta1   0.889915    0.093713  9.49620  0.00000
## gamma1 -0.070927    0.136008 -0.52149  0.60202
## 
## LogLikelihood : -187.6314 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       3.5024
## Bayes        3.6251
## Shibata      3.4985
## Hannan-Quinn 3.5522
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.04092  0.8397
## Lag[2*(p+q)+(p+q)-1][2]   0.48513  0.7006
## Lag[4*(p+q)+(p+q)-1][5]   1.26482  0.7974
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.1831  0.6687
## Lag[2*(p+q)+(p+q)-1][5]    1.2283  0.8062
## Lag[4*(p+q)+(p+q)-1][9]    2.3875  0.8541
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.6457 0.500 2.000  0.4217
## ARCH Lag[5]    0.7079 1.440 1.667  0.8211
## ARCH Lag[7]    1.7794 2.315 1.543  0.7639
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.7115
## Individual Statistics:              
## mu     0.17004
## omega  0.35284
## alpha1 0.07106
## beta1  0.37865
## gamma1 0.07738
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.90461 0.3677    
## Negative Sign Bias 0.07789 0.9381    
## Positive Sign Bias 0.85743 0.3932    
## Joint Effect       1.51995 0.6777    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     111.5    4.297e-15
## 2    30     128.9    1.476e-14
## 3    40     134.4    2.095e-12
## 4    50     139.1    1.460e-10
## 
## 
## Elapsed time : 0.3751609
#PLotting The Fitted Model
par(mfrow = c(3, 4))
for(i in 1:12){
  plot(egm2_fit, which = i)
}
## 
## please wait...calculating quantiles...

#Forecasting
forc=ugarchforecast(egm2_fit);forc
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: eGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
## 
## 0-roll forecast [T0=1970-04-21]:
##      Series Sigma
## T+1   17.42 1.589
## T+2   17.42 1.562
## T+3   17.42 1.540
## T+4   17.42 1.519
## T+5   17.42 1.502
## T+6   17.42 1.486
## T+7   17.42 1.473
## T+8   17.42 1.460
## T+9   17.42 1.450
## T+10  17.42 1.440
#Plotting The Forecasting results
plot(forc, which = 1)  

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

#(III) GJR-GARCH MODEL
#Importing Libraries
library(rugarch)
library(forecast)

#Setting Working Directory and Importing Dataset
setwd("C:\\Users\\derek\\OneDrive\\Desktop\\R Class")
data= read.csv("bills.csv");data
##     Month    BONDS BILLS
## 1     Jul  9.77600 16.91
## 2     Mar  9.88250 17.73
## 3     Mar  8.71850 17.87
## 4     Feb  8.38350 17.84
## 5     Mar 23.73200 20.34
## 6     Jun  8.26625 16.06
## 7     Feb 10.63040 17.91
## 8     Jun  7.39000 18.18
## 9     Jul 10.56575 15.75
## 10    Jan  8.58650 15.93
## 11    Apr  8.91927 18.04
## 12    Feb  8.38350 17.84
## 13    Jun  7.39000 18.18
## 14    Jan  9.25900 17.03
## 15    Feb  9.16000 17.06
## 16    Mar  9.00700 16.91
## 17    May 10.44000 20.12
## 18    Mar  8.71850 17.87
## 19    Mar  9.00700 16.91
## 20    Apr  8.80300 16.70
## 21    Sep  7.77200 19.73
## 22    May  8.82100 16.97
## 23    Nov  8.64300 15.94
## 24    Aug 10.92900 20.13
## 25    Feb  9.16000 17.06
## 26    Jul 11.95420 20.15
## 27    Jun  8.26625 16.06
## 28    Sep  7.77200 19.73
## 29    Jul  5.91720 17.02
## 30    Dec  8.57800 15.99
## 31    Feb  8.38350 17.84
## 32    May  9.46200 17.45
## 33    Jul 10.56575 15.75
## 34    Mar 10.17980 15.46
## 35    May  8.05750 18.22
## 36    Dec  8.24760 18.15
## 37    Mar  9.88250 17.73
## 38    Aug  8.41060 16.26
## 39    Jun  8.26625 16.06
## 40    Sep 14.61320 16.82
## 41    Jul 11.95420 20.15
## 42    Sep  7.77200 19.73
## 43    May  8.25650 15.26
## 44    Oct  9.19600 19.04
## 45    Nov  9.94500 16.89
## 46    Mar  9.00700 16.91
## 47    May  8.05750 18.22
## 48    Jul 11.95420 20.15
## 49    May  8.05750 18.22
## 50    Mar  9.00700 16.91
## 51    Aug  8.41060 16.26
## 52    Feb  8.59000 15.47
## 53    Jan  9.25900 17.03
## 54    Oct  8.68400 16.00
## 55    May  8.82100 16.97
## 56    May 10.44000 20.12
## 57    Aug 10.92900 20.13
## 58    Dec  8.24760 18.15
## 59    Jan  8.07880 18.13
## 60    Jun  6.20600 16.97
## 61    Sep  8.47900 16.04
## 62    Mar  9.00700 16.91
## 63    Jan  9.25900 17.03
## 64    Feb  8.59000 15.47
## 65    Sep  9.68650 16.86
## 66    Mar  9.88250 17.73
## 67    May  8.25650 15.26
## 68    Nov 12.33340 17.16
## 69    Feb  9.16000 17.06
## 70    Jul  9.77600 16.91
## 71    Apr 10.37900 17.87
## 72    Apr  8.91927 18.04
## 73    Jun  9.80960 16.36
## 74    Jun 10.18300 20.30
## 75    Jul 10.56575 15.75
## 76    Aug 10.92900 20.13
## 77    Oct  9.71700 17.00
## 78    Oct  9.71700 17.00
## 79    Mar 10.17980 15.46
## 80    Jul  9.77600 16.91
## 81    Dec  9.90890 18.30
## 82    May  9.46200 17.45
## 83    Jun  7.39000 18.18
## 84    Feb 10.63040 17.91
## 85    Jan 11.35820 18.00
## 86    Oct  8.68400 16.00
## 87    Apr 20.01700 20.22
## 88    Jan  8.07880 18.13
## 89    May 10.44000 20.12
## 90    Jun  7.39000 18.18
## 91    Mar  8.71850 17.87
## 92    Jan  9.25900 17.03
## 93    Apr  8.91927 18.04
## 94    Oct  9.71700 17.00
## 95    Jan  9.25900 17.03
## 96    Aug  8.41060 16.26
## 97    Oct 21.65400 16.58
## 98    Jan  9.25900 17.03
## 99    Nov  9.94500 16.89
## 100   Nov  8.64300 15.94
## 101   Apr  8.42250 15.40
## 102   Dec  9.90890 18.30
## 103   Jun  9.80960 16.36
## 104   Dec  8.24760 18.15
## 105   Jul  9.77600 16.91
## 106   Oct 21.65400 16.58
## 107   Jun  9.80960 16.36
## 108   Nov  8.64300 15.94
## 109   Feb  8.38350 17.84
## 110   May  8.82100 16.97
#Selecting the Treasury Bills Column
D3=data$BILLS;D3
##   [1] 16.91 17.73 17.87 17.84 20.34 16.06 17.91 18.18 15.75 15.93 18.04 17.84
##  [13] 18.18 17.03 17.06 16.91 20.12 17.87 16.91 16.70 19.73 16.97 15.94 20.13
##  [25] 17.06 20.15 16.06 19.73 17.02 15.99 17.84 17.45 15.75 15.46 18.22 18.15
##  [37] 17.73 16.26 16.06 16.82 20.15 19.73 15.26 19.04 16.89 16.91 18.22 20.15
##  [49] 18.22 16.91 16.26 15.47 17.03 16.00 16.97 20.12 20.13 18.15 18.13 16.97
##  [61] 16.04 16.91 17.03 15.47 16.86 17.73 15.26 17.16 17.06 16.91 17.87 18.04
##  [73] 16.36 20.30 15.75 20.13 17.00 17.00 15.46 16.91 18.30 17.45 18.18 17.91
##  [85] 18.00 16.00 20.22 18.13 20.12 18.18 17.87 17.03 18.04 17.00 17.03 16.26
##  [97] 16.58 17.03 16.89 15.94 15.40 18.30 16.36 18.15 16.91 16.58 16.36 15.94
## [109] 17.84 16.97
#GJR-GARCH(1,1) Model With Standard Normal Distribution
N1=ugarchspec(variance.model=list(model="gjrGARCH",garchOrder=c(1,1)),
              mean.model=list(armaOrder=c(0,0),include.mean=F),
              distribution.model="norm");N1
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : gjrGARCH(1,1)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(0,0,0)
## Include Mean     : FALSE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  norm 
## Includes Skew    :  FALSE 
## Includes Shape   :  FALSE 
## Includes Lambda  :  FALSE
#GJR-GARCH(1,1) Model With Student-T Distribution
S1=ugarchspec(variance.model=list(model="gjrGARCH",garchOrder=c(1,1)),
              mean.model=list(armaOrder=c(0,0),include.mean=F),
              distribution.model="std");S1
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : gjrGARCH(1,1)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(0,0,0)
## Include Mean     : FALSE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  std 
## Includes Skew    :  FALSE 
## Includes Shape   :  TRUE 
## Includes Lambda  :  FALSE
#GJR-GARCH(1,1) Model With General Error Distribution (GED)
G1=ugarchspec(variance.model=list(model="gjrGARCH",garchOrder=c(1,1)),
              mean.model=list(armaOrder=c(0,0),include.mean=F),
              distribution.model="ged");G1
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : gjrGARCH(1,1)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(0,0,0)
## Include Mean     : FALSE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  ged 
## Includes Skew    :  FALSE 
## Includes Shape   :  TRUE 
## Includes Lambda  :  FALSE
#Model Fitting
S1fit <- ugarchfit(spec = S1, data = D3, solver = "nlminb")  

#Plotting the Results
S1fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : gjrGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : std 
## 
## Convergence Problem:
## Solver Message: false convergence (8)
# Print forecast summary
print(forc)
## 
## *------------------------------------*
## *       GARCH Model Forecast         *
## *------------------------------------*
## Model: eGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
## 
## 0-roll forecast [T0=1970-04-21]:
##      Series Sigma
## T+1   17.42 1.589
## T+2   17.42 1.562
## T+3   17.42 1.540
## T+4   17.42 1.519
## T+5   17.42 1.502
## T+6   17.42 1.486
## T+7   17.42 1.473
## T+8   17.42 1.460
## T+9   17.42 1.450
## T+10  17.42 1.440
print(forc, which = 1)
## <S4 Type Object>
## attr(,"forecast")
## attr(,"forecast")$n.ahead
## [1] 10
## 
## attr(,"forecast")$N
## [1] 110
## 
## attr(,"forecast")$n.start
## [1] 0
## 
## attr(,"forecast")$n.roll
## [1] 0
## 
## attr(,"forecast")$sigmaFor
##      1970-04-21
## T+1    1.588618
## T+2    1.562481
## T+3    1.539583
## T+4    1.519489
## T+5    1.501827
## T+6    1.486282
## T+7    1.472584
## T+8    1.460500
## T+9    1.449829
## T+10   1.440399
## 
## attr(,"forecast")$seriesFor
##      1970-04-21
## T+1    17.42073
## T+2    17.42073
## T+3    17.42073
## T+4    17.42073
## T+5    17.42073
## T+6    17.42073
## T+7    17.42073
## T+8    17.42073
## T+9    17.42073
## T+10   17.42073
## 
## attr(,"model")
## attr(,"model")$modelinc
##       mu       ar       ma   arfima    archm    mxreg    omega    alpha 
##        1        0        0        0        0        0        1        1 
##     beta    gamma     eta1     eta2    delta   lambda    vxreg     skew 
##        1        1        0        0        0        0        0        0 
##    shape ghlambda       xi      aux      aux      aux 
##        0        0        0        0        1       NA 
## 
## attr(,"model")$modeldesc
## attr(,"model")$modeldesc$distribution
## [1] "norm"
## 
## attr(,"model")$modeldesc$distno
## [1] 1
## 
## attr(,"model")$modeldesc$vmodel
## [1] "eGARCH"
## 
## 
## attr(,"model")$modeldata
## attr(,"model")$modeldata$T
## [1] 110
## 
## attr(,"model")$modeldata$data
##   [1] 16.91 17.73 17.87 17.84 20.34 16.06 17.91 18.18 15.75 15.93 18.04 17.84
##  [13] 18.18 17.03 17.06 16.91 20.12 17.87 16.91 16.70 19.73 16.97 15.94 20.13
##  [25] 17.06 20.15 16.06 19.73 17.02 15.99 17.84 17.45 15.75 15.46 18.22 18.15
##  [37] 17.73 16.26 16.06 16.82 20.15 19.73 15.26 19.04 16.89 16.91 18.22 20.15
##  [49] 18.22 16.91 16.26 15.47 17.03 16.00 16.97 20.12 20.13 18.15 18.13 16.97
##  [61] 16.04 16.91 17.03 15.47 16.86 17.73 15.26 17.16 17.06 16.91 17.87 18.04
##  [73] 16.36 20.30 15.75 20.13 17.00 17.00 15.46 16.91 18.30 17.45 18.18 17.91
##  [85] 18.00 16.00 20.22 18.13 20.12 18.18 17.87 17.03 18.04 17.00 17.03 16.26
##  [97] 16.58 17.03 16.89 15.94 15.40 18.30 16.36 18.15 16.91 16.58 16.36 15.94
## [109] 17.84 16.97
## 
## attr(,"model")$modeldata$index
##   [1] "1970-01-02 UTC" "1970-01-03 UTC" "1970-01-04 UTC" "1970-01-05 UTC"
##   [5] "1970-01-06 UTC" "1970-01-07 UTC" "1970-01-08 UTC" "1970-01-09 UTC"
##   [9] "1970-01-10 UTC" "1970-01-11 UTC" "1970-01-12 UTC" "1970-01-13 UTC"
##  [13] "1970-01-14 UTC" "1970-01-15 UTC" "1970-01-16 UTC" "1970-01-17 UTC"
##  [17] "1970-01-18 UTC" "1970-01-19 UTC" "1970-01-20 UTC" "1970-01-21 UTC"
##  [21] "1970-01-22 UTC" "1970-01-23 UTC" "1970-01-24 UTC" "1970-01-25 UTC"
##  [25] "1970-01-26 UTC" "1970-01-27 UTC" "1970-01-28 UTC" "1970-01-29 UTC"
##  [29] "1970-01-30 UTC" "1970-01-31 UTC" "1970-02-01 UTC" "1970-02-02 UTC"
##  [33] "1970-02-03 UTC" "1970-02-04 UTC" "1970-02-05 UTC" "1970-02-06 UTC"
##  [37] "1970-02-07 UTC" "1970-02-08 UTC" "1970-02-09 UTC" "1970-02-10 UTC"
##  [41] "1970-02-11 UTC" "1970-02-12 UTC" "1970-02-13 UTC" "1970-02-14 UTC"
##  [45] "1970-02-15 UTC" "1970-02-16 UTC" "1970-02-17 UTC" "1970-02-18 UTC"
##  [49] "1970-02-19 UTC" "1970-02-20 UTC" "1970-02-21 UTC" "1970-02-22 UTC"
##  [53] "1970-02-23 UTC" "1970-02-24 UTC" "1970-02-25 UTC" "1970-02-26 UTC"
##  [57] "1970-02-27 UTC" "1970-02-28 UTC" "1970-03-01 UTC" "1970-03-02 UTC"
##  [61] "1970-03-03 UTC" "1970-03-04 UTC" "1970-03-05 UTC" "1970-03-06 UTC"
##  [65] "1970-03-07 UTC" "1970-03-08 UTC" "1970-03-09 UTC" "1970-03-10 UTC"
##  [69] "1970-03-11 UTC" "1970-03-12 UTC" "1970-03-13 UTC" "1970-03-14 UTC"
##  [73] "1970-03-15 UTC" "1970-03-16 UTC" "1970-03-17 UTC" "1970-03-18 UTC"
##  [77] "1970-03-19 UTC" "1970-03-20 UTC" "1970-03-21 UTC" "1970-03-22 UTC"
##  [81] "1970-03-23 UTC" "1970-03-24 UTC" "1970-03-25 UTC" "1970-03-26 UTC"
##  [85] "1970-03-27 UTC" "1970-03-28 UTC" "1970-03-29 UTC" "1970-03-30 UTC"
##  [89] "1970-03-31 UTC" "1970-04-01 UTC" "1970-04-02 UTC" "1970-04-03 UTC"
##  [93] "1970-04-04 UTC" "1970-04-05 UTC" "1970-04-06 UTC" "1970-04-07 UTC"
##  [97] "1970-04-08 UTC" "1970-04-09 UTC" "1970-04-10 UTC" "1970-04-11 UTC"
## [101] "1970-04-12 UTC" "1970-04-13 UTC" "1970-04-14 UTC" "1970-04-15 UTC"
## [105] "1970-04-16 UTC" "1970-04-17 UTC" "1970-04-18 UTC" "1970-04-19 UTC"
## [109] "1970-04-20 UTC" "1970-04-21 UTC"
## 
## attr(,"model")$modeldata$period
## Time difference of 1 days
## 
## attr(,"model")$modeldata$sigma
##   [1] 1.337851 1.378118 1.394762 1.400353 1.407532 1.252326 1.297123 1.307832
##   [9] 1.299238 1.339601 1.377176 1.372601 1.382109 1.367619 1.405714 1.440614
##  [17] 1.472035 1.324873 1.336180 1.376584 1.413063 1.295084 1.338958 1.376612
##  [25] 1.236746 1.285331 1.151197 1.203190 1.100753 1.158548 1.209863 1.231820
##  [33] 1.279637 1.321535 1.359361 1.344014 1.334637 1.354853 1.392022 1.425520
##  [41] 1.458055 1.310247 1.200305 1.246811 1.185463 1.237303 1.285453 1.275832
##  [49] 1.142387 1.142559 1.197374 1.246715 1.290390 1.334791 1.372942 1.410431
##  [57] 1.268449 1.136760 1.142080 1.148444 1.203027 1.251388 1.298482 1.342244
##  [65] 1.378407 1.415145 1.428636 1.456869 1.487467 1.514993 1.539549 1.532177
##  [73] 1.513966 1.537207 1.373247 1.407546 1.265185 1.311465 1.354112 1.389267
##  [81] 1.425204 1.399020 1.433385 1.414581 1.415744 1.410660 1.442385 1.291609
##  [89] 1.287651 1.155187 1.157294 1.180704 1.233245 1.239718 1.287924 1.332519
##  [97] 1.371533 1.408129 1.442739 1.473918 1.499815 1.521926 1.487188 1.512939
## [105] 1.489088 1.516077 1.539683 1.560469 1.578166 1.569136
## 
## attr(,"model")$modeldata$residuals
##   [1] -0.51073393  0.30926607  0.44926607  0.41926607  2.91926607 -1.36073393
##   [7]  0.48926607  0.75926607 -1.67073393 -1.49073393  0.61926607  0.41926607
##  [13]  0.75926607 -0.39073393 -0.36073393 -0.51073393  2.69926607  0.44926607
##  [19] -0.51073393 -0.72073393  2.30926607 -0.45073393 -1.48073393  2.70926607
##  [25] -0.36073393  2.72926607 -1.36073393  2.30926607 -0.40073393 -1.43073393
##  [31]  0.41926607  0.02926607 -1.67073393 -1.96073393  0.79926607  0.72926607
##  [37]  0.30926607 -1.16073393 -1.36073393 -0.60073393  2.72926607  2.30926607
##  [43] -2.16073393  1.61926607 -0.53073393 -0.51073393  0.79926607  2.72926607
##  [49]  0.79926607 -0.51073393 -1.16073393 -1.95073393 -0.39073393 -1.42073393
##  [55] -0.45073393  2.69926607  2.70926607  0.72926607  0.70926607 -0.45073393
##  [61] -1.38073393 -0.51073393 -0.39073393 -1.95073393 -0.56073393  0.30926607
##  [67] -2.16073393 -0.26073393 -0.36073393 -0.51073393  0.44926607  0.61926607
##  [73] -1.06073393  2.87926607 -1.67073393  2.70926607 -0.42073393 -0.42073393
##  [79] -1.96073393 -0.51073393  0.87926607  0.02926607  0.75926607  0.48926607
##  [85]  0.57926607 -1.42073393  2.79926607  0.70926607  2.69926607  0.75926607
##  [91]  0.44926607 -0.39073393  0.61926607 -0.42073393 -0.39073393 -1.16073393
##  [97] -0.84073393 -0.39073393 -0.53073393 -1.48073393 -2.02073393  0.87926607
## [103] -1.06073393  0.72926607 -0.51073393 -0.84073393 -1.06073393 -1.48073393
## [109]  0.41926607 -0.45073393
## 
## 
## attr(,"model")$pars
##                Level Fixed Include Estimate        LB       UB
## mu       17.42073393     0       1        1 -1744.209 1744.209
## ar        0.00000000     0       0        0        NA       NA
## ma        0.00000000     0       0        0        NA       NA
## arfima    0.00000000     0       0        0        NA       NA
## archm     0.00000000     0       0        0        NA       NA
## mxreg     0.00000000     0       0        0        NA       NA
## omega     0.06872993     0       1        1   -10.000   10.000
## alpha1   -0.06587363     0       1        1   -10.000   10.000
## beta1     0.88991519     0       1        1    -1.000    1.000
## gamma1   -0.07092741     0       1        1   -10.000   10.000
## eta1      0.00000000     0       0        0        NA       NA
## eta2      0.00000000     0       0        0        NA       NA
## delta     0.00000000     0       0        0        NA       NA
## lambda    0.00000000     0       0        0        NA       NA
## vxreg     0.00000000     0       0        0        NA       NA
## skew      0.00000000     0       0        0        NA       NA
## shape     0.00000000     0       0        0        NA       NA
## ghlambda  0.00000000     0       0        0        NA       NA
## xi        0.00000000     0       0        0        NA       NA
## 
## attr(,"model")$start.pars
## NULL
## 
## attr(,"model")$fixed.pars
## NULL
## 
## attr(,"model")$maxOrder
## [1] 1
## 
## attr(,"model")$pos.matrix
##          start stop include
## mu           1    1       1
## ar           0    0       0
## ma           0    0       0
## arfima       0    0       0
## archm        0    0       0
## mxreg        0    0       0
## omega        2    2       1
## alpha        3    3       1
## beta         4    4       1
## gamma        5    5       1
## eta1         0    0       0
## eta2         0    0       0
## delta        0    0       0
## lambda       0    0       0
## vxreg        0    0       0
## skew         0    0       0
## shape        0    0       0
## ghlambda     0    0       0
## xi           0    0       0
## aux          0    0       0
## aux          6    6       1
## 
## attr(,"model")$fmodel
## NULL
## 
## attr(,"model")$pidx
##          begin end
## mu           1   1
## ar           2   2
## ma           3   3
## arfima       4   4
## archm        5   5
## mxreg        6   6
## omega        7   7
## alpha        8   8
## beta         9   9
## gamma       10  10
## eta1        11  11
## eta2        12  12
## delta       13  13
## lambda      14  14
## vxreg       15  15
## skew        16  16
## shape       17  17
## ghlambda    18  18
## xi          19  19
## 
## attr(,"model")$n.start
## [1] 0
## 
## attr(,"class")
## [1] "uGARCHforecast"
## attr(,"class")attr(,"package")
## [1] "rugarch"
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

# (IV) Kaplan-Meir Model and Cummulative Hazard Function Analysis
#Importing Libraries
library(survival)
library(survminer)
## Warning: package 'survminer' was built under R version 4.4.3
## Loading required package: ggplot2
## Loading required package: ggpubr
## Warning: package 'ggpubr' was built under R version 4.4.3
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:forecast':
## 
##     gghistogram
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
#Dataframe
D1=data.frame(time=c(1,2,2,3,4,5,6,8,9,10),event=c(0,1,0,1,0,1,0,1,0,1))
kmc=with(D1,Surv(time,event));kmc
##  [1]  1+  2   2+  3   4+  5   6+  8   9+ 10
#Fitting the Kaplan-Meir Model
kmc2 = surv_fit(Surv(time, event) ~ 1, data = D1); kmc2
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
##     time = c(1, 2, 2, 3, 4, 5, 6, 8, 9, 10), event = c(0, 1, 
##     0, 1, 0, 1, 0, 1, 0, 1)), class = "data.frame", row.names = c(NA, 
## -10L)))
## 
##       n events median 0.95LCL 0.95UCL
## [1,] 10      5      8       5      NA
summary(kmc2)
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
##     time = c(1, 2, 2, 3, 4, 5, 6, 8, 9, 10), event = c(0, 1, 
##     0, 1, 0, 1, 0, 1, 0, 1)), class = "data.frame", row.names = c(NA, 
## -10L)))
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     2      9       1    0.889   0.105        0.706            1
##     3      7       1    0.762   0.148        0.521            1
##     5      5       1    0.610   0.181        0.341            1
##     8      3       1    0.406   0.205        0.151            1
##    10      1       1    0.000     NaN           NA           NA
#Plotting Kaplan-Meir curve
plot(kmc, 
     col = "blue",           
     lwd = 2,                  
     xlab = "Time",            
     ylab = "Survival Probability",  
     main = "Kaplan-Meier Curve") 

# Plotting cumulative hazard 
grid()
plot(
  kmc2,
  fun = "cumhaz",      
  col = "purple",
  lwd = 2,
  xlab = "Time (Days)",
  ylab = "Cumulative Hazard",
  main = "Cumulative Hazard Function"
)

#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------

#Data Analysis and Plotting in RStudio; First Class
setwd("C:\\Users\\derek\\OneDrive\\Documents\\DDMA")
colortaste_data<-read.csv('colortaste.csv')
head(colortaste_data)
##   X  color taste weight size eaten
## 1 1    red sweet    1.5  5.7 FALSE
## 2 2   blue  sour    1.6  6.8  TRUE
## 3 3  green  sour    1.7  6.2 FALSE
## 4 4 orange salty    1.5  5.6 FALSE
## 5 5 purple salty    1.7  5.9 FALSE
## 6 6 orange salty    1.6  5.4  TRUE
# Scatterplot of Size vs Weight
plot(colortaste_data$size,colortaste_data$weight,
     xlab='size',ylab='weight',
     main='scatteplot of size against weight')

# Aggregate the mean size by color
mean_size <- tapply(colortaste_data$size, colortaste_data$color, mean)

#Bar Plot
barplot(mean_size,
        col = 'blue',
        xlab = "Color",
        ylab = "Average Size",
        main = "Bar Plot of Average Size by Color")

#histogram
hist(colortaste_data$weight,
     col = 'green',
     xlab = 'weight',ylab='frequency',
     main = 'histogram of weiight frequency')

#-------------------------------------------------------------------------------
#THE END
#-------------------------------------------------------------------------------