Question 3.

To fit the oringinal model:

library(knitr)
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)
library(ggplot2)
setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW3")
data <- read.csv("prostateCancerSub.csv")

death <- numeric(nrow(data))
for (i in 1:length(death)) {
  if (data$Status[i] == "alive") {
    death[i] <- 0
  } else {
    death[i] <- 1
  }
}

data$Trt <- factor(data$Trt, levels = c("placebo", "0.2 mg estrogen", "1.0 mg estrogen", "5.0 mg estrogen"))
data <- cbind(data, death)
model <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1), data)

(a)

The martingale residuals against Serum hemoglobin (hg):

r_M0 <- residuals(model)
ggplot(data.frame(x = data$hg, y = r_M0), aes(x = x, y = y)) + 
  geom_point() + geom_smooth() + 
  labs(title = "Martingale Residuals", x = "Serum hemoglobin", y = "martingale residual")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

The figure shows that adding hg as a linear term into the model is not appropriate. Therefore, we consider the nonlinear form.

model1 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + hg, data)
model2 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + poly(hg, 2), data)
func <- function(x) {cbind(x, (x - 16) * (x - 16 > 0))} # a piecewise linear model with a changepoint at 16
model3 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg), data)

AIC:

aic <- data.frame(Model = c("model", "model1", "model2", "model3"), AIC = c(AIC(model), AIC(model1), AIC(model2), AIC(model3)))

aic %>% kable()
Model AIC
model 3995.847
model1 3984.773
model2 3980.339
model3 3975.313

According to the output above, we find that model3 has the smallest AIC. Therefore, we choose the functional form which is a piecewise linear model with a changepoint at 16.

(b)

r_M0 <- residuals(model)
ggplot(data.frame(x = data$sz, y = r_M0), aes(x = x, y = y)) + 
  geom_point() + geom_smooth() + 
  labs(title = "Martingale Residuals", x = " Tumor size", y = "martingale residual")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

The figure shows that adding sz as a linear term into the model is appropriate. Therefore, we consider the linear form.

model4 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz, data)

Therefore, we choose to add sz as a linear term.

(c)

r_M0 <- residuals(model)
ggplot(data.frame(x = data$Age, y = r_M0), aes(x = x, y = y)) + 
  geom_point() + geom_smooth() + 
  labs(title = "Martingale Residuals", x = "Age", y = "martingale residual")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

The figure shows that adding hg as a linear term into the model is not appropriate. Therefore, we consider the nonlinear form.

model5 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz + Age, data)
model6 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz + poly(Age, 2), data)
func1 <- function(x) {cbind(x, (x - 75) * (x - 75 > 0))} # a piecewise linear model with a changepoint at 75
model7 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz + func1(Age), data)
func2 <- function(x){pmax(x, 75)}
model8 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz + func2(Age), data)

AIC:

aic <- data.frame(Model = c("model", "model5", "model6", "model7", "model8"), AIC = c(AIC(model), AIC(model5), AIC(model6), AIC(model7), AIC(model8)))

aic %>% kable()
Model AIC
model 3995.847
model5 3958.192
model6 3954.400
model7 3952.504
model8 3950.531

According to the output above, we find that model4 has the smallest AIC. Therefore, we choose the functional form which is Piecewise Linear Age with changepoint at 75 and no effect before 75.

Effects on survival:

summary(model8)
## Call:
## coxph(formula = Surv(Time, death != 0) ~ Trt + I(Stage == 4) + 
##     I(hx == 1) + func(hg) + sz + func2(Age), data = data)
## 
##   n= 502, number of events= 354 
## 
##                         coef exp(coef)  se(coef)      z Pr(>|z|)    
## Trt0.2 mg estrogen  0.007205  1.007231  0.146017  0.049 0.960646    
## Trt1.0 mg estrogen -0.426573  0.652742  0.159158 -2.680 0.007358 ** 
## Trt5.0 mg estrogen -0.040963  0.959864  0.148158 -0.276 0.782177    
## I(Stage == 4)TRUE   0.226188  1.253811  0.112492  2.011 0.044358 *  
## I(hx == 1)TRUE      0.459816  1.583782  0.109354  4.205 2.61e-05 ***
## func(hg)x          -0.125971  0.881640  0.030627 -4.113 3.90e-05 ***
## func(hg)            0.662232  1.939115  0.171939  3.852 0.000117 ***
## sz                  0.018732  1.018909  0.004326  4.330 1.49e-05 ***
## func2(Age)          0.091362  1.095666  0.024169  3.780 0.000157 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##                    exp(coef) exp(-coef) lower .95 upper .95
## Trt0.2 mg estrogen    1.0072     0.9928    0.7566    1.3410
## Trt1.0 mg estrogen    0.6527     1.5320    0.4778    0.8917
## Trt5.0 mg estrogen    0.9599     1.0418    0.7180    1.2833
## I(Stage == 4)TRUE     1.2538     0.7976    1.0057    1.5631
## I(hx == 1)TRUE        1.5838     0.6314    1.2782    1.9624
## func(hg)x             0.8816     1.1342    0.8303    0.9362
## func(hg)              1.9391     0.5157    1.3844    2.7162
## sz                    1.0189     0.9814    1.0103    1.0276
## func2(Age)            1.0957     0.9127    1.0450    1.1488
## 
## Concordance= 0.643  (se = 0.016 )
## Likelihood ratio test= 88.98  on 9 df,   p=3e-15
## Wald test            = 91.54  on 9 df,   p=8e-16
## Score (logrank) test = 94.73  on 9 df,   p=<2e-16
  1. \(\hat{\beta}_6=-0.125971\), which means that the greater serum hemoglobin (below 16), the lower hazard of death.

  2. \(\hat{\beta}_7=0.662232\), which means that the greater serum hemoglobin (beyond 16), the greater hazard of death.

  3. \(\hat{\beta}_8=0.018732\), which means that the greater tumor size, the greater hazard of death.

4.\(\hat{\beta}_9=0.091362\), which means that the greater age (beyond 75), the greater hazard of death. There is no effect on survival with the age under 75.

Question 4.

(a)

res <- cox.zph(model8)
res
##               chisq df     p
## Trt           2.932  3 0.402
## I(Stage == 4) 0.965  1 0.326
## I(hx == 1)    0.327  1 0.567
## func(hg)      5.291  2 0.071
## sz            0.437  1 0.509
## func2(Age)    1.089  1 0.297
## GLOBAL        8.974  9 0.440

For the significance level 0.05, no covariate violates the PH assumption.

plot(res)

From the plot of scaled Schoenfeld residuals above, they only deviate slightly from the horizontal line. Therefore, we can conclude that the problem for the violation of the PH assumption is not serious.

(b)

r <- residuals(model8, type = 'dfbetas') # standardized version

df <- data.frame(index = 1:(dim(r)[1]), status = factor(death),
                    db_hg1 = r[, 6], db_hg2 = r[, 7], db_sz = r[, 8], db_Age = r[, 9])

# Delta-beta residuals for Serum hemoglobin1
ggplot(df, aes(index, db_hg1, color = status)) + 
  geom_linerange(ymin = 0, ymax = df$db_hg1) +
  labs(title = "Delta-beta Residuals for  Serum hemoglobin1", x = "index", y = "delta-beta residual") + 
  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))

# Serum hemoglobin2
ggplot(df, aes(index, db_hg2, color = status)) + 
  geom_linerange(ymin = 0, ymax = df$db_hg2) +
  labs(title = "Delta-beta Residuals for Serum hemoglobin2", x = "index", y = "delta-beta residual") + 
  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))

# Tumor size
ggplot(df, aes(index, db_sz, color = status)) + 
  geom_linerange(ymin = 0, ymax = df$db_sz) +
  labs(title = "Delta-beta Residuals for Tumor size", x = "index", y = "delta-beta residual") + 
  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))

# Age
ggplot(df, aes(index, db_Age, color = status)) + 
  geom_linerange(ymin = 0, ymax = df$db_Age) +
  labs(title = "Delta-beta Residuals for Age", x = "index", y = "delta-beta residual") + 
  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))

According to the plot above, we find that the scaled delta-beta residual plot of \(\beta_7\) and \(\beta_9\) show an influential observation.

model8
## Call:
## coxph(formula = Surv(Time, death != 0) ~ Trt + I(Stage == 4) + 
##     I(hx == 1) + func(hg) + sz + func2(Age), data = data)
## 
##                         coef exp(coef)  se(coef)      z        p
## Trt0.2 mg estrogen  0.007205  1.007231  0.146017  0.049 0.960646
## Trt1.0 mg estrogen -0.426573  0.652742  0.159158 -2.680 0.007358
## Trt5.0 mg estrogen -0.040963  0.959864  0.148158 -0.276 0.782177
## I(Stage == 4)TRUE   0.226188  1.253811  0.112492  2.011 0.044358
## I(hx == 1)TRUE      0.459816  1.583782  0.109354  4.205 2.61e-05
## func(hg)x          -0.125971  0.881640  0.030627 -4.113 3.90e-05
## func(hg)            0.662232  1.939115  0.171939  3.852 0.000117
## sz                  0.018732  1.018909  0.004326  4.330 1.49e-05
## func2(Age)          0.091362  1.095666  0.024169  3.780 0.000157
## 
## Likelihood ratio test=88.98  on 9 df, p=2.612e-15
## n= 502, number of events= 354
df %>% filter(db_hg2 > 0.15)
##     index status      db_hg1    db_hg2        db_sz       db_Age
## 502   502      1 -0.02885859 0.1596201 -0.005267057 -0.004569443
model9 <- coxph(Surv(Time, death != 0) ~ Trt + I(Stage == 4) + I(hx == 1) + func(hg) + sz + func2(Age), data=data[-502,])
model9
## Call:
## coxph(formula = Surv(Time, death != 0) ~ Trt + I(Stage == 4) + 
##     I(hx == 1) + func(hg) + sz + func2(Age), data = data[-502, 
##     ])
## 
##                         coef exp(coef)  se(coef)      z        p
## Trt0.2 mg estrogen  0.008534  1.008571  0.146050  0.058 0.953402
## Trt1.0 mg estrogen -0.426210  0.652979  0.159197 -2.677 0.007423
## Trt5.0 mg estrogen -0.039562  0.961211  0.148220 -0.267 0.789537
## I(Stage == 4)TRUE   0.221009  1.247335  0.113484  1.947 0.051475
## I(hx == 1)TRUE      0.462036  1.587302  0.109594  4.216 2.49e-05
## func(hg)x          -0.124230  0.883177  0.031071 -3.998 6.38e-05
## func(hg)            0.605786  1.832693  0.244718  2.475 0.013307
## sz                  0.018782  1.018960  0.004335  4.332 1.48e-05
## func2(Age)          0.091625  1.095954  0.024177  3.790 0.000151
## 
## Likelihood ratio test=86.01  on 9 df, p=1.025e-14
## n= 501, number of events= 353

The p-value of \(\hat{\beta}_7\) increases from 0.000117 to 0.013307 after removing the influential observation, which means it becomes less significant.

(c)

cox_snell <- function(fit) {
  cumhaz0 <- basehaz(fit) # get the baseline hazard
  Lambda0 <- approxfun(cumhaz0$time, cumhaz0$hazard, method = 'constant')
  r_C <- Lambda0(fit$y[,1]) * exp(fit$linear.predictors)
  # obtain the KM estimate of the residuals
  km_rC <- survfit(Surv(r_C, fit$y[,2]) ~ 1)
  
  df <- data.frame(x = km_rC$time, y = -log(km_rC$surv), yl = -log(km_rC$upper), 
                    yu = -log(km_rC$lower))
  df <- df[km_rC$n.risk >= 10,] # keep the estimate when at least 10 individuals are at risk
  
  figure <- ggplot(df, aes(x)) +
    geom_step(aes(y = y), colour = 'blue') + 
    geom_step(aes(y = yu), lty = 2) + geom_step(aes(y = yl), lty = 2) + 
    geom_line(aes(y = x), colour = 'red') + 
    labs(title = "Cox-Snell Residuals", x = "Residual", y = "cumulative hazard") + 
    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))
  
  return(figure)
}

cox_snell(model8)

The cumulative hazard of the Cox–Snell residuals is closed to the line with 1 slope and 0 intercept. Therefore, we can conclude that the overall goodness of fit of our model is good.

(d)

By the deviance residual:

r_M <- residuals(model8)
r_D <- residuals(model8, type = 'deviance')
df <- data.frame(risk_score = model8$linear.predictors, status = as.factor(model8$y[, 2]), r_D = r_D)

ggplot(df, aes(x = risk_score, y = r_D, color = status)) +
  geom_point() + 
  labs(title = "Deviance Residuals", x = "risk score", y = "deviance residual") + 
  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))

From the plot of the deviance residual above, all observations are between (-3, 3), so there is no outlier.