setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW3")
data <- read.csv("prostateCancerSub.csv")
library(survival)
library(ggplot2)
## Warning: 程辑包'ggplot2'是用R版本4.2.3 来建造的
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ tibble 3.2.1 ✔ dplyr 1.0.10
## ✔ tidyr 1.3.0 ✔ stringr 1.5.1
## ✔ readr 2.1.4 ✔ forcats 0.5.2
## ✔ purrr 1.0.1
## Warning: 程辑包'tibble'是用R版本4.2.3 来建造的
## Warning: 程辑包'tidyr'是用R版本4.2.3 来建造的
## Warning: 程辑包'readr'是用R版本4.2.3 来建造的
## Warning: 程辑包'purrr'是用R版本4.2.3 来建造的
## Warning: 程辑包'stringr'是用R版本4.2.3 来建造的
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(parfm)
## Warning: 程辑包'parfm'是用R版本4.2.3 来建造的
## 载入需要的程辑包:optimx
## Warning: 程辑包'optimx'是用R版本4.2.3 来建造的
library(frailtyEM)
## Warning: 程辑包'frailtyEM'是用R版本4.2.3 来建造的
data$combined_cause <- ifelse(data$Status %in% c("dead - prostatic ca", "dead - other ca"), "cancer",
ifelse(data$Status %in% c("dead - cerebrovascular", "dead - heart or vascular"), "vascular disease",
ifelse(data$Status %in% c("alive"), "alive", "other causes")))
# Creating a new data frame with the combined causes of death
combined_data <- data.frame(time = data$Time, status = data$combined_cause)
# Obtaining the corresponding cumulative incidence for each type of death
# Treating deaths from the other two causes as censored
cancer_cumulative <- survfit(Surv(time, status == "cancer") ~ 1, data = combined_data)
vascular_cumulative <- survfit(Surv(time, status == "vascular disease") ~ 1, data = combined_data)
other_cumulative <- survfit(Surv(time, status == "other causes") ~ 1, data = combined_data)
cumDist_ca <- 1 - cancer_cumulative$surv
cumDist_va <- 1 - vascular_cumulative$surv
cumDist_ot <- 1- other_cumulative$surv
time <- cancer_cumulative$time / 12
par(mar = c(5, 4, 4, 4) + 0.3) # Leave space for right axis
plot(cumDist_ca ~ time, type = "s", ylim = c(0, 1), lwd = 2, xlab = "", ylab = "", col = "red")
par(new = TRUE)
plot(cumDist_va ~ time, type = "s", ylim = c(0, 1), lwd = 2, axes = FALSE, xlab = "", ylab = "", col = "blue", lty=2)
par(new = TRUE)
plot(cumDist_ot ~ time, type = "s", ylim = c(0, 1), lwd = 2, axes = FALSE, xlab = "", ylab = "", col = "green", lty=3)
mtext("Years from cancer diagnosis", side = 1, line = 2.5)
mtext("The cumulative incidence functions", side = 2, line = 2.5, col = "dodgerblue")
legend("topright", legend = c("Cancer", "Vascular Disease", "Other Causes"), col = c("red", "blue", "green"), lty = 1:3, cex = 0.8)
grid()
We can find that cancer is the main cause of death, followed by vascular disease.
state.cp <- rep(0, nrow(data)) # cp: competing risks
state.cp[data$combined_cause == 'cancer'] <- 1
state.cp[data$combined_cause == 'vascular disease'] <- 2
state.cp[!(data$combined_cause %in% c('cancer', 'vascular disease', 'alive'))] <- 3 # death due to other causes
table(state.cp) # check if the status is correct
## state.cp
## 0 1 2 3
## 148 155 127 72
data$status.cp <- factor(state.cp)
fit <- survfit(Surv(Time, status.cp) ~ 1, data)
fit$pstate # the first column for 'alive', i.e., the overall survival
## [,1] [,2] [,3] [,4]
## [1,] 0.9681275 0.007968127 0.009960159 0.01394422
## [2,] 0.9482072 0.011952191 0.019920319 0.01992032
## [3,] 0.9262948 0.019920319 0.025896414 0.02788845
## [4,] 0.9203187 0.019920319 0.031872510 0.02788845
## [5,] 0.9103586 0.025896414 0.035856574 0.02788845
## [6,] 0.8884462 0.037848606 0.043824701 0.02988048
## [7,] 0.8705179 0.049800797 0.047808765 0.03187251
## [8,] 0.8545817 0.055776892 0.053784861 0.03585657
## [9,] 0.8386454 0.061752988 0.059760956 0.03984064
## [10,] 0.8207171 0.071713147 0.063745020 0.04382470
## [11,] 0.8127490 0.077689243 0.065737052 0.04382470
## [12,] 0.8027888 0.081673307 0.067729084 0.04780876
## [13,] 0.7768924 0.097609562 0.075697211 0.04980080
## [14,] 0.7629482 0.101593625 0.081673307 0.05378486
## [15,] 0.7490040 0.111553785 0.083665339 0.05577689
## [16,] 0.7430279 0.115537849 0.085657371 0.05577689
## [17,] 0.7310757 0.119521912 0.089641434 0.05976096
## [18,] 0.7231076 0.121513944 0.091633466 0.06374502
## [19,] 0.7131474 0.125498008 0.097609562 0.06374502
## [20,] 0.6992032 0.137450199 0.099601594 0.06374502
## [21,] 0.6832669 0.141434263 0.109561753 0.06573705
## [22,] 0.6613546 0.153386454 0.115537849 0.06972112
## [23,] 0.6533865 0.157370518 0.115537849 0.07370518
## [24,] 0.6414343 0.159362550 0.123505976 0.07569721
## [25,] 0.6254980 0.167330677 0.127490040 0.07968127
## [26,] 0.6195219 0.171314741 0.127490040 0.08167331
## [27,] 0.6015936 0.179282869 0.137450199 0.08167331
## [28,] 0.5816733 0.187250996 0.141434263 0.08964143
## [29,] 0.5677291 0.195219124 0.145418327 0.09163347
## [30,] 0.5597610 0.199203187 0.147410359 0.09362550
## [31,] 0.5418327 0.203187251 0.157370518 0.09760956
## [32,] 0.5298805 0.211155378 0.161354582 0.09760956
## [33,] 0.5219124 0.217131474 0.163346614 0.09760956
## [34,] 0.5039841 0.221115538 0.171314741 0.10358566
## [35,] 0.4980080 0.225099602 0.173306773 0.10358566
## [36,] 0.4880478 0.227091633 0.179282869 0.10557769
## [37,] 0.4741036 0.231075697 0.185258964 0.10956175
## [38,] 0.4681275 0.235059761 0.187250996 0.10956175
## [39,] 0.4621514 0.239043825 0.189243028 0.10956175
## [40,] 0.4541833 0.243027888 0.193227092 0.10956175
## [41,] 0.4322709 0.249003984 0.209163347 0.10956175
## [42,] 0.4282869 0.250996016 0.209163347 0.11155378
## [43,] 0.4203187 0.252988048 0.215139442 0.11155378
## [44,] 0.4123506 0.256972112 0.215139442 0.11553785
## [45,] 0.4063745 0.256972112 0.219123506 0.11752988
## [46,] 0.3944223 0.260956175 0.225099602 0.11952191
## [47,] 0.3904382 0.262948207 0.227091633 0.11952191
## [48,] 0.3884462 0.262948207 0.227091633 0.12151394
## [49,] 0.3824701 0.264940239 0.231075697 0.12151394
## [50,] 0.3745020 0.266932271 0.233067729 0.12549801
## [51,] 0.3664913 0.268934955 0.237073098 0.12750069
## [52,] 0.3643480 0.268934955 0.237073098 0.12964392
## [53,] 0.3530329 0.275724049 0.241599160 0.12964392
## [54,] 0.3412651 0.282784707 0.243952713 0.13199747
## [55,] 0.3388618 0.285187982 0.243952713 0.13199747
## [56,] 0.3364240 0.287625837 0.243952713 0.13199747
## [57,] 0.3289479 0.290117866 0.246444742 0.13448950
## [58,] 0.3211158 0.292728564 0.251666138 0.13448950
## [59,] 0.3183712 0.292728564 0.251666138 0.13723408
## [60,] 0.3125295 0.292728564 0.254586974 0.14015491
## [61,] 0.3094655 0.295792579 0.254586974 0.14015491
## [62,] 0.3031499 0.298950391 0.257744786 0.14015491
## [63,] 0.2998186 0.302281708 0.257744786 0.14015491
## [64,] 0.2962913 0.302281708 0.257744786 0.14368219
## [65,] 0.2885954 0.306129648 0.257744786 0.14753013
## [66,] 0.2799806 0.314744437 0.257744786 0.14753013
## [67,] 0.2752352 0.314744437 0.257744786 0.15227557
## [68,] 0.2752352 0.314744437 0.257744786 0.15227557
## [69,] 0.2681779 0.321801750 0.257744786 0.15227557
## [70,] 0.2607285 0.329251136 0.257744786 0.15227557
## [71,] 0.2514168 0.338562868 0.257744786 0.15227557
## [72,] 0.2514168 0.338562868 0.257744786 0.15227557
## [73,] 0.2514168 0.338562868 0.257744786 0.15227557
## [74,] 0.2381843 0.351795331 0.257744786 0.15227557
## [75,] 0.2381843 0.351795331 0.257744786 0.15227557
## [76,] 0.2381843 0.351795331 0.257744786 0.15227557
rowSums(fit$pstate) # should sum up to 1
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [39] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
nrow(fit$pstate)
## [1] 76
ggplot(data.frame(x = fit$time/12, y1 = fit$pstate[, 2], y2 = fit$pstate[, 3], y3 = fit$pstate[, 4]), aes(x)) +
geom_step(aes(y = y1, colour = "Death due to cancer")) +
geom_step(aes(y = y2, colour = "Death due to vascular disease")) +
geom_step(aes(y = y3, colour = "Death due to other causes")) +
labs(title = "Estimated Cumulative Incidence Functions for Different Causes of Death", x = "Time (years)", y = "Estimated F(t)") +
theme(plot.title = element_text(hjust = 0.5, size = 15), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12), legend.position = 'bottom')
The conlusion is the same as that of (1).
set.seed(1220)
n <- 2000
# Randomly sample n_i from {2, 3, 4} with equal probability
n_i <- sample(c(2, 3, 4), n, replace = TRUE)
N <- sum(n_i)
X1 <- runif(N)
X2 <- rgamma(n, shape = 0.5, rate = 0.5) %>% log() %>% rep(n_i)
u <- runif(N)
t <- sqrt(-log(1-u) / exp(-2 * X1 + X2))
c <- runif(N, 3, 6)
delta <- as.numeric(t <= c)
# Report the summary statistics of t_ij and the numbers of 0 and 1 for delta_ij
summary(t)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0192 0.9386 1.9100 9.5589 4.4909 2809.0954
table(delta)
## delta
## 0 1
## 1540 4503
data1 <- data.frame(t = t, delta = delta, x1 = X1, x2 = X2)
# Fit the Cox proportional hazards model with both X1 and X2 as covariates
cox_model_both <- coxph(Surv(t, delta) ~ X1 + X2, data = data1)
# Fit the Cox proportional hazards model with only X1 as a covariate
cox_model_x1 <- coxph(Surv(t, delta) ~ X1, data = data1)
summary(cox_model_both)
## Call:
## coxph(formula = Surv(t, delta) ~ X1 + X2, data = data1)
##
## n= 6043, number of events= 4503
##
## coef exp(coef) se(coef) z Pr(>|z|)
## X1 -2.07110 0.12605 0.05662 -36.58 <2e-16 ***
## X2 1.00198 2.72366 0.01530 65.50 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## X1 0.126 7.9335 0.1128 0.1408
## X2 2.724 0.3672 2.6432 2.8066
##
## Concordance= 0.829 (se = 0.003 )
## Likelihood ratio test= 7038 on 2 df, p=<2e-16
## Wald test = 4573 on 2 df, p=<2e-16
## Score (logrank) test = 4051 on 2 df, p=<2e-16
summary(cox_model_x1)
## Call:
## coxph(formula = Surv(t, delta) ~ X1, data = data1)
##
## n= 6043, number of events= 4503
##
## coef exp(coef) se(coef) z Pr(>|z|)
## X1 -0.91125 0.40202 0.05225 -17.44 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## X1 0.402 2.487 0.3629 0.4454
##
## Concordance= 0.591 (se = 0.004 )
## Likelihood ratio test= 306.2 on 1 df, p=<2e-16
## Wald test = 304.1 on 1 df, p=<2e-16
## Score (logrank) test = 308.1 on 1 df, p=<2e-16
For Model with both X1 and X2, the coefficient of X1 is-2.07110. But in the model having only X1, we got the coefficient equals to -0.91125, which is different from its true value. We can consider that X2 is a significant covariate which can’t be omitted.
data1 <- data.frame(ID = rep(1:2000, n_i), t = t, delta = delta, x1 = X1, x2 = X2)
cox_shared_frailty_model <- emfrail(Surv(t, delta) ~ X1 + cluster(ID), data = data1)
summary(cox_shared_frailty_model)
## Call:
## emfrail(formula = Surv(t, delta) ~ X1 + cluster(ID), data = data1)
##
## Regression coefficients:
## coef exp(coef) se(coef) adj. se z p
## X1 -2.0737 0.1257 0.0712 0.0732 -28.3352 0
## Estimated distribution: gamma / left truncation: FALSE
##
## Fit summary:
## Commenges-Andersen test for heterogeneity: p-val 0
## no-frailty Log-likelihood: -36625.44
## Log-likelihood: -35179.91
## LRT: 1/2 * pchisq(2891), p-val 0
##
## Frailty summary:
## estimate lower 95% upper 95%
## Var[Z] 2.013 1.862 2.185
## Kendall's tau 0.502 0.482 0.522
## Median concordance 0.514 0.492 0.536
## E[logZ] -1.280 -1.407 -1.170
## Var[logZ] 4.992 4.373 5.744
## theta 0.497 0.458 0.537
## Confidence intervals based on the likelihood function
According to the result above, we find that the \(\hat{\beta}_1=−2.0737\), which is close to the true value. This means that the bias due to omitting important variables in the model is corrected by shared frailty into the model. And the p-value of likelihood ratio test shows that the frailty term is highly significant. Also, we have \(\hat{\theta}=0.497\), which is close to the true value 0.5.