setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW1")
data <- read.csv("kidneyTrans.csv")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.2 ✔ purrr 1.0.1
## ✔ tibble 3.2.1 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.4.1
## ✔ readr 2.1.4 ✔ forcats 0.5.2
## Warning: 程辑包'ggplot2'是用R版本4.2.3 来建造的
## Warning: 程辑包'tibble'是用R版本4.2.3 来建造的
## Warning: 程辑包'readr'是用R版本4.2.3 来建造的
## Warning: 程辑包'purrr'是用R版本4.2.3 来建造的
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(asaur)
library(survival)
data <- data %>% filter(age >= 35)
I think log-normal distribution would be more appropriate for the survival times from kidney transplant to death. There is no proportional hazards assumption in the dataset and it’s hazard function is not monotonic, which shows that Weibull distribution is not appropriate.
data1 <- data %>% filter(age < 55)
Check Weibull distribution:
death <- data1$death
times <- data1$time
times[times == 0] <- 0.5 # re-define survival times listed as 0 to 0.5
km_fit <- survfit(Surv(times, death) ~ 1)
survEst <- km_fit$surv
survTime <- km_fit$time
logLogSurvEst <- log(-log(survEst)) # log{-log(S(t))}
logSurvTime <- log(survTime) # log(t)
ggplot(data.frame(x = logSurvTime, y = logLogSurvEst), mapping = aes(x = x, y = y)) +
geom_step() +
geom_smooth(method = 'lm', formula = y ~ x) +
labs(title = "Log-cumulative Hazard Plot", x = expression(log(t)), y = "log[-log{S(t)}]") +
theme(plot.title = element_text(hjust = 0.5, size = 13), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12))
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
logLogSurvEst1 <- logLogSurvEst[is.finite(logLogSurvEst)]
logSurvTime1 <- logSurvTime[-(1:2)]
(lm_fit <- lm(logLogSurvEst1 ~ logSurvTime1))
##
## Call:
## lm(formula = logLogSurvEst1 ~ logSurvTime1)
##
## Coefficients:
## (Intercept) logSurvTime1
## -6.5040 0.6735
Check log-logistic distribution:
km_fit <- survfit(Surv(times, death) ~ 1)
survEst <- km_fit$surv
survTime <- km_fit$time
logOddsSurvEst <- log(survEst / (1 - survEst)) # log{S(t) / (1 - S(t))}
phiInvSurvEst <- qnorm(1 - survEst) # \Phi^{-1}(1 - S(t))
logSurvTime <- log(survTime) # log(t)
ggplot(data.frame(x = logSurvTime, y = logOddsSurvEst), mapping = aes(x = x, y = y)) +
geom_step() +
geom_smooth(method = 'lm', formula = y ~ x) +
labs(title = "Assess Suitability of Log-Logistic Distribution", x = expression(log(t)), y = "Estimated log-odds") +
theme(plot.title = element_text(hjust = 0.5, size = 13), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12))
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
Check log_normal distribution:
ggplot(data.frame(x = logSurvTime, y = phiInvSurvEst), mapping = aes(x = x, y = y)) +
geom_step() +
geom_smooth(method = 'lm', formula = y ~ x) +
labs(title = "Assess Suitability of Log-Normal Distribution", x = expression(log(t)), y = expression(Phi^{-1}(1 - S(t)))) +
theme(plot.title = element_text(hjust = 0.5, size = 13), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12))
## Warning: Removed 2 rows containing non-finite values (`stat_smooth()`).
According to the figures above, we find that the plot of \(\Phi^{-1}\{1-\hat{S}_{KM}(t) \}\) against \(\log{t}\) gives the best straight line among 3 distribution, so we consider the log-normal distribution to be the most suitable for the survival times.
data2 <- data %>% filter(age >= 55)
est_km1 <- survfit(Surv(time, death) ~ 1, conf.type = "plain", conf.int = 0.9, data = data1)
plot(est_km1, main = "90% Confidence interval without transformation(35 ≤ age < 55)", xlab = "Time (Days)", ylab = "Estimated survival function")
est_km2 <- survfit(Surv(time, death) ~ 1, conf.type = "plain", conf.int = 0.9, data = data2)
plot(est_km2, main = "90% Confidence interval without transformation(>55)", xlab = "Time (Days)", ylab = "Estimated survival function")
summary(est_km1)
## Call: survfit(formula = Surv(time, death) ~ 1, data = data1, conf.type = "plain",
## conf.int = 0.9)
##
## time n.risk n.event survival std.err lower 90% CI upper 90% CI
## 7 422 1 0.998 0.00237 0.994 1.000
## 10 421 1 0.995 0.00334 0.990 1.000
## 17 417 1 0.993 0.00410 0.986 1.000
## 26 416 1 0.990 0.00473 0.983 0.998
## 28 415 1 0.988 0.00529 0.979 0.997
## 37 412 1 0.986 0.00580 0.976 0.995
## 40 411 1 0.983 0.00626 0.973 0.994
## 44 410 1 0.981 0.00669 0.970 0.992
## 57 407 2 0.976 0.00747 0.964 0.988
## 59 405 1 0.974 0.00783 0.961 0.987
## 62 404 1 0.971 0.00818 0.958 0.985
## 68 400 2 0.966 0.00883 0.952 0.981
## 88 396 1 0.964 0.00914 0.949 0.979
## 91 395 1 0.962 0.00943 0.946 0.977
## 98 393 1 0.959 0.00972 0.943 0.975
## 104 392 1 0.957 0.01000 0.940 0.973
## 106 389 1 0.954 0.01027 0.937 0.971
## 119 381 1 0.952 0.01055 0.934 0.969
## 121 380 1 0.949 0.01081 0.931 0.967
## 150 377 1 0.947 0.01107 0.928 0.965
## 154 374 1 0.944 0.01133 0.925 0.963
## 190 371 1 0.942 0.01158 0.923 0.961
## 228 363 1 0.939 0.01183 0.920 0.958
## 242 357 1 0.936 0.01209 0.916 0.956
## 248 354 1 0.934 0.01234 0.913 0.954
## 249 353 1 0.931 0.01259 0.910 0.952
## 252 351 1 0.928 0.01283 0.907 0.950
## 273 345 1 0.926 0.01307 0.904 0.947
## 297 340 1 0.923 0.01331 0.901 0.945
## 311 338 1 0.920 0.01355 0.898 0.943
## 340 331 1 0.917 0.01379 0.895 0.940
## 344 327 1 0.915 0.01403 0.892 0.938
## 346 326 1 0.912 0.01427 0.888 0.935
## 354 323 1 0.909 0.01450 0.885 0.933
## 470 310 1 0.906 0.01475 0.882 0.930
## 478 309 1 0.903 0.01499 0.879 0.928
## 495 307 1 0.900 0.01522 0.875 0.925
## 614 295 1 0.897 0.01547 0.872 0.923
## 615 294 1 0.894 0.01572 0.868 0.920
## 697 282 1 0.891 0.01598 0.865 0.917
## 776 271 1 0.888 0.01626 0.861 0.914
## 793 269 1 0.884 0.01653 0.857 0.912
## 840 268 1 0.881 0.01679 0.853 0.909
## 875 262 1 0.878 0.01706 0.850 0.906
## 929 254 1 0.874 0.01734 0.846 0.903
## 939 252 1 0.871 0.01762 0.842 0.900
## 943 251 1 0.867 0.01788 0.838 0.897
## 945 250 2 0.860 0.01840 0.830 0.891
## 1016 241 1 0.857 0.01867 0.826 0.888
## 1164 220 1 0.853 0.01899 0.822 0.884
## 1186 218 1 0.849 0.01930 0.817 0.881
## 1191 217 1 0.845 0.01960 0.813 0.877
## 1275 203 1 0.841 0.01994 0.808 0.874
## 1326 198 1 0.837 0.02029 0.803 0.870
## 1331 197 1 0.832 0.02062 0.799 0.866
## 1388 191 1 0.828 0.02097 0.794 0.863
## 1473 185 1 0.824 0.02133 0.789 0.859
## 1777 151 1 0.818 0.02188 0.782 0.854
## 1820 141 1 0.812 0.02248 0.775 0.849
## 1835 140 1 0.807 0.02305 0.769 0.844
## 1877 137 1 0.801 0.02362 0.762 0.840
## 1940 132 1 0.795 0.02421 0.755 0.834
## 2034 122 1 0.788 0.02487 0.747 0.829
## 2276 95 1 0.780 0.02596 0.737 0.822
## 2301 93 1 0.771 0.02700 0.727 0.816
## 2369 88 1 0.763 0.02808 0.716 0.809
## 2414 85 1 0.754 0.02915 0.706 0.802
## 2421 84 1 0.745 0.03015 0.695 0.794
## 2557 74 1 0.735 0.03138 0.683 0.786
## 2567 73 1 0.725 0.03252 0.671 0.778
## 2650 65 1 0.713 0.03388 0.658 0.769
## 2795 44 1 0.697 0.03678 0.637 0.758
## 3146 19 1 0.661 0.04990 0.578 0.743
From the Kaplan–Meier estimate of the 2 groups, we choose p from 0.01 to 0.33 (by 0.01). The we need to get the quantiles:
qua <- function(surv, p, t){
if(p>1|p<0){
stop("p value is wrong!")
}
k <- 0
q <- 0
for(i in 1:length(t)){
if(p==1-surv[i]){
q <- (t[i]+t[i+1])/2
k <- 1
}
}
if(k==0){
for(i in 1:length(t)){
if(p>1-surv[i]){
q <- t[i]
k <- 1
}
}
}
if(k==0){
stop("The quantile can't be estimate!")
}
return(q)
}
p <- seq(0.01, 0.33, 0.01)
t1 <- numeric(length(p))
t2 <- numeric(length(p))
c1 <- est_km1$n.censor
time1 <- est_km1$time[c1 == 0]
surv1 <- est_km1$surv[c1 == 0]
c2 <- est_km2$n.censor
time2 <- est_km2$time[c2 == 0]
surv2 <- est_km2$surv[c2 == 0]
for(i in 1:length(p)){
t1[i] <- qua(surv1, p[i], time1)
t2[i] <- qua(surv2, p[i], time2)
}
data.frame(p, t1, t2)
## p t1 t2
## 1 0.01 26 2
## 2 0.02 44 10
## 3 0.03 59 26
## 4 0.04 91 26
## 5 0.05 119 50
## 6 0.06 190 56
## 7 0.07 249 69
## 8 0.08 311 78
## 9 0.09 346 98
## 10 0.10 495 158
## 11 0.11 697 158
## 12 0.12 840 291
## 13 0.13 939 334
## 14 0.14 945 391
## 15 0.15 1164 421
## 16 0.16 1275 439
## 17 0.17 1331 490
## 18 0.18 1473 583
## 19 0.19 1820 652
## 20 0.20 1877 730
## 21 0.21 1940 790
## 22 0.22 1940 864
## 23 0.23 2301 946
## 24 0.24 2301 1013
## 25 0.25 2414 1105
## 26 0.26 2421 1210
## 27 0.27 2557 1210
## 28 0.28 2567 1357
## 29 0.29 2650 1384
## 30 0.30 2650 1418
## 31 0.31 2795 1509
## 32 0.32 2795 1509
## 33 0.33 2795 1734
ggplot(data.frame(x = t1, y = t2), aes(x = x, y = y)) +
geom_point() + geom_smooth(span = 1) +
labs(title = "Q-Q Plot", x = "quantile for 35<age<55 group", y = "quantile for age>55 group") +
theme(plot.title = element_text(hjust = 0.5, size = 13), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
The Q-Q plot is not a straight line, so we conclude that the AFT model is not valid.
\(\log \Lambda(t)=\log\{-\log S(t)\}\), so we can use the plot of \(\log\{-\log S(t)\}\) against \(t\) to check:
t1 <- est_km1$time
t2 <- est_km2$time
y1 <- log(-log(est_km1$surv))
y2 <- log(-log(est_km2$surv))
ggplot() +
geom_step(aes(x = t1, y = y1, color = "35<=age<55"), size = 1) +
geom_step(aes(x = t2, y = y2, color = "age>=55"), size = 1) +
labs(title = "Kaplan-Meier estimate of log{log[S(t)]}", x = "time", y = "log{-log[S(t)]}") +
theme(plot.title = element_text(hjust = 0.5, size = 13), axis.title = element_text(size = 14),
axis.text.x = element_text(size = 11), axis.text.y = element_text(size = 12))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
According to the plot, we find that there is a vertical shift between 2 curves, so the proportional hazard assumption is valid.
setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW4")
data <- read.csv("TdapVaccine.csv")
time <- ifelse(data$Delivery>=37, 37, data$Delivery)
status <- ifelse(data$Delivery>=37, 0, 1)
data1 <- cbind(data, time, status)
model1 <- coxph(Surv(time, status) ~ Vac, data1)
summary(model1)
## Call:
## coxph(formula = Surv(time, status) ~ Vac, data = data1)
##
## n= 1000, number of events= 364
##
## coef exp(coef) se(coef) z Pr(>|z|)
## Vac -0.4447 0.6410 0.1145 -3.885 0.000102 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## Vac 0.641 1.56 0.5122 0.8022
##
## Concordance= 0.553 (se = 0.012 )
## Likelihood ratio test= 15.91 on 1 df, p=7e-05
## Wald test = 15.09 on 1 df, p=1e-04
## Score (logrank) test = 15.34 on 1 df, p=9e-05
The p-value is 0.000102 <<0.05, so we consider that receiving the Tdap vaccine during pregnancy has an influence on preterm birth.
data2 <- tmerge(data1, data1, id = X, status = event(time, status), tvac = tdc(tVac))
data2 <- data2[, c("X", "tstart", "tstop", "status", "tvac")]
model2 <- coxph(Surv(tstart, tstop, status) ~ tvac, data2)
summary(model2)
## Call:
## coxph(formula = Surv(tstart, tstop, status) ~ tvac, data = data2)
##
## n= 1326, number of events= 364
##
## coef exp(coef) se(coef) z Pr(>|z|)
## tvac 0.1209 1.1285 0.1149 1.053 0.293
##
## exp(coef) exp(-coef) lower .95 upper .95
## tvac 1.129 0.8861 0.901 1.413
##
## Concordance= 0.512 (se = 0.012 )
## Likelihood ratio test= 1.09 on 1 df, p=0.3
## Wald test = 1.11 on 1 df, p=0.3
## Score (logrank) test = 1.11 on 1 df, p=0.3
The p-value = 0.293>0.05, so we consider that receiving the Tdap vaccine during pregnancy has no influence on preterm birth.
For model1, the variable \(Vac\) is a time-independent covariate, which means the model was fitted by using the future information. Therefore, we prefer the conclusion from model2, where the \(Vac\) was used as a time-dependent covariate. There is not enough evidence to show that receiving the Tdap vaccine during pregnancy has an influence on preterm birth.