#Packages yang digunakan

library(survival)
library(KMsurv)
## Warning: package 'KMsurv' was built under R version 4.5.2

#Analisis Kaplan-Meier untuk covariate catheter type

data(kidney)
attach(kidney)
kmfit1 <- survfit(Surv(time, delta)~type)
plot(kmfit1, lty=1:2, xlab="Waktu hingga infeksi terjadi (bulan)", ylab="Fungsi survival")
leg.names <- c("surgical", "percutaneous")
legend(.8, .3, leg.names, lty=1:2, bty="n")
title(main = "")

#Regresi Cox-PH

model.CoxPH <- coxph(Surv(time,delta)~as.factor(type), data = kidney, method = "efron")
model.CoxPH
## Call:
## coxph(formula = Surv(time, delta) ~ as.factor(type), data = kidney, 
##     method = "efron")
## 
##                     coef exp(coef) se(coef)      z     p
## as.factor(type)2 -0.6126    0.5420   0.3979 -1.539 0.124
## 
## Likelihood ratio test=2.41  on 1 df, p=0.1207
## n= 119, number of events= 26

Hasil regresi Cox menunjukkan bahwa jenis type kategori 2 memiliki hazard ratio sebesar 0,542, yang berarti risikonya sekitar 45,8% lebih rendah dibandingkan kategori referensi (kategori 1). Namun, pengaruh tersebut tidak signifikan secara statistik (p = 0,124), sehingga belum terdapat bukti yang cukup bahwa jenis type berpengaruh terhadap risiko kejadian pada taraf signifikansi 5%.

#Regresi Extended Cox dengan Fungsi log(t)

time <- kidney$time+0.5
stop.int <- as.numeric(unlist(dimnames(table(time))))
start.int <- c(0,stop.int[-length(stop.int)])
npatient <- length(time)
nrep <- rep(0, npatient)
for (i in 1:npatient) {
  nrep[i]<-sum(stop.int<=time[i])
}
start.int1 <- start.int[1:nrep[1]]
stop.int1 <- stop.int[1:nrep[1]]
for (i in 2:npatient) {
  start.int1<-c(start.int1, start.int[1:nrep[i]])
  stop.int1<-c(stop.int1, stop.int[1:nrep[i]])
}
type1 <- rep(type,nrep)
delta1 <- rep(0, length(stop.int1))
delta1[cumsum(nrep)] <- delta

Time dependent covariate dengan covariate cathether type

t.type1 <- stop.int1*type1
logt.type1 <- log(stop.int1)*type1

#Membentuk model untuk yang time dependent covariate (extended cox model dengan fungsi waktu, g(t)=log t)

Coxph.logt <- coxph(Surv(start.int1,stop.int1,delta1)~type1+logt.type1, method = "efron")
Coxph.logt
## Call:
## coxph(formula = Surv(start.int1, stop.int1, delta1) ~ type1 + 
##     logt.type1, method = "efron")
## 
##               coef exp(coef) se(coef)      z      p
## type1       2.0904    8.0880   1.1889  1.758 0.0787
## logt.type1 -1.7275    0.1777   0.6719 -2.571 0.0101
## 
## Likelihood ratio test=14.52  on 2 df, p=0.0007021
## n= 1132, number of events= 26

Hasil model extended Cox menunjukkan bahwa efek interaksi antara variabel type1 dan waktu (log t) berpengaruh signifikan (p = 0,0101), sehingga mengindikasikan bahwa pengaruh type1 terhadap risiko kejadian berubah seiring waktu dan asumsi Proportional Hazards tidak terpenuhi.

Regresi Extended Cox dengan fungsi heaviside

time <- kidney$time
delta.times <- as.numeric(unlist(dimnames(table(time[kidney$delta==1]))))
n.etimes <- length(delta.times)
logliks <- rep(0, n.etimes)
for (i in 1:n.etimes) {
  t.type2 <- type1
  t.type2[stop.int1<=delta.times[i]] < 0
  ph.cp <- coxph(Surv(start.int1,stop.int1,delta1)~type1+t.type2, method = "efron")
  logliks[i] <- ph.cp$loglik[2]
}
t.type2 <- type1
t.type2[stop.int1<=delta.times[4]] <- 0
ph.cp <- coxph(Surv(start.int1,stop.int1, delta1)~type1+t.type2, method = "efron")
ph.cp
## Call:
## coxph(formula = Surv(start.int1, stop.int1, delta1) ~ type1 + 
##     t.type2, method = "efron")
## 
##             coef exp(coef) se(coef)      z       p
## type1    1.62852   5.09633  1.06140  1.534 0.12495
## t.type2 -3.38902   0.03374  1.23755 -2.739 0.00617
## 
## Likelihood ratio test=13.83  on 2 df, p=0.0009946
## n= 1132, number of events= 26
cbind(delta.times,logliks)
##       delta.times   logliks
##  [1,]         0.5 -103.0278
##  [2,]         1.5 -103.0278
##  [3,]         2.5 -103.0278
##  [4,]         3.5 -103.0278
##  [5,]         4.5 -103.0278
##  [6,]         5.5 -103.0278
##  [7,]         6.5 -103.0278
##  [8,]         8.5 -103.0278
##  [9,]         9.5 -103.0278
## [10,]        10.5 -103.0278
## [11,]        11.5 -103.0278
## [12,]        15.5 -103.0278
## [13,]        16.5 -103.0278
## [14,]        18.5 -103.0278
## [15,]        23.5 -103.0278
## [16,]        26.5 -103.0278

Hasil model extended Cox menunjukkan bahwa interaksi type2 dengan waktu berpengaruh signifikan (p = 0,00617), yang mengindikasikan bahwa pengaruh variabel type terhadap risiko kejadian berubah seiring waktu, sehingga asumsi Proportional Hazards tidak terpenuhi.

#Model yang cocok untuk data

t.type2[stop.int1<=delta.times[5]]<-0
cox.piecewise <- coxph(Surv(start.int1, stop.int1, delta1)~type1+t.type2, method = "efron")
summary(cox.piecewise)
## Call:
## coxph(formula = Surv(start.int1, stop.int1, delta1) ~ type1 + 
##     t.type2, method = "efron")
## 
##   n= 1132, number of events= 26 
## 
##             coef exp(coef) se(coef)      z Pr(>|z|)   
## type1    1.10023   3.00485  0.78294  1.405  0.15995   
## t.type2 -3.19436   0.04099  1.09095 -2.928  0.00341 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##         exp(coef) exp(-coef) lower .95 upper .95
## type1     3.00485     0.3328  0.647709   13.9401
## t.type2   0.04099    24.3946  0.004832    0.3478
## 
## Concordance= 0.651  (se = 0.041 )
## Likelihood ratio test= 14.06  on 2 df,   p=9e-04
## Wald test            = 9.57  on 2 df,   p=0.008
## Score (logrank) test = 12.99  on 2 df,   p=0.002

Hasil model Piecewise Cox menunjukkan bahwa interaksi type2 dengan waktu berpengaruh signifikan terhadap risiko kejadian (p = 0,00341; HR = 0,04099), sedangkan type1 tidak berpengaruh signifikan (p = 0,15995); hasil ini mengindikasikan bahwa pengaruh variabel type terhadap risiko kejadian berubah pada interval waktu tertentu sehingga model Piecewise Cox dapat mengakomodasi pelanggaran asumsi Proportional Hazards.

#Perbandingan model Cox-Ph dengan extended Cox (time dependent)*

model.CoxPH <- coxph(Surv(time,delta)~as.factor(type), data=kidney, method="efron")
Cox.logt <- coxph(Surv(start.int1, stop.int1, delta1)~type1+logt.type1, method = "efron")

Nilai Log Likelihood

loglik.CoxPH <- model.CoxPH$loglik[2]
loglik.Cox.logt <- Cox.logt$loglik[2]

Hitung Statistik Uji

chi2.hitung<--2*(loglik.CoxPH - loglik.Cox.logt)

#Nilai p-value untuk uji ratio likelihood

pval.logt<-1-pchisq(chi2.hitung,1)
pval.logt
## [1] 0.0005002196

#Perbandingan model Cox-PH dengan extended Cox (heaviside)*

model.CoxPH <- coxph(Surv(time,delta)~as.factor(type), data=kidney, method="efron")
Cox.piecewise <- coxph(Surv(start.int1, stop.int1, delta1)~type1+t.type2, method = "efron")

Nilai Log Likelihood

loglik.CoxPH <- model.CoxPH$loglik[2]
loglik.Cox.piecewise <- Cox.piecewise$loglik[2]

Hitung Statistik Uji

chi2.hitung<--2*(loglik.CoxPH - loglik.Cox.piecewise)

Nilai p-value untuk uji ratio likelihood

pval.logt<-1-pchisq(chi2.hitung,1)
pval.logt
## [1] 0.0006410422