setwd("C:/Users/hp/Desktop")
data <- read.csv("kidneyTrans.csv")
library(survival)
library(tidyverse)
library(ggplot2)
library(muhaz)

(1)

data_BF <- data %>% filter(race == 2, gender == 2)
time <- data_BF$time
status <- data_BF$death


est_km <- survfit(Surv(time, status) ~ 1, conf.int = 0.90,conf.type = "plain")
plot(est_km, main = "90% Confidence interval without transformation(BF)", xlab = "Time", ylab = "Estimated survival function")

(2)

summary(est_km)
## Call: survfit(formula = Surv(time, status) ~ 1, conf.int = 0.9, conf.type = "plain")
## 
##  time n.risk n.event survival std.err lower 90% CI upper 90% CI
##    40     58       1    0.983  0.0171        0.955        1.000
##    45     57       1    0.966  0.0240        0.926        1.000
##   106     55       1    0.948  0.0293        0.900        0.996
##   121     52       1    0.930  0.0339        0.874        0.985
##   229     51       1    0.912  0.0378        0.849        0.974
##   344     45       1    0.891  0.0421        0.822        0.960
##   864     37       1    0.867  0.0473        0.789        0.945
##   929     30       1    0.838  0.0539        0.750        0.927
##   943     29       1    0.809  0.0592        0.712        0.907
##  1016     26       1    0.778  0.0646        0.672        0.885
##  1196     24       1    0.746  0.0696        0.631        0.860
##  2171     13       1    0.688  0.0846        0.549        0.828
##  2276     11       1    0.626  0.0974        0.466        0.786
##  2650      7       1    0.536  0.1176        0.343        0.730

According to the result above, the probability a black female will survival at least 12 months (365 days) after transplantation is about 0.891 and its confidence interval is [0.822, 0.960].

(3)

\(t_{0.25} = inf\{S_{KM}(t) \leq 0.75\} = 1196\).

\([t_l, t_u]=[929, 2650]\).

(4)

res <- muhaz(time, status, bw.grid = 365, kern = "biquadratic", bw.method = "global", b.cor = "none")
plot(res, main = "Kernel-smoothed estimate with b=365(BF)", col = "red", lwd = 2)

For the kernel-smoothed hazard function of black female patients, there is a peak at around 1000 days, and the hazard rate increases with time after 2000 days.

(5)

res1 <- muhaz(time, status, bw.grid = 365, kern = "biquadratic", bw.method = "global", b.cor = "left")
plot(res, main = "Kernel-smoothed estimate with b=365(BF)", col = "red", lwd = 2)
lines(res1, col = "blue", lwd = 1, lty=2)
legend("topright", legend = c("no boundary correction", "left boundary correction"),
       col=c("red", "blue"), lwd = 3, cex=0.8)

The two lines become more different near the left boundary. Because no boundary correction was applied in the ‘red’ case, while the ‘blue’ case did left boundary correction.

(6)

data_WF <- data %>% filter(race == 1, gender == 2)
time1 <- data_WF$time
status1 <- data_WF$death


est_km1 <- survfit(Surv(time1, status1) ~ 1, conf.int = 0.90,conf.type = "plain")
plot(est_km1, main = "90% Confidence interval without transformation(WF)", xlab = "Time", ylab = "Estimated survival function")

res2 <- muhaz(time1, status1, bw.grid = 365, kern = "biquadratic", bw.method = "global", b.cor = "none")
plot(res2, main = "Kernel-smoothed estimate with b=365(WF)", col = "red", lwd = 2)

On the whole, the survival function of white female patients is much flatter than that of black female patients, indicating that their survival probability is higher. At the same time, their hazard rate at every time points is very low, remaining below 0.00012.

(7)

data_B <- data %>% filter(race == 2)
data_W <- data %>% filter(race == 1)
t1 <- data_B$time
t2 <- data_W$time
s1 <- data_B$death
s2 <- data_W$death

time_tot <- c(t1, t2)
status_tot <- c(s1, s2)
stain <- rep(c("black", "white"), times = c(151, 712))
skin_color = data.frame(time = time_tot, status = status_tot, stain = stain)
survdiff(Surv(time, status) ~ stain, data = skin_color)
## Call:
## survdiff(formula = Surv(time, status) ~ stain, data = skin_color)
## 
##               N Observed Expected (O-E)^2/E (O-E)^2/V
## stain=black 151       28     23.4     0.925      1.11
## stain=white 712      112    116.6     0.185      1.11
## 
##  Chisq= 1.1  on 1 degrees of freedom, p= 0.3

Since the p-value is 0.3(>0.05), we can not conclude that the survival functions of black and white patients are significantly different.

(8)

lrt_strat <- survdiff(Surv(time, death != 0) ~ race + strata(gender), data = data)
lrt_strat
## Call:
## survdiff(formula = Surv(time, death != 0) ~ race + strata(gender), 
##     data = data)
## 
##          N Observed Expected (O-E)^2/E (O-E)^2/V
## race=1 712      112    116.7     0.188      1.13
## race=2 151       28     23.3     0.942      1.13
## 
##  Chisq= 1.1  on 1 degrees of freedom, p= 0.3

Since the p-value is 0.3(>0.05), we still can not conclude that the survival functions of black and white patients are significantly different after taking account of gender.

(9)

female:

time_tot <- c(time, time1)
status_tot <- c(status, status1)
stain <- rep(c("black_female", "white_female"), times = c(59, 280))
skin_color = data.frame(time = time_tot, status = status_tot, stain = stain)
survdiff(Surv(time, status) ~ stain, data = skin_color)
## Call:
## survdiff(formula = Surv(time, status) ~ stain, data = skin_color)
## 
##                      N Observed Expected (O-E)^2/E (O-E)^2/V
## stain=black_female  59       14     8.21     4.076      4.85
## stain=white_female 280       39    44.79     0.748      4.85
## 
##  Chisq= 4.8  on 1 degrees of freedom, p= 0.03

male:

data_BM <- data %>% filter(race == 2, gender == 1)
data_WM <- data %>% filter(race == 1, gender == 1)
time1 <- data_BM$time
time2 <- data_WM$time
status1 <- data_BM$death
status2 <- data_WM$death

time_tot <- c(time1, time2)
status_tot <- c(status1, status2)
stain <- rep(c("black_male", "white_male"), times = c(92, 432))
skin_color = data.frame(time = time_tot, status = status_tot, stain = stain)
survdiff(Surv(time, status) ~ stain, data = skin_color)
## Call:
## survdiff(formula = Surv(time, status) ~ stain, data = skin_color)
## 
##                    N Observed Expected (O-E)^2/E (O-E)^2/V
## stain=black_male  92       14     15.1    0.0801     0.097
## stain=white_male 432       73     71.9    0.0168     0.097
## 
##  Chisq= 0.1  on 1 degrees of freedom, p= 0.8

We got different results based on three situation. The survival functions of black and white patients are only shown significantly different when it was based on the data of female patients. This shows that the whole data set is not complete enough.