0% found this document useful (0 votes)
171 views12 pages

Reliability Theory and Survival Analysis Final

- The document describes various parametric regression models used in survival analysis including the Cox proportional hazards model, exponential, Weibull, gamma, and lognormal distributions. It provides the formulas and explains how to fit each model in R using maximum likelihood estimation. Examples of fitting each model to simulated survival data are shown.

Uploaded by

Ayushi jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views12 pages

Reliability Theory and Survival Analysis Final

- The document describes various parametric regression models used in survival analysis including the Cox proportional hazards model, exponential, Weibull, gamma, and lognormal distributions. It provides the formulas and explains how to fit each model in R using maximum likelihood estimation. Examples of fitting each model to simulated survival data are shown.

Uploaded by

Ayushi jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Reliability Theory and Survival Analysis

AYUSHI JAIN/19STM21/GL6382

27 May 2021

Q.1:-Describe CoxPh model and its fitting?


Ans:- COX PROPORTIONAL HAZARDS MODEL (CoxPH): The Cox proportional-hazards
model (Cox, 1972) is essentially a regression model commonly used statistical in medical
research for investigating the association between the survival time of patients and one or
more predictor variables.
The purpose of the model is to evaluate simultaneously the effect of several factors on
survival. In other words, it allows us to examine how specified factors influence the rate of
a particular event happening (e.g., infection, death) at a particular point in time. This rate is
commonly referred as the hazard rate. Predictor variables (or factors) are usually termed
covariates in the survival-analysis literature.
The Cox model is expressed by the hazard function denoted by h(t). Briefly, the hazard
function can be interpreted as the risk of dying at time t. It can be estimated as follow:

ℎ(𝑡) = ℎ0 (𝑡) ∗ exp(𝑏1 𝑥1 + 𝑏2 𝑥2 +. . . . +𝑏𝑝 𝑥𝑝 )

where,
->t represents the survival time
->h(t) is the hazard function determined by a set of p covariates (x1,x2,...,xp)
->the coefficients (b1,b2,...,bp) measure the impact (i.e., the effect size) of covariates.
->the term h0 is called the baseline hazard. It corresponds to the value of the hazard if all
the xi are equal to zero (the quantity exp(0) equals 1). The 't' in h(t) reminds us that the
hazard may vary over time.

library("survival")

## Warning: package 'survival' was built under R version 4.0.5

args(coxph)

## function (formula, data, weights, subset, na.action, init, control,


## ties = c("efron", "breslow", "exact"), singular.ok = TRUE,
## robust, model = FALSE, x = FALSE, y = TRUE, tt, method = ties,
## id, cluster, istate, statedata, nocenter = c(-1, 0, 1), ...)
## NULL

function (formula, data, weights, subset, na.action, init, control,


ties = c("efron", "breslow", "exact"), singular.ok = TRUE,
robust = FALSE, model = FALSE, x = FALSE, y = TRUE, tt, method = ties,
...)
NULL

## function (formula, data, weights, subset, na.action, init, control,


## ties = c("efron", "breslow", "exact"), singular.ok = TRUE,
## robust = FALSE, model = FALSE, x = FALSE, y = TRUE, tt, method = ties,
## ...)
## NULL

Q.2:-Describe parametric regression model in survival analysis?


Ans:- A parametric survival model is a well-recognized statistical technique for exploring
the relationship between the survival of a patient, a parametric distribution and several
explanatory variables. It allows us to estimate the parameters of the distribution.
Exponential, weibull, lognormal, loglogistic and gamma are Popular parametric
models.Parametric models are often easier to work with provided they fit the data well.
The Exponential Distribution---ℎ(𝑡) = 𝜆 This is the simplest distribution to work with. It
has the hazard function
ℎ(𝑡) = 𝜆
which gives the memory-less property. This means, the risk of having an event of interest is
the same at any point in time as it was at the beginning. We have
𝑡 𝑡
𝐻(𝑡) = ∫ ℎ (𝑢)𝑑𝑢 = ∫ 𝜆 𝑑𝑢 = 𝜆𝑡
0 0

, and

𝑆(𝑡) = 𝑒 −𝐻(𝑡) = 𝑒 −𝜆𝑡


,
and pdf is

𝑓(𝑡) = ℎ(𝑡)𝑆(𝑡) = 𝜆𝑒 −𝜆𝑡

Fitting of exponential model


# Define -l(theta), negative of log-likelihood
nlle<-function(theta,y){
ll<-dexp(y,rate=theta,log=TRUE) #vetor of log-likelihoods
ll<-sum(ll)
return(-ll)
}
dump("nlle",file="nlle.txt")
source("nlle.txt")
# Simulate 20 observation from exponential distribution
set.seed(123)
y<-rexp(20,rate=0.03)
Fitting using optim()
M1<-optim(par=c(0.01),fn=nlle,method="L-BFGS-B",
lower=.001,hessian = TRUE,y=y)
print(M1)

## $par
## [1] 0.03699251
##
## $value
## [1] 85.94567
##
## $counts
## function gradient
## 12 12
##
## $convergence
## [1] 0
##
## $message
## [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
##
## $hessian
## [,1]
## [1,] 14636.52

## Extract estimate and its standard error


MLE<-M1$par #ML Estimator of rate,lambda
se=sqrt(diag(solve(M1$hessian))) # Standard error
out<-rbind(MLE,se)
colnames(out)<-c("rate")
# Final result
out

## rate
## MLE 0.036992509
## se 0.008265727

** Weibull distribution**---𝐻(𝑡) = (𝜆𝑡)𝛼


The cumulative hazard function for Weibull is the 𝛼 exponentiation of the hazard of
exponential, that is

𝐻(𝑡) = (𝜆𝑡)𝛼

⇒ ℎ(𝑡) = 𝐻′(𝑡) = 𝛼(𝜆𝑡)𝛼−1 𝜆


⇒ ℎ(𝑡) = 𝛼𝜆(𝜆𝑡)𝛼−1 = 𝛼𝜆𝛼 𝑡 𝛼−1

𝛼
⇒ 𝑆(𝑡) = 𝑒 −𝐻(𝑡) = 𝑒 −(𝜆𝑡)
Here 𝛼 is termed as shape parameter and 𝜆 is termed as rate parameter of Weibull
1
distribution. It may also be noted that in R it is 𝑠𝑐𝑎𝑙𝑒 = 𝑟𝑎𝑡𝑒.

• Interpretation of shape parameter 𝛼 for its different values:


𝛼 = 1 implies that ℎ(𝑡) is constant, that is exponential.
𝛼 < 1, implies ℎ(𝑡) is decreasing hazard rate
𝛼 > 1, implies ℎ(𝑡) is increasing hazard rate.

• The mean of Weibull is


1
𝛤(1 + 𝛼 ) 1
𝐸(𝑇) = = 𝑠𝑐𝑎𝑙𝑒 𝛤(1 + )
𝜆 𝛼

1
Note: 𝑠𝑐𝑎𝑙𝑒 = 𝜆.

• The median of Weibull is

1
(𝑙𝑜𝑔(2))𝛼
𝑡0.5 =
𝜆

In R Weibull is defined in terms of sacle, not rate.

Fitting of weibull distribution


#simulate observations
set.seed(1)
y<-rweibull(30,shape=1.3,scale=20)
# Define a function which returns -l(theta)
nllw<-function(theta,y){
ll<-dweibull(y,shape=theta[1],scale=theta[2],log=TRUE)
ll<-sum(ll)
return(-ll)
}
#Fit Weibull model using optim now
M2<-optim(par=c(0.2,5),fn=nllw,hessian=TRUE,
y=y,method="L-BFGS-B",lower=c(0.01,0.2))
print(M2)

## $par
## [1] 1.290646 19.153511
##
## $value
## [1] 114.9194
##
## $counts
## function gradient
## 16 16
##
## $convergence
## [1] 0
##
## $message
## [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
##
## $hessian
## [,1] [,2]
## [1,] 31.6382541 -0.6391224
## [2,] -0.6391224 0.1362192

MLE<-M2$par
se<-sqrt(diag(solve(M2$hessian)))
out<-rbind(MLE,se)
colnames(out)<-c("shape","scale")
out

## shape scale
## MLE 1.2906464 19.153511
## se 0.1868602 2.847762

** Gamma distribution**
Gamma distribution is another distribution which has shape and rate parameters.
However, it has an option for scale parameter also. Its pdf can be defined as:
𝜆𝑘 𝑡 𝑘−1 𝑒𝑥𝑝(−𝜆𝑡)
𝑓(𝑡) =
𝛤(𝑘)
where 𝑘 is known as shape parameter and 𝜆 is known as rate parameter. Its mean and
variance are in closed form defined as:
𝑘
𝑀𝑒𝑎𝑛 = 𝐸(𝑇) =
𝜆
and variance is
𝑘
𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒 = 𝑉𝑎𝑟(𝑇) =
𝜆2
However, when scale parameter, 𝜃 = 1/𝜆, then in that situation:
𝐸(𝑇) = 𝑘𝜃
and variance is
𝑉𝑎𝑟(𝑇) = 𝑘𝜃 2
for 𝑠ℎ𝑎𝑝𝑒 = 1, gamma reduces to exponential distribution. Consequently, hazard function
for 𝑘 = 1 is constant, for 𝑘 < 1 it is decreasing, whereas for 𝑘 > 1 it is increasing.
Fitting of gamma distribution
nllg<-function(theta,y,censor){
ll<-censor*dgamma(y,shape=theta[1],rate=theta[2],log=TRUE)+(1-
censor)*pgamma(y,shape=theta[1],rate=theta[2],log.p = TRUE,lower.tail =
FALSE)
ll<-sum(ll)
return(-ll)
}
dump("nllg",file="nllg.txt")
source("nllg.txt")

table116<- read.table("C:\\Users\\HP\\Downloads\\table116.txt", sep="")


# Fit using optim
M4<-
optim(par=c(1,1),fn=nllg,hessian=TRUE,y=table116$time,censor=table116$censor,
method="L-BFGS-B",lower=c(.01,.01))
MLE<-M4$par
se<-sqrt(diag(solve(M4$hessian)))
result<-rbind(MLE,se)
colnames(result)<-c("shape","rate")
result

## shape rate
## MLE 0.9645338 0.02173167
## se 0.4355252 0.01492654

Lognormal distribution---𝑙𝑜𝑔(𝑇) ∼ 𝑁(𝜇, 𝜎) If 𝑙𝑜𝑔(𝑇) ∼ 𝑁(𝜇, 𝜎), then


𝑇 ∼ 𝐿𝑜𝑔𝑛𝑜𝑟𝑚𝑎𝑙(𝜇, 𝜎)
where 𝜇 = 𝑚𝑒𝑎𝑛(𝑙𝑜𝑔(𝑇)), 𝜎 = 𝑠𝑑(𝑙𝑜𝑔(𝑇)). Its pdf is defines as:
𝑓(𝑡) = (2𝜋𝜎 2 𝑡 2 )−1/2 𝑒𝑥𝑝(−(𝑙𝑜𝑔(𝑡) − 𝜇)2 /(2𝜎 2 ))
with mean and variance defined as:
1
𝐸(𝑇) = 𝑒𝑥𝑝((𝜇 + 𝜎 2 ))
2
𝑉𝑎𝑟(𝑇) = 𝑒𝑥𝑝(2𝜇 + 2𝜎 2 ) − 𝑒𝑥𝑝(2𝜇 + 𝜎 2 )
In R it is defined as:
𝑇 ∼ 𝑑𝑙𝑛𝑜𝑟𝑚(𝜇, 𝜎)
Thus, pdf, cdf, quantile and random simulations form lognormal can be defined as:
pdf is dlnorm(x,mu,sigma) cdf is plnorm(x,mu,sigma) quantile is qlnorm(p,mu,sigma)
random simulation is rlnorm(n,mu,sigma)
Fitting of lognormal distribution
#Define an -l(theta) for lognormal
nllLN<-function(theta,y,censor){
ll<-censor*dlnorm(y,meanlog=theta[1],sdlog=theta[2],log=TRUE)+(1-
censor)*plnorm(y,meanlog = theta[1],sdlog=theta[2],log.p=TRUE,lower.tail =
FALSE)
ll<-sum(ll)
return(-ll)
}
dump("nllLN",file="nllLN.txt")
source("nllLN.txt")
table116 <- read.table("C:/Users/hp/Downloads/table116.txt", sep="")

# Fit using optim


M3<-
optim(par=c(1,1),fn=nllLN,hessian=TRUE,y=table116$time,censor=table116$censor
,method="L-BFGS-B",lower=c(.01,.01))
M3

## $par
## [1] 3.301151 1.425359
##
## $value
## [1] 33.52954
##
## $counts
## function gradient
## 14 14
##
## $convergence
## [1] 0
##
## $message
## [1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
##
## $hessian
## [,1] [,2]
## [1,] 4.477081 -1.143580
## [2,] -1.143580 6.494733

MLE<-M3$par
se<-sqrt(diag(solve(M3$hessian)))
result<-rbind(MLE,se)
colnames(result)<-c("mu","sigma")
result

## mu sigma
## MLE 3.301151 1.4253594
## se 0.483610 0.4015246

Q:-3 Discuss model selection in survival analysis?


Ans:- The function step() for backward step wise regression using AIC as selection criteria
as:
require(survival)
require(asaur)

## Loading required package: asaur

data(pharmacoSmoking)
dat1=pharmacoSmoking
dat1$ttr[dat1$ttr==0]<-0.5

# Weibull fit

weib.fit=survreg(Surv(ttr,relapse)~grp+age+employment,dist="weibull",data=dat
1)
model.step.weib<-step(weib.fit)

## Start: AIC=920.3
## Surv(ttr, relapse) ~ grp + age + employment
##
## Df AIC
## <none> 920.30
## - employment 2 925.38
## - grp 1 926.86
## - age 1 930.14

weib.fit$loglik #the first component for intercept only and the second for
both

## [1] -466.128 -454.150

-2*weib.fit$loglik # -2 times maximum log-likelihood

## [1] 932.2561 908.2999


The result is that grp, age and employment as predictors in the final model. The criteria for
selection is minimum AIC, which is defined as
𝐴𝐼𝐶 = −2 𝑙𝑜𝑔𝑙𝑖𝑘𝑒𝑙𝑖ℎ𝑜𝑜𝑑 + 2𝑘
where 𝑘 is the number of parameters in the model.
Q:-4 Make a comparative study of Cox'sPH and AFT?
Ans:- We shall compare two population under the assumption of Proportional hazard and
AFT as Weibull. Here we are considering the simple example of comparing just two groups.
Suppose now that we have two groups of survival data, one of the patients who received an
experimental treatment and one of patients who received control.
Cox's P H:In proportional hazard model scenario it is defined as
ℎ1 (𝑡) = 𝑒 𝛽 ℎ0 (𝑡)
where ℎ1 (𝑡) stands for hazard of treated group and ℎ0 (𝑡) stand for hazard of control group.
Note that above expression can be expressed in the form of hazard ratio as
ℎ1 (𝑡)
= 𝑒𝛽
ℎ0 (𝑡)
or in terms of logarithm of hazard ratio
ℎ1 (𝑡)
𝑙𝑜𝑔( )=𝛽
ℎ0 (𝑡)
Interpretation: If the experimental treatment were effective in increasing survival, the
hazard ratio would be less than one, and the log-hazard ratio 𝛽 would be negative.
Accelerated failure time (AFT): In this model we assume that the survival time for
treated patient is a multiple of 𝑒 𝛾 of what the survival time would have been had the
patient received the control treatment.
Interpretation: A key property of the AFT model is this: if the treatment is effective, the
acceleration time coefficient 𝑒 𝛾 will be greater than 1, and thus 𝛾 > 0.
Comparing two Weibull distributions using the AFT and Proportional hazards models:
• Proportional hazards model

ℎ1 (𝑡) = 𝑒 𝛽 ℎ0 (𝑡)
• Accelerated failure time (AFT) model given by

ℎ1 (𝑡) = 𝑒 −𝛾 ℎ0 (𝑒 −𝛾 𝑡)
• 𝛽 < 0  ⇒ 𝑒 𝛽 < 1 for proportional hazards model, this implies treatment is effective
(increase survival and decrease in hazard)
• 𝛾 > 0 ⇒ 𝑒 𝛾 > 1 for AFT implies treatment is effective.

• A relation between AFT and CoxPH


𝛾 −𝛾
𝑒 𝛽 = 𝑒 −𝜎 ⇒ 𝛽 =
𝜎
Comparison using pharmacoSmoking data The data frame 'pharmacoSmoking' deals
with Randomized trial of triple therapy vs. patch for smoking cessation. It has the 14
variable and 125 rows. Details can be seen from the package asaur. However, its structure
can be seen in the following. Response variable is ttr time to relapse and censoring status
is relapse, grp stands for treatment group.
library(asaur)
# Let us replace 0 with 0.5 in response ttr
dat1<-pharmacoSmoking
dat1$ttr[dat1$ttr==0]<-0.5
#Fit an AFT Weibull model
library(survival)
result_survreg_grp<-survreg(Surv(ttr,relapse)~grp,data=dat1,dist="weibull")
# Fit a cox proportional hazards model
# In CoxPH, distribution is not specified. The only condition is that of
constant hazard ratio.
result_coxph_grp<-coxph(Surv(ttr,relapse)~grp,data=dat1)

Get the summary of results


summary(result_survreg_grp)

##
## Call:
## survreg(formula = Surv(ttr, relapse) ~ grp, data = dat1, dist = "weibull")
## Value Std. Error z p
## (Intercept) 5.2859 0.3320 15.92 <2e-16
## grppatchOnly -1.2514 0.4348 -2.88 0.004
## Log(scale) 0.6888 0.0911 7.56 4e-14
##
## Scale= 1.99
##
## Weibull distribution
## Loglik(model)= -461.8 Loglik(intercept only)= -466.1
## Chisq= 8.63 on 1 degrees of freedom, p= 0.0033
## Number of Newton-Raphson Iterations: 5
## n= 125

^ = −1.251, indicating that ptchOnly treatment group has shorter times to


Note that 𝛾
^
relapse than the triple therapy group by a factor of 𝑒 𝛾 = 𝑒 −1.251 = 0.286. This implies that
^
^ = − 𝛾 = 1.251 = 0.629
𝛽
^
𝜎 1.99
which implies that
^
𝑒 𝛽 = 𝑒 0.629 = 1.88
which means that 88% hazard is higher with patchOnly as compared to triple therapy. This
can be obtained from CoxPH model also as
summary(result_coxph_grp)

## Call:
## coxph(formula = Surv(ttr, relapse) ~ grp, data = dat1)
##
## n= 125, number of events= 89
##
## coef exp(coef) se(coef) z Pr(>|z|)
## grppatchOnly 0.6050 1.8313 0.2161 2.8 0.00511 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## grppatchOnly 1.831 0.5461 1.199 2.797
##
## Concordance= 0.581 (se = 0.027 )
## Likelihood ratio test= 7.99 on 1 df, p=0.005
## Wald test = 7.84 on 1 df, p=0.005
## Score (logrank) test = 8.07 on 1 df, p=0.004

^ = 0.629. This fact has already been observed. ##


Note that coef=0.6050 is very close to 𝛽
Plotting of survival curves under CoxPH and AFT
mu0.hat<-result_survreg_grp$coef[1]
sigma.hat<-result_survreg_grp$scale
alpha.hat<-1/sigma.hat
lambda.hat<-exp(-mu0.hat)

From these we compute the baseline hazard function


tt.vec<-0:182
surv0.vec<-1-pweibull(tt.vec,shape=alpha.hat,scale=1/lambda.hat)

Since survival function for patchOnly group is


^
𝛾
−^
𝑒 𝜎
𝑆1 (𝑡) = 𝑆0 (𝑡)
Thus,
gamma.hat<-result_survreg_grp$coef[2]
surv1.vec<-surv0.vec^(exp(-gamma.hat/sigma.hat))
Estimate survivals under coxph
coxph.surv.est<-
survfit(result_coxph_grp,newdata=data.frame(list(grp=c("combination","patchOn
ly"))))

Plot CoxPH and AFT together


plot(coxph.surv.est,col=c("green","yellow"),xlab="Time in
days",ylab="Survival probability")
lines(surv0.vec~tt.vec,col="green")
lines(surv1.vec~tt.vec,col="yellow")
legend("topright",legend=c("Cox.model.combination","Weibull.model.combination
",
"Cox.modelpatchOnly","Weibull.modelpatchOnly"),col=c("green","green","yellow"
,"yellow"),lty=c(1,1,1,1))

From above plot, it is evident that both the models reach to the same conclusion. Moreover,
it is also evident that combination or triple therapy performs better than the patchOnly as
it improves the survival probability.

You might also like