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)
## Warning: package 'timeSeries' was built under R version 4.4.3
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 4.4.3
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:rugarch':
## 
##     quantile
## The following objects are masked from 'package:graphics':
## 
##     lines, points
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")
data <- read.csv("DATA25.csv")
head(data)
##   Year    Month STOCK  GDP  CPI PENSION INFLATION CLAIMS INTEREST
## 1 2025    April  3.74 4.11 6.12   13.50      5.58   4.59     4.12
## 2 2025    March  3.81 3.62 6.33   13.59      5.53   4.53     3.97
## 3 2025 February  3.98 3.45 6.47   13.49      5.70   4.53     4.07
## 4 2025  January  4.21 3.28 6.48   13.47      4.04   4.63     4.20
## 5 2024 December  4.50 2.99 6.51   13.70      4.35   4.95     4.44
## 6 2024 November  4.81 2.75 6.56   13.73      4.28   5.20     4.62
D1 <- data$STOCK
D1
##   [1] 3.74 3.81 3.98 4.21 4.50 4.81 5.14 5.50 5.77 5.97 4.64 6.49 6.73 6.97 7.26
##  [16] 7.50 7.67 7.87 8.10 8.32 8.52 8.68 8.77 8.78 8.71 8.59 8.30 7.95 7.66 7.38
##  [31] 7.48 6.81 6.61 6.45 6.29 6.16 6.05 6.29 6.23 6.08 5.62 6.10 6.07 5.35 5.71
##  [46] 5.53 5.35 5.20 4.66 5.17 5.16 5.74 5.41 5.53 5.67 5.79 5.87 6.01 6.16 6.18
##  [61] 6.03 5.84 5.72 5.29 5.20 5.19 5.19 5.24 5.40 5.32 5.16 5.04 4.91 4.91 4.67
##  [76] 4.65 4.68 4.69 4.59 4.53 4.53 4.63 4.95 5.20 5.61 6.24 6.89 7.40 7.79 7.98
##  [91] 8.15 8.33 8.40 8.36 8.21 8.13 7.84 7.20 6.76 6.43 6.26 6.30 6.43 6.48 6.50
## [106] 6.47 6.44 6.46 6.59 6.72 6.88 6.87 6.77 6.58 6.42 6.31 6.29 6.34 6.54
plot(D1, main = "Raw Log Returns", ylab = "Log Returns", col = "blue")

ts.plot(D1, main = "Time Series of Log Returns", ylab = "Log Returns")

M1 <- garchFit(formula = ~garch(1,2), data = D1, cond.dist = "snorm")
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 2)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 2
##  Max GARCH Order:           2
##  Maximum Order:             2
##  Conditional Dist:          snorm
##  h.start:                   3
##  llh.start:                 1
##  Length of Series:          119
##  Recursion Init:            mci
##  Series Scale:              1.239203
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                       U         V   params includes
##     mu     -50.45123778  50.45124 5.045124     TRUE
##     omega    0.00000100 100.00000 0.100000     TRUE
##     alpha1   0.00000001   1.00000 0.100000     TRUE
##     gamma1  -0.99999999   1.00000 0.100000    FALSE
##     beta1    0.00000001   1.00000 0.400000     TRUE
##     beta2    0.00000001   1.00000 0.400000     TRUE
##     delta    0.00000000   2.00000 2.000000    FALSE
##     skew     0.10000000  10.00000 1.000000     TRUE
##     shape    1.00000000  10.00000 4.000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1  beta2   skew 
##      1      2      3      5      6      8 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     159.15632:  5.04512 0.100000 0.100000 0.400000 0.400000  1.00000
##   1:     154.96272:  4.98105 0.0795523 0.0970025 0.379120 0.377975  1.00139
##   2:     145.29726:  4.63034 0.0387274 0.219075 0.347994 0.336366  1.01042
##   3:     143.77976:  4.74054 0.0134761 0.226519 0.331240 0.315731  1.00675
##   4:     141.53780:  4.76701 0.0417582 0.255220 0.323517 0.303153  1.00215
##   5:     133.69533:  4.94410 0.0181346 0.429541 0.246389 0.179869 0.960476
##   6:     133.50531:  4.98232 0.0704934 0.435647 0.230336 0.153306 0.941103
##   7:     131.72265:  4.88518 0.0496979 0.440950 0.226620 0.142772 0.931741
##   8:     131.28133:  4.85010 0.0302994 0.460043 0.223834 0.133610 0.918981
##   9:     130.43826:  4.82684 0.0436768 0.473522 0.232860 0.138055 0.895305
##  10:     121.85339:  4.66564 0.0352493 0.514441 0.237341 0.0964051 0.596775
##  11:     120.54503:  4.64756 0.0382866 0.520446 0.212082 0.105740 0.540769
##  12:     119.09007:  4.49829 0.0667699 0.558318 0.225412 0.116121 0.530353
##  13:     116.64726:  4.56949 0.0395345 0.576797 0.208926 0.103286 0.487975
##  14:     115.74709:  4.49852 0.0690692 0.618864 0.212452 0.107128 0.462521
##  15:     114.93566:  4.47834 0.0440602 0.627573 0.186266 0.0946344 0.418297
##  16:     114.86322:  4.62883 0.0738977 0.651082 0.165184 0.0727406 0.402222
##  17:     112.88807:  4.53560 0.0667130 0.656119 0.168660 0.0616501 0.420254
##  18:     112.87831:  4.45035 0.0711361 0.676014 0.161911 0.0571075 0.409690
##  19:     112.26814:  4.51813 0.0761797 0.679580 0.161745 0.0566190 0.410548
##  20:     112.05904:  4.49713 0.0703083 0.686370 0.158548 0.0473171 0.415549
##  21:     111.88938:  4.53934 0.0724036 0.692093 0.160213 0.0416226 0.424344
##  22:     111.66557:  4.51111 0.0732520 0.697332 0.153546 0.0395645 0.413784
##  23:     111.50435:  4.54359 0.0796713 0.705889 0.148215 0.0363240 0.408796
##  24:     111.26317:  4.51885 0.0761330 0.711066 0.147511 0.0278785 0.417987
##  25:     111.10233:  4.55255 0.0809214 0.720895 0.143365 0.0222535 0.415400
##  26:     110.86668:  4.51781 0.0817139 0.723809 0.136015 0.0215723 0.404977
##  27:     110.64654:  4.54396 0.0866924 0.732214 0.129776 0.0144392 0.407730
##  28:     110.43833:  4.52176 0.0851346 0.737968 0.125037 0.00520194 0.415279
##  29:     110.23597:  4.54026 0.0910233 0.743198 0.118188 0.00407577 0.405481
##  30:     110.10299:  4.50936 0.0925877 0.747645 0.110045 0.00131918 0.396205
##  31:     109.87975:  4.54078 0.0994268 0.753305 0.100604 1.00000e-08 0.394370
##  32:     109.65682:  4.52027 0.0998938 0.759605 0.0879943 1.00000e-08 0.396429
##  33:     109.48203:  4.53711 0.105041 0.766279 0.0779378 1.00000e-08 0.390400
##  34:     109.30219:  4.51548 0.107267 0.770518 0.0670727 1.00000e-08 0.382633
##  35:     108.39293:  4.53836 0.117359 0.822909 1.00000e-08 1.00000e-08 0.406651
##  36:     108.04285:  4.51276 0.119590 0.848459 1.00000e-08 1.00000e-08 0.383358
##  37:     108.03885:  4.54440 0.119655 0.848841 1.00000e-08 1.00000e-08 0.381788
##  38:     107.98549:  4.53050 0.118036 0.849003 1.00000e-08 1.00000e-08 0.381290
##  39:     107.96994:  4.53400 0.115699 0.849828 1.00000e-08 1.00000e-08 0.379324
##  40:     107.96334:  4.53731 0.115749 0.852615 1.00000e-08 1.00000e-08 0.377814
##  41:     107.95833:  4.53552 0.114816 0.855676 1.00000e-08 1.00000e-08 0.378163
##  42:     107.95505:  4.54068 0.113022 0.857485 1.00000e-08 1.00000e-08 0.379880
##  43:     107.95136:  4.54351 0.112023 0.864075 1.00000e-08 1.00000e-08 0.380209
##  44:     107.95084:  4.54334 0.110830 0.865235 1.00000e-08 1.00000e-08 0.381000
##  45:     107.95059:  4.54464 0.110764 0.865188 1.00000e-08 1.00000e-08 0.380598
##  46:     107.95058:  4.54450 0.110813 0.865252 1.00000e-08 1.00000e-08 0.380731
##  47:     107.95058:  4.54447 0.110816 0.865229 1.00000e-08 1.00000e-08 0.380697
##  48:     107.95058:  4.54448 0.110813 0.865233 1.00000e-08 1.00000e-08 0.380701
## 
## Final Estimate of the Negative LLH:
##  LLH:  133.4723    norm LLH:  1.121616 
##         mu      omega     alpha1      beta1      beta2       skew 
## 5.63153825 0.17016682 0.86523311 0.00000001 0.00000001 0.38070103 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                mu      omega     alpha1      beta1      beta2       skew
## mu     -244.05355  -337.8425   13.07284 -101.44793 -156.14286  -11.06279
## omega  -337.84250 -1040.4837 -150.07729 -379.41079 -560.92880 -319.41019
## alpha1   13.07284  -150.0773 -212.82810 -166.33298 -141.20029   36.78071
## beta1  -101.44793  -379.4108 -166.33298 -238.90843 -251.99424   28.72531
## beta2  -156.14286  -560.9288 -141.20029 -251.99424 -323.51758  -43.53064
## skew    -11.06279  -319.4102   36.78071   28.72531  -43.53064 -666.98227
## attr(,"time")
## Time difference of 0.0167501 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## 
## Time to Estimate Parameters:
##  Time difference of 0.08739901 secs
summary(M1)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = D1, cond.dist = "snorm") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x000001efac6777a0>
##  [data = D1]
## 
## Conditional Distribution:
##  snorm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1       beta2        skew  
## 5.63153825  0.17016682  0.86523311  0.00000001  0.00000001  0.38070103  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     5.632e+00   8.563e-02   65.765  < 2e-16 ***
## omega  1.702e-01         NaN      NaN      NaN    
## alpha1 8.652e-01   1.303e-01    6.638 3.18e-11 ***
## beta1  1.000e-08   2.234e-01    0.000        1    
## beta2  1.000e-08         NaN      NaN      NaN    
## skew   3.807e-01   2.235e-02   17.036  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -133.4723    normalized:  -1.121616 
## 
## Description:
##  Fri May 30 11:45:20 2025 by user: Brian Juma 
## 
## 
## Standardised Residuals Tests:
##                                   Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2   13.3817145 1.242217e-03
##  Shapiro-Wilk Test  R    W        0.8651981 5.225923e-09
##  Ljung-Box Test     R    Q(10)  335.9525493 0.000000e+00
##  Ljung-Box Test     R    Q(15)  336.3501854 0.000000e+00
##  Ljung-Box Test     R    Q(20)  339.8315722 0.000000e+00
##  Ljung-Box Test     R^2  Q(10)   39.7333261 1.887816e-05
##  Ljung-Box Test     R^2  Q(15)   44.9315362 7.850087e-05
##  Ljung-Box Test     R^2  Q(20)   48.1981224 3.989977e-04
##  LM Arch Test       R    TR^2    36.7745879 2.430013e-04
## 
## Information Criterion Statistics:
##      AIC      BIC      SIC     HQIC 
## 2.344073 2.484197 2.339306 2.400973
spec <- ugarchspec(
  variance.model = list(model = "eGARCH", garchOrder = c(1,2)),
  mean.model = list(armaOrder = c(1,2), include.mean = FALSE),
  distribution.model = "std"
)
spec
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : eGARCH(1,2)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(1,0,2)
## Include Mean     : FALSE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  std 
## Includes Skew    :  FALSE 
## Includes Shape   :  TRUE 
## Includes Lambda  :  FALSE
fit <- ugarchfit(spec = spec, data = D1)
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->warning: solver failer to converge.
fit
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,2)
## Mean Model   : ARFIMA(1,0,2)
## Distribution : std 
## 
## Convergence Problem:
## Solver Message:

Load Libraries and Data

library(survival)
data(package = "survival")
str(aml)
## 'data.frame':    23 obs. of  3 variables:
##  $ time  : num  9 13 13 18 23 28 31 34 45 48 ...
##  $ status: num  1 1 0 1 1 0 1 1 0 1 ...
##  $ x     : Factor w/ 2 levels "Maintained","Nonmaintained": 1 1 1 1 1 1 1 1 1 1 ...
head(aml)
##   time status          x
## 1    9      1 Maintained
## 2   13      1 Maintained
## 3   13      0 Maintained
## 4   18      1 Maintained
## 5   23      1 Maintained
## 6   28      0 Maintained
surv_object <- Surv(time = aml$time, event = aml$status)
km_fit <- survfit(surv_object ~ x, data = aml)
print(km_fit)
## Call: survfit(formula = surv_object ~ x, data = aml)
## 
##                  n events median 0.95LCL 0.95UCL
## x=Maintained    11      7     31      18      NA
## x=Nonmaintained 12     11     23       8      NA
summary(km_fit)
## Call: survfit(formula = surv_object ~ x, data = aml)
## 
##                 x=Maintained 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     9     11       1    0.909  0.0867       0.7541        1.000
##    13     10       1    0.818  0.1163       0.6192        1.000
##    18      8       1    0.716  0.1397       0.4884        1.000
##    23      7       1    0.614  0.1526       0.3769        0.999
##    31      5       1    0.491  0.1642       0.2549        0.946
##    34      4       1    0.368  0.1627       0.1549        0.875
##    48      2       1    0.184  0.1535       0.0359        0.944
## 
##                 x=Nonmaintained 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     12       2   0.8333  0.1076       0.6470        1.000
##     8     10       2   0.6667  0.1361       0.4468        0.995
##    12      8       1   0.5833  0.1423       0.3616        0.941
##    23      6       1   0.4861  0.1481       0.2675        0.883
##    27      5       1   0.3889  0.1470       0.1854        0.816
##    30      4       1   0.2917  0.1387       0.1148        0.741
##    33      3       1   0.1944  0.1219       0.0569        0.664
##    43      2       1   0.0972  0.0919       0.0153        0.620
##    45      1       1   0.0000     NaN           NA           NA
plot(km_fit,
     xlab = "Time (Weeks)",
     ylab = "Survival Probability",
     col = c("blue", "red"),
     lty = c(1, 2),
     main = "Kaplan-Meier Survival Curves for AML Patients by Treatment",
     mark.time = TRUE)
legend("topright",
       legend = levels(aml$x),
       col = c("blue", "red"),
       lty = c(1, 2))

plot(km_fit,
     xlab = "Time (Weeks)",
     ylab = "Survival Probability",
     col = c("blue", "red"),
     lty = c(1, 2),
     main = "Kaplan-Meier Survival Curves with Confidence Intervals",
     mark.time = TRUE,
     conf.int = TRUE)
legend("topright",
       legend = levels(aml$x),
       col = c("blue", "red"),
       lty = c(1, 2))

surv_diff <- survdiff(surv_object ~ x, data = aml)
print(surv_diff)
## Call:
## survdiff(formula = surv_object ~ x, data = aml)
## 
##                  N Observed Expected (O-E)^2/E (O-E)^2/V
## x=Maintained    11        7    10.69      1.27       3.4
## x=Nonmaintained 12       11     7.31      1.86       3.4
## 
##  Chisq= 3.4  on 1 degrees of freedom, p= 0.07
## Load Required Libraries
library(goftest)
data <- read.csv("DATA25.csv")
claims <- data$CLAIMS
D1 <- diff(claims)
D1
##   [1] -0.06  0.00  0.10  0.32  0.25  0.41  0.63  0.65  0.51  0.39  0.19  0.17
##  [13]  0.18  0.07 -0.04 -0.15 -0.08 -0.29 -0.64 -0.44 -0.33 -0.17  0.04  0.13
##  [25]  0.05  0.02 -0.03 -0.03  0.02  0.13  0.13  0.16 -0.01 -0.10 -0.19 -0.16
##  [37] -0.11 -0.02  0.05  0.20  0.09  0.02  0.04 -0.06  0.00  0.11  0.14  0.09
##  [49]  0.11  0.11  0.14 -0.14 -0.14 -0.20 -0.27 -0.19 -0.18 -0.20 -0.29 -0.33
##  [61] -0.34 -0.30 -0.25 -0.06  0.12  0.40  0.65  0.72  0.91  0.96  1.18  1.29
##  [73]  1.37  1.25  1.04  0.94  0.70  0.43  0.10 -0.05 -0.52 -0.83 -1.08 -1.20
##  [85] -1.33 -1.31 -1.18 -1.12 -1.00 -0.92 -0.76 -0.71 -0.44 -0.12  0.03  0.06
##  [97]  0.10  0.28  0.29  0.34  0.40  0.42  0.47  0.71  0.85  0.76  0.60  1.00
## [109]  1.18  0.99  1.01  0.93  0.76  0.82  0.79  0.35 -0.20 -0.31
D1_positive <- D1[D1 > 0]