setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW1")
data <- read.csv("kidneyTrans.csv")
library(survival)
library(ggplot2)
## Warning: 程辑包'ggplot2'是用R版本4.2.3 来建造的
model1 <- coxph(Surv(time, death)~factor(race), data = data)
summary(model1)
## Call:
## coxph(formula = Surv(time, death) ~ factor(race), data = data)
##
## n= 863, number of events= 140
##
## coef exp(coef) se(coef) z Pr(>|z|)
## factor(race)2 0.2224 1.2491 0.2114 1.052 0.293
##
## exp(coef) exp(-coef) lower .95 upper .95
## factor(race)2 1.249 0.8006 0.8253 1.89
##
## Concordance= 0.508 (se = 0.017 )
## Likelihood ratio test= 1.06 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
model2 <- coxph(Surv(time, death)~factor(race)*factor(gender), data)
summary(model2)
## Call:
## coxph(formula = Surv(time, death) ~ factor(race) * factor(gender),
## data = data)
##
## n= 863, number of events= 140
##
## coef exp(coef) se(coef) z Pr(>|z|)
## factor(race)2 -0.08878 0.91504 0.29182 -0.304 0.7609
## factor(gender)2 -0.24848 0.77998 0.19854 -1.252 0.2107
## factor(race)2:factor(gender)2 0.74549 2.10747 0.42711 1.745 0.0809 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## factor(race)2 0.915 1.0928 0.5165 1.621
## factor(gender)2 0.780 1.2821 0.5285 1.151
## factor(race)2:factor(gender)2 2.107 0.4745 0.9124 4.868
##
## Concordance= 0.54 (se = 0.024 )
## Likelihood ratio test= 4.37 on 3 df, p=0.2
## Wald test = 4.64 on 3 df, p=0.2
## Score (logrank) test = 4.74 on 3 df, p=0.2
According to the outcomes above, we can see that the factor
race is significant in model2 but not significant in
model1.For black female people: \(\lambda_{BF}(t)=\lambda_0(t)exp({\beta_1})\)
For white female people: \(\lambda_{WF}(t)=\lambda_0(t)\)
Therefore, we obtain \(\beta_1=\frac{\lambda_{BF}(t)}{\lambda_{WF}(t)}\)cumhaz0_2 <- basehaz(model2, centered = FALSE)
## Warning in survfit.coxph(fit, se.fit = FALSE): the model contains interactions;
## the default curve based on columm means of the X matrix is almost certainly not
## useful. Consider adding a newdata argument.
survprob <- data.frame(time = cumhaz0_2$time, surv0 = exp(-cumhaz0_2$hazard),
surv1 = exp(-exp(model2$coefficients[1]) * cumhaz0_2$hazard),
surv2 = exp(-exp(model2$coefficients[2]) * cumhaz0_2$hazard),
surv3 = exp(-exp(model2$coefficients[1]+model2$coefficients[2]+model2$coefficients[3]) * cumhaz0_2$hazard))
ggplot(survprob, aes(time)) +
geom_step(aes(y = surv0, colour = "White Female")) +
geom_step(aes(y = surv1, colour = "Black Female")) +
geom_step(aes(y = surv2, colour = "White Male")) +
geom_step(aes(y = surv3, colour = "Black Male")) +
labs(title = "Cox PH model esimate for Model2", x = "Time (days)", y = "Estimated survival function") +
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))
model <- coxph(Surv(time, death)~factor(race)*factor(gender)+pspline(age), data)
summary(model)
## Call:
## coxph(formula = Surv(time, death) ~ factor(race) * factor(gender) +
## pspline(age), data = data)
##
## n= 863, number of events= 140
##
## coef se(coef) se2 Chisq DF p
## factor(race)2 -0.14143 0.294030 0.293595 0.23 1.00 6.3e-01
## factor(gender)2 -0.09948 0.199479 0.199275 0.25 1.00 6.2e-01
## pspline(age), linear 0.05027 0.007538 0.007538 44.48 1.00 2.6e-11
## pspline(age), nonlin 3.75 3.03 2.9e-01
## factor(race)2:factor(gend 0.58188 0.427803 0.427542 1.85 1.00 1.7e-01
##
## exp(coef) exp(-coef) lower .95 upper .95
## factor(race)2 0.8681 1.151917 0.48787 1.545e+00
## factor(gender)2 0.9053 1.104594 0.61235 1.338e+00
## ps(age)3 1.9824 0.504434 0.15770 2.492e+01
## ps(age)4 3.9206 0.255062 0.04850 3.169e+02
## ps(age)5 7.5000 0.133334 0.02818 1.996e+03
## ps(age)6 12.4137 0.080556 0.02694 5.721e+03
## ps(age)7 17.1172 0.058421 0.03420 8.568e+03
## ps(age)8 32.9979 0.030305 0.07077 1.539e+04
## ps(age)9 60.5622 0.016512 0.13372 2.743e+04
## ps(age)10 62.7202 0.015944 0.13778 2.855e+04
## ps(age)11 77.1365 0.012964 0.16853 3.531e+04
## ps(age)12 151.1207 0.006617 0.32603 7.005e+04
## ps(age)13 276.8249 0.003612 0.51795 1.480e+05
## ps(age)14 488.2104 0.002048 0.53594 4.447e+05
## factor(race)2:factor(gend 1.7894 0.558846 0.77368 4.139e+00
##
## Iterations: 3 outer, 9 Newton-Raphson
## Theta= 0.6480586
## Degrees of freedom for terms= 1 1 4 1
## Concordance= 0.676 (se = 0.022 )
## Likelihood ratio test= 64.05 on 7.02 df, p=2e-11
Although the p-value of the non-linear term of age is 0.29(>0.05) which is not significant, the linear term is significant. Thus, we can add age as a linear term into Model2.
model <- coxph(Surv(time, death)~factor(race)*factor(gender)+age, data)
final <- step(model, scope = list(upper = ~factor(race)*factor(gender)+age, lower = ~1))
## Start: AIC=1707.4
## Surv(time, death) ~ factor(race) * factor(gender) + age
##
## Df AIC
## - factor(race):factor(gender) 1 1707.3
## <none> 1707.4
## - age 1 1760.0
##
## Step: AIC=1707.28
## Surv(time, death) ~ factor(race) + factor(gender) + age
##
## Df AIC
## - factor(gender) 1 1705.3
## - factor(race) 1 1705.6
## <none> 1707.3
## + factor(race):factor(gender) 1 1707.4
## - age 1 1761.0
##
## Step: AIC=1705.3
## Surv(time, death) ~ factor(race) + age
##
## Df AIC
## - factor(race) 1 1703.6
## <none> 1705.3
## + factor(gender) 1 1707.3
## - age 1 1759.3
##
## Step: AIC=1703.6
## Surv(time, death) ~ age
##
## Df AIC
## <none> 1703.6
## + factor(race) 1 1705.3
## + factor(gender) 1 1705.6
## - age 1 1758.3
According the outcome above, we consider the model only has the linear term of age as the final best model.
setwd("C:/Users/hp/Desktop/看看我/课程/生存分析/HW2")
data1 <- read.csv("larynxCancer.csv")
model1 <- coxph(Surv(time, death)~factor(stage), data1)
summary(model1)
## Call:
## coxph(formula = Surv(time, death) ~ factor(stage), data = data1)
##
## n= 90, number of events= 50
##
## coef exp(coef) se(coef) z Pr(>|z|)
## factor(stage)2 0.06481 1.06696 0.45843 0.141 0.8876
## factor(stage)3 0.61481 1.84930 0.35519 1.731 0.0835 .
## factor(stage)4 1.73490 5.66838 0.41939 4.137 3.52e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## factor(stage)2 1.067 0.9372 0.4344 2.62
## factor(stage)3 1.849 0.5407 0.9219 3.71
## factor(stage)4 5.668 0.1764 2.4916 12.90
##
## Concordance= 0.668 (se = 0.037 )
## Likelihood ratio test= 16.49 on 3 df, p=9e-04
## Wald test = 19.24 on 3 df, p=2e-04
## Score (logrank) test = 22.88 on 3 df, p=4e-05
The model is the Cox proportional model to the time to death of the male
larynx cancer patients with only stage.
The hazard ratios: stage II vs. stage I: \(\hat{\beta_1}=0.06481\)
stage III vs. stage I: \(\hat{\beta_2}=0.61481\)
stage IV vs. stage I: \(\hat{\beta_3}=1.73490\)
Wald Test:
model2 <- coxph(Surv(time, death)~factor(stage) + age, data1)
beta <- model2$coefficients[1:3]
I_var <- model2$var[1:3, 1:3]
z2_wald <- as.numeric(t(beta) %*% solve(I_var) %*% beta)
p_value <- 1-pchisq(z2_wald, df = 3)
p_value
## [1] 0.0004566683
By the Wald Test, we got the p-value=0.0004566683 < 0.05. Therefore,
we can reject the null hypothesis.
Likelihood Ratio Tests:
model3 <- coxph(Surv(time, death)~ age, data1)
z2_lr <- 2 * (model2$loglik[-1] - model3$loglik[1])
p_lr <- 1 - pchisq(z2_lr, df = 1)
anova(model2, model3)
## Analysis of Deviance Table
## Cox model: response is Surv(time, death)
## Model 1: ~ factor(stage) + age
## Model 2: ~ age
## loglik Chisq Df P(>|Chi|)
## 1 -187.71
## 2 -195.55 15.681 3 0.001318 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
By the Likelihood Ratio Test, we got the p-value=0.001318 < 0.05.
Therefore, we can reject the null hypothesis.
We conclude that there is no difference in survival between patients
with different stages of disease.
C <- matrix(c(1, 0, -1, 1, 0, -1, 0, 0), ncol = 4)
beta <- model2$coefficients[1:4]
I_var <- model2$var[1:4, 1:4]
z2_wald <- as.numeric(t(C %*% beta) %*% solve(C %*% I_var %*% t(C)) %*% C %*% beta)
p_value <- 1-pchisq(z2_wald, df = 2)
p_value
## [1] 0.00429843
The p_value=0.00429843(<0.05), so we reject the null hypothesis and
consider \(H_1\) to be true.
For Likelihood Ratio Tests:
for (i in 1:90) {
if (data1$stage[i] != 1) {
data1$stage[i] <- 0
}
}
model4 <- coxph(Surv(time, death)~factor(stage) + age, data1)
anova(model2, model4)
## Analysis of Deviance Table
## Cox model: response is Surv(time, death)
## Model 1: ~ factor(stage) + age
## Model 2: ~ factor(stage) + age
## loglik Chisq Df P(>|Chi|)
## 1 -187.71
## 2 -192.74 10.057 2 0.006548 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The p-value=0.006548(<0.05), so we got the same conclusion as Wald test.