# Biostatistics III in R

#### Code by Andreas Karlsson and Mark Clements

# Cox model for cause-specific mortality for melanoma (all stages)

Use Cox regression to model the cause-specific survival of patients with skin melanoma (including all stages).

You may have to install the required packages the first time you use them. You can install a package by `install.packages("package_of_interest")` for each package you require.

    library(biostat3)

Load the melanoma data and explore it.

    data(melanoma)
    melanoma <- transform(melanoma,
                          death_cancer = ifelse( status == "Dead: cancer", 1, 0))

## (a)

Without adjusting for potential confounders, we see that females have a 37% lower mortality rate than males.

    # Cox regression with time-since-entry as the timescale
    # Note that R uses the Efron method for approximating the likelihood in the presence of ties,
    # whereas Stata (and some other software) uses the Breslow method
    cox1 <- coxph(Surv(surv_mm, death_cancer) ~ sex, data=melanoma)
    summary(cox1)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ sex, data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##               coef exp(coef) se(coef)      z Pr(>|z|)
    ## sexFemale -0.46741   0.62662  0.04612 -10.13   <2e-16 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##           exp(coef) exp(-coef) lower .95 upper .95
    ## sexFemale    0.6266      1.596    0.5725    0.6859
    ##
    ## Concordance= 0.562  (se = 0.006 )
    ## Rsquare= 0.013   (max possible= 0.985 )
    ## Likelihood ratio test= 103.7  on 1 df,   p=0
    ## Wald test            = 102.7  on 1 df,   p=0
    ## Score (logrank) test = 104.6  on 1 df,   p=0

## (b)

After adjusting for a range of potential confounders, we see that the estimated difference in cancer-specific mortality between males and females has decreased slightly but there is still quite a large difference.

    cox2 <- coxph(Surv(surv_mm, death_cancer) ~ sex + agegrp + stage + subsite + year8594, data=melanoma)
    summary(cox2)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ sex + agegrp +
    ##     stage + subsite + year8594, data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##                             coef exp(coef) se(coef)      z Pr(>|z|)
    ## sexFemale               -0.28919   0.74887  0.04865 -5.944 2.78e-09 ***
    ## agegrp45-59              0.23835   1.26915  0.06745  3.534  0.00041 ***
    ## agegrp60-74              0.54980   1.73291  0.06510  8.445  < 2e-16 ***
    ## agegrp75+                1.02773   2.79472  0.07641 13.451  < 2e-16 ***
    ## stageLocalised           0.03773   1.03845  0.06869  0.549  0.58287
    ## stageRegional            1.56895   4.80158  0.09144 17.158  < 2e-16 ***
    ## stageDistant             2.62087  13.74770  0.08137 32.209  < 2e-16 ***
    ## subsiteTrunk             0.33488   1.39777  0.07066  4.740 2.14e-06 ***
    ## subsiteLimbs             0.03379   1.03437  0.07436  0.454  0.64951
    ## subsiteMultiple and NOS  0.27000   1.30997  0.10230  2.639  0.00831 **
    ## year8594Diagnosed 85-94 -0.24037   0.78633  0.04791 -5.017 5.24e-07 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##                         exp(coef) exp(-coef) lower .95 upper .95
    ## sexFemale                  0.7489    1.33535    0.6808    0.8238
    ## agegrp45-59                1.2692    0.78793    1.1120    1.4485
    ## agegrp60-74                1.7329    0.57706    1.5253    1.9688
    ## agegrp75+                  2.7947    0.35782    2.4060    3.2462
    ## stageLocalised             1.0384    0.96298    0.9076    1.1881
    ## stageRegional              4.8016    0.20826    4.0137    5.7441
    ## stageDistant              13.7477    0.07274   11.7211   16.1248
    ## subsiteTrunk               1.3978    0.71543    1.2170    1.6054
    ## subsiteLimbs               1.0344    0.96677    0.8941    1.1967
    ## subsiteMultiple and NOS    1.3100    0.76338    1.0720    1.6008
    ## year8594Diagnosed 85-94    0.7863    1.27172    0.7159    0.8637
    ##
    ## Concordance= 0.755  (se = 0.007 )
    ## Rsquare= 0.212   (max possible= 0.985 )
    ## Likelihood ratio test= 1854  on 11 df,   p=0
    ## Wald test            = 2579  on 11 df,   p=0
    ## Score (logrank) test = 4112  on 11 df,   p=0

## (c)

Let’s first estimate the effect of gender for each age group without adjusting for confounders.

    cox3 <- coxph(Surv(surv_mm, death_cancer) ~ agegrp + agegrp * sex, data=melanoma)
    summary(cox3)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ agegrp + agegrp *
    ##     sex, data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##                           coef exp(coef) se(coef)      z Pr(>|z|)
    ## agegrp45-59            0.18031   1.19759  0.08501  2.121 0.033927 *
    ## agegrp60-74            0.40466   1.49879  0.08462  4.782 1.74e-06 ***
    ## agegrp75+              0.84551   2.32916  0.10341  8.176 3.33e-16 ***
    ## sexFemale             -0.78242   0.45730  0.10444 -7.491 6.82e-14 ***
    ## agegrp45-59:sexFemale  0.18821   1.20709  0.13870  1.357 0.174790
    ## agegrp60-74:sexFemale  0.44362   1.55835  0.13112  3.383 0.000716 ***
    ## agegrp75+:sexFemale    0.38773   1.47363  0.14855  2.610 0.009054 **
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##                       exp(coef) exp(-coef) lower .95 upper .95
    ## agegrp45-59              1.1976     0.8350    1.0138    1.4147
    ## agegrp60-74              1.4988     0.6672    1.2697    1.7692
    ## agegrp75+                2.3292     0.4293    1.9019    2.8525
    ## sexFemale                0.4573     2.1868    0.3726    0.5612
    ## agegrp45-59:sexFemale    1.2071     0.8284    0.9198    1.5842
    ## agegrp60-74:sexFemale    1.5583     0.6417    1.2052    2.0150
    ## agegrp75+:sexFemale      1.4736     0.6786    1.1014    1.9717
    ##
    ## Concordance= 0.622  (se = 0.007 )
    ## Rsquare= 0.042   (max possible= 0.985 )
    ## Likelihood ratio test= 332.6  on 7 df,   p=0
    ## Wald test            = 307.7  on 7 df,   p=0
    ## Score (logrank) test = 333.9  on 7 df,   p=0

We see that there is some evidence that the survival advantage experienced by females depends on age. The hazard ratio for males/females in the youngest age group is 0.46, while in the highest age group the hazard ratio is 0.68. There is evidence that the hazard ratios for gender differ across the age groups (p=0.0037). However, after adjusting for stage, subsite, and period there is no longer evidence of an interaction.

There is not strong evidence in support of the hypothesis (although some may consider that there is weak evidence).

## (d)

After having fitted a main effects model we can check the proportional hazards assumption by fitting a regression line through the model-based Schoenfeld residulas and check if the slope is statistically different from zero.

There is strong evidence that the proportional hazard assumption is not satisfied for the effect of stage. Unless our primary interest is in the stage effect we can fit a stratified Cox model where we stratify on stage (i.e. estimate a separate baseline hazard function for each stage group).

    cox4 <- coxph(Surv(surv_mm, death_cancer) ~ sex + year8594 + agegrp + subsite + stage, data=melanoma)
    summary(cox4)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ sex + year8594 +
    ##     agegrp + subsite + stage, data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##                             coef exp(coef) se(coef)      z Pr(>|z|)
    ## sexFemale               -0.28919   0.74887  0.04865 -5.944 2.78e-09 ***
    ## year8594Diagnosed 85-94 -0.24037   0.78633  0.04791 -5.017 5.24e-07 ***
    ## agegrp45-59              0.23835   1.26915  0.06745  3.534  0.00041 ***
    ## agegrp60-74              0.54980   1.73291  0.06510  8.445  < 2e-16 ***
    ## agegrp75+                1.02773   2.79472  0.07641 13.451  < 2e-16 ***
    ## subsiteTrunk             0.33488   1.39777  0.07066  4.740 2.14e-06 ***
    ## subsiteLimbs             0.03379   1.03437  0.07436  0.454  0.64951
    ## subsiteMultiple and NOS  0.27000   1.30997  0.10230  2.639  0.00831 **
    ## stageLocalised           0.03773   1.03845  0.06869  0.549  0.58287
    ## stageRegional            1.56895   4.80158  0.09144 17.158  < 2e-16 ***
    ## stageDistant             2.62087  13.74770  0.08137 32.209  < 2e-16 ***
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##                         exp(coef) exp(-coef) lower .95 upper .95
    ## sexFemale                  0.7489    1.33535    0.6808    0.8238
    ## year8594Diagnosed 85-94    0.7863    1.27172    0.7159    0.8637
    ## agegrp45-59                1.2692    0.78793    1.1120    1.4485
    ## agegrp60-74                1.7329    0.57706    1.5253    1.9688
    ## agegrp75+                  2.7947    0.35782    2.4060    3.2462
    ## subsiteTrunk               1.3978    0.71543    1.2170    1.6054
    ## subsiteLimbs               1.0344    0.96677    0.8941    1.1967
    ## subsiteMultiple and NOS    1.3100    0.76338    1.0720    1.6008
    ## stageLocalised             1.0384    0.96298    0.9076    1.1881
    ## stageRegional              4.8016    0.20826    4.0137    5.7441
    ## stageDistant              13.7477    0.07274   11.7211   16.1248
    ##
    ## Concordance= 0.755  (se = 0.007 )
    ## Rsquare= 0.212   (max possible= 0.985 )
    ## Likelihood ratio test= 1854  on 11 df,   p=0
    ## Wald test            = 2579  on 11 df,   p=0
    ## Score (logrank) test = 4112  on 11 df,   p=0

    ## Test proportional hazards assumption
    cox.zph(cox4, transform="log")

    ##                               rho    chisq        p
    ## sexFemale               -1.63e-02 5.18e-01 4.72e-01
    ## year8594Diagnosed 85-94 -2.70e-02 1.42e+00 2.34e-01
    ## agegrp45-59              3.86e-03 2.88e-02 8.65e-01
    ## agegrp60-74              3.99e-03 3.08e-02 8.61e-01
    ## agegrp75+               -5.53e-03 6.02e-02 8.06e-01
    ## subsiteTrunk             2.59e-03 1.35e-02 9.07e-01
    ## subsiteLimbs            -7.45e-04 1.11e-03 9.73e-01
    ## subsiteMultiple and NOS -7.57e-02 1.07e+01 1.04e-03
    ## stageLocalised           8.32e-02 1.32e+01 2.81e-04
    ## stageRegional            5.74e-05 6.27e-06 9.98e-01
    ## stageDistant            -1.45e-01 3.85e+01 5.35e-10
    ## GLOBAL                         NA 2.09e+02 0.00e+00

    cox.zph(cox4, transform="identity") # Stata default

    ##                              rho   chisq        p
    ## sexFemale                0.03148  1.9231 1.66e-01
    ## year8594Diagnosed 85-94 -0.00818  0.1299 7.19e-01
    ## agegrp45-59             -0.00845  0.1381 7.10e-01
    ## agegrp60-74             -0.00902  0.1575 6.91e-01
    ## agegrp75+               -0.02320  1.0579 3.04e-01
    ## subsiteTrunk             0.01630  0.5339 4.65e-01
    ## subsiteLimbs             0.00361  0.0261 8.72e-01
    ## subsiteMultiple and NOS -0.00763  0.1092 7.41e-01
    ## stageLocalised           0.08215 12.8687 3.34e-04
    ## stageRegional           -0.01842  0.6460 4.22e-01
    ## stageDistant            -0.06776  8.3772 3.80e-03
    ## GLOBAL                        NA 84.1461 2.32e-13

If we re-do a test for non-proportional hazards we find that there is no longer evidence that any of the remaining covariates effects seem to depend on time since diagnosis.

Having accounted for the time-dependent effect of stage, there is still no evidence that the effect of sex is modified by age at diagnosis.

    ## Stratified Cox model; separate baseline hazard functions are fit for each strata.
    cox5 <- coxph(Surv(surv_mm, death_cancer) ~ sex + year8594 + agegrp + subsite + strata(stage), data=melanoma)
    summary(cox5)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ sex + year8594 +
    ##     agegrp + subsite + strata(stage), data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##                              coef exp(coef)  se(coef)      z Pr(>|z|)
    ## sexFemale               -0.299867  0.740917  0.048742 -6.152 7.65e-10 ***
    ## year8594Diagnosed 85-94 -0.238915  0.787482  0.047838 -4.994 5.90e-07 ***
    ## agegrp45-59              0.233856  1.263462  0.067463  3.466 0.000527 ***
    ## agegrp60-74              0.552148  1.736981  0.065125  8.478  < 2e-16 ***
    ## agegrp75+                1.017338  2.765823  0.076435 13.310  < 2e-16 ***
    ## subsiteTrunk             0.293094  1.340569  0.070578  4.153 3.28e-05 ***
    ## subsiteLimbs            -0.003252  0.996753  0.074215 -0.044 0.965045
    ## subsiteMultiple and NOS  0.228109  1.256222  0.102565  2.224 0.026146 *
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##                         exp(coef) exp(-coef) lower .95 upper .95
    ## sexFemale                  0.7409     1.3497    0.6734    0.8152
    ## year8594Diagnosed 85-94    0.7875     1.2699    0.7170    0.8649
    ## agegrp45-59                1.2635     0.7915    1.1070    1.4421
    ## agegrp60-74                1.7370     0.5757    1.5288    1.9735
    ## agegrp75+                  2.7658     0.3616    2.3810    3.2128
    ## subsiteTrunk               1.3406     0.7460    1.1674    1.5394
    ## subsiteLimbs               0.9968     1.0033    0.8618    1.1528
    ## subsiteMultiple and NOS    1.2562     0.7960    1.0275    1.5359
    ##
    ## Concordance= 0.645  (se = 0.01 )
    ## Rsquare= 0.035   (max possible= 0.969 )
    ## Likelihood ratio test= 276  on 8 df,   p=0
    ## Wald test            = 279.6  on 8 df,   p=0
    ## Score (logrank) test = 286.5  on 8 df,   p=0

    ## Test proportional hazards assumption
    cox.zph(cox5, transform="log")

    ##                              rho    chisq       p
    ## sexFemale               -0.01143  0.25311 0.61490
    ## year8594Diagnosed 85-94 -0.02319  1.04099 0.30759
    ## agegrp45-59              0.00108  0.00226 0.96207
    ## agegrp60-74             -0.00137  0.00366 0.95177
    ## agegrp75+               -0.00718  0.10146 0.75009
    ## subsiteTrunk             0.01169  0.27337 0.60108
    ## subsiteLimbs             0.00477  0.04535 0.83136
    ## subsiteMultiple and NOS -0.06766  8.53850 0.00348
    ## GLOBAL                        NA 16.62536 0.03426

    cox.zph(cox5, transform="identity")

    ##                              rho  chisq     p
    ## sexFemale                0.03384 2.2209 0.136
    ## year8594Diagnosed 85-94 -0.00479 0.0445 0.833
    ## agegrp45-59             -0.00998 0.1923 0.661
    ## agegrp60-74             -0.01433 0.3977 0.528
    ## agegrp75+               -0.02751 1.4897 0.222
    ## subsiteTrunk             0.01898 0.7199 0.396
    ## subsiteLimbs             0.00527 0.0553 0.814
    ## subsiteMultiple and NOS -0.00748 0.1043 0.747
    ## GLOBAL                        NA 5.2235 0.733

If you have time make sure you check for additional interaction terms between the remaining covariates, i.e. between age at diagnosis and stage.

    cox5 <- coxph(Surv(surv_mm, death_cancer) ~ sex * agegrp + year8594 + agegrp + subsite + strata(stage), data=melanoma)
    summary(cox5)

    ## Call:
    ## coxph(formula = Surv(surv_mm, death_cancer) ~ sex * agegrp +
    ##     year8594 + agegrp + subsite + strata(stage), data = melanoma)
    ##
    ##   n= 7775, number of events= 1913
    ##
    ##                              coef exp(coef)  se(coef)      z Pr(>|z|)
    ## sexFemale               -0.492146  0.611313  0.105915 -4.647 3.37e-06 ***
    ## agegrp45-59              0.158472  1.171719  0.085336  1.857   0.0633 .
    ## agegrp60-74              0.439206  1.551475  0.084962  5.169 2.35e-07 ***
    ## agegrp75+                0.898995  2.457132  0.104912  8.569  < 2e-16 ***
    ## year8594Diagnosed 85-94 -0.238206  0.788040  0.047827 -4.981 6.34e-07 ***
    ## subsiteTrunk             0.299616  1.349341  0.070684  4.239 2.25e-05 ***
    ## subsiteLimbs             0.003931  1.003938  0.074275  0.053   0.9578
    ## subsiteMultiple and NOS  0.236333  1.266596  0.102775  2.300   0.0215 *
    ## sexFemale:agegrp45-59    0.182131  1.199772  0.138904  1.311   0.1898
    ## sexFemale:agegrp60-74    0.269005  1.308662  0.131729  2.042   0.0411 *
    ## sexFemale:agegrp75+      0.265538  1.304133  0.149245  1.779   0.0752 .
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
    ##
    ##                         exp(coef) exp(-coef) lower .95 upper .95
    ## sexFemale                  0.6113     1.6358    0.4967    0.7523
    ## agegrp45-59                1.1717     0.8534    0.9913    1.3850
    ## agegrp60-74                1.5515     0.6445    1.3135    1.8326
    ## agegrp75+                  2.4571     0.4070    2.0004    3.0181
    ## year8594Diagnosed 85-94    0.7880     1.2690    0.7175    0.8655
    ## subsiteTrunk               1.3493     0.7411    1.1748    1.5498
    ## subsiteLimbs               1.0039     0.9961    0.8679    1.1613
    ## subsiteMultiple and NOS    1.2666     0.7895    1.0355    1.5492
    ## sexFemale:agegrp45-59      1.1998     0.8335    0.9138    1.5752
    ## sexFemale:agegrp60-74      1.3087     0.7641    1.0109    1.6942
    ## sexFemale:agegrp75+        1.3041     0.7668    0.9734    1.7473
    ##
    ## Concordance= 0.645  (se = 0.01 )
    ## Rsquare= 0.035   (max possible= 0.969 )
    ## Likelihood ratio test= 280.8  on 11 df,   p=0
    ## Wald test            = 275.6  on 11 df,   p=0
    ## Score (logrank) test = 287.5  on 11 df,   p=0

    anova(cox5)

    ## Analysis of Deviance Table
    ##  Cox model: response is Surv(surv_mm, death_cancer)
    ## Terms added sequentially (first to last)
    ##
    ##            loglik   Chisq Df Pr(>|Chi|)
    ## NULL       -13520
    ## sex        -13501  36.687  1  1.387e-09 ***
    ## agegrp     -13411 180.387  3  < 2.2e-16 ***
    ## year8594   -13400  22.864  1  1.739e-06 ***
    ## subsite    -13382  36.051  3  7.306e-08 ***
    ## sex:agegrp -13379   4.813  3      0.186
    ## ---
    ## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1