Survival

library(survival)
d <- read.csv("SCOM.csv")
head(d)
##         DATE ISSUER DEALS        VOLUME       TURNOVER MAX.PRICE MIN.PRICE
## 1 02/01/2019   SCOM   155  1,778,397.00  39,637,648.40     23.00     22.10
## 2 03/01/2019   SCOM   288 12,255,996.00 271,459,760.40     22.60     21.00
## 3 04/01/2019   SCOM   181  2,210,307.00  47,113,391.10     22.00     20.10
## 4 07/01/2019   SCOM   187  6,283,857.00 134,894,261.40     22.00     21.30
## 5 08/01/2019   SCOM   102  1,532,800.00  33,720,640.00     22.20     21.50
## 6 09/01/2019   SCOM   368 21,758,774.00 485,471,249.60     22.75     22.05
##   AVG.PRICE Closing.Price
## 1     22.36         22.25
## 2     22.13         22.15
## 3     21.39         21.30
## 4     21.51         21.45
## 5     21.90         22.00
## 6     22.33         22.30
Max <- d$MAX.PRICE
min <- d$MIN.PRICE
closing <- d$Closing.Price

hist(Max)

hist(min)

hist(closing)

barplot(Max)

barplot(min)

barplot(closing)

### Regression

x <- d$MAX.PRICE
y <- d$MIN.PRICE
z <- d$Closing.Price
h <- d$volume

summary(lm(x ~ y))
## 
## Call:
## lm(formula = x ~ y)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3034 -0.6291 -0.1917  0.4126  3.8963 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.90478    0.07021   12.89   <2e-16 ***
## y            1.01662    0.00254  400.27   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8468 on 1493 degrees of freedom
## Multiple R-squared:  0.9908, Adjusted R-squared:  0.9908 
## F-statistic: 1.602e+05 on 1 and 1493 DF,  p-value: < 2.2e-16
summary(lm(y ~ z))
## 
## Call:
## lm(formula = y ~ z)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9987 -0.6667  0.0102  0.5254  6.9549 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.461385   0.109983   4.195 2.89e-05 ***
## z           0.961503   0.003892 247.057  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.333 on 1493 degrees of freedom
## Multiple R-squared:  0.9761, Adjusted R-squared:  0.9761 
## F-statistic: 6.104e+04 on 1 and 1493 DF,  p-value: < 2.2e-16
summary(lm(x ~ z))
## 
## Call:
## lm(formula = x ~ z)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.7130 -0.8313 -0.0848  0.7865  7.8276 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.339324   0.126630   10.58   <2e-16 ***
## z           0.978769   0.004481  218.43   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.535 on 1493 degrees of freedom
## Multiple R-squared:  0.9697, Adjusted R-squared:  0.9696 
## F-statistic: 4.771e+04 on 1 and 1493 DF,  p-value: < 2.2e-16
summary(lm(x ~ y + z))
## 
## Call:
## lm(formula = x ~ y + z)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.3834 -0.6151 -0.1825  0.4120  3.7987 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.89550    0.07002  12.790  < 2e-16 ***
## y            0.96194    0.01638  58.727  < 2e-16 ***
## z            0.05386    0.01594   3.379 0.000747 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8439 on 1492 degrees of freedom
## Multiple R-squared:  0.9908, Adjusted R-squared:  0.9908 
## F-statistic: 8.067e+04 on 2 and 1492 DF,  p-value: < 2.2e-16

Correlation

cor(x, y)
## [1] 0.995373
cor(y, z)
## [1] 0.9879896
cor(x, z)
## [1] 0.984712
aov(lm(y ~ x))
## Call:
##    aov(formula = lm(y ~ x))
## 
## Terms:
##                         x Residuals
## Sum of Squares  110136.31   1026.33
## Deg. of Freedom         1      1493
## 
## Residual standard error: 0.8291112
## Estimated effects may be unbalanced
summary(lm(y ~ x))
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.8182 -0.4459  0.1898  0.6018  1.3895 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.639294   0.070549  -9.062   <2e-16 ***
## x            0.974570   0.002435 400.269   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8291 on 1493 degrees of freedom
## Multiple R-squared:  0.9908, Adjusted R-squared:  0.9908 
## F-statistic: 1.602e+05 on 1 and 1493 DF,  p-value: < 2.2e-16
plot(lm(y ~ x))

hist(x)

### fGarch

# Load GARCH library (make sure it's installed already)
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(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 objects are masked from 'package:graphics':
## 
##     lines, points

Timeseries

log.z <- diff(z)
plot(log.z)

ts.plot(log.z)

acf(log.z)

pacf(log.z)

s <- log.z^2
m2 <- garchFit(~garch(1,1), data=log.z, trace=FALSE)
summary(m2)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 1), data = log.z, trace = FALSE) 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 1)
## <environment: 0x000001f975a116b8>
##  [data = log.z]
## 
## Conditional Distribution:
##  norm 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1  
## -0.0019777   0.0228430   0.2212501   0.6903999  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)    
## mu     -0.001978    0.010372   -0.191 0.848784    
## omega   0.022843    0.006363    3.590 0.000331 ***
## alpha1  0.221250    0.041738    5.301 1.15e-07 ***
## beta1   0.690400    0.058122   11.878  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -830.5222    normalized:  -0.5559051 
## 
## Description:
##  Wed May  7 08:35:05 2025 by user: Brian Juma 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2  931.343560 0.000000e+00
##  Shapiro-Wilk Test  R    W        0.942090 0.000000e+00
##  Ljung-Box Test     R    Q(10)   77.600581 1.480038e-12
##  Ljung-Box Test     R    Q(15)   84.752142 9.347967e-12
##  Ljung-Box Test     R    Q(20)   90.167630 6.929035e-11
##  Ljung-Box Test     R^2  Q(10)    7.724979 6.556804e-01
##  Ljung-Box Test     R^2  Q(15)   11.996060 6.793273e-01
##  Ljung-Box Test     R^2  Q(20)   22.493352 3.143508e-01
##  LM Arch Test       R    TR^2     9.628747 6.484940e-01
## 
## Information Criterion Statistics:
##      AIC      BIC      SIC     HQIC 
## 1.117165 1.131380 1.117151 1.122461
plot(m2, which = 1)  # Series and conditional SD

plot(m2, which = 2)  # ACF of standardized residuals

plot(m2, which = 3)  # Normal Q-Q plot

plot(m2, which = 4)  # P-values for Ljung-Box statistic

### rugarch install.packages(“rugarch”, dependencies = TRUE)

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
# Define the model specification
z <- d$Closing.Price
D3 <- diff(tail(z, 200))
m2 <- ugarchspec(
  variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
  mean.model = list(armaOrder = c(0, 0), include.mean = FALSE),
  distribution.model = "norm"
)

# Fit the model to the data

m2fit <- ugarchfit(spec = m2, data = D3)

f <- ugarchforecast(m2fit, n.ahead = 120)
plot(f, which = 1)

#kaplan meier

library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
d1=data.frame(time=c(2,10,12,18,4),event=c(1,0,1,1,0))
kmc=with(d1,Surv(time,event));
kmc
## [1]  2  10+ 12  18   4+
plot(kmc)

plot(kmc,fun="cumhaz")

kmc2=survfit(Surv(time,event)~1,data=d1);kmc2
## Call: survfit(formula = Surv(time, event) ~ 1, data = d1)
## 
##      n events median 0.95LCL 0.95UCL
## [1,] 5      3     12      12      NA
summary(kmc2)
## Call: survfit(formula = Surv(time, event) ~ 1, data = d1)
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     2      5       1      0.8   0.179       0.5161            1
##    12      2       1      0.4   0.297       0.0935            1
##    18      1       1      0.0     NaN           NA           NA