# Biostatistics III in R

#### Code by Johan Zetterqvist and Mark Clements

# Exercise 7. Model cause-specific mortality with Poisson regression

In this exercise we model, using Poisson regression, cause-specific mortality of patients diagnosed with localised (stage==1) melanoma.

In exercise 9 we model cause-specific mortality using Cox regression and in exercise 28 we use flexible parametric models. The aim is to illustrate that these three methods are very similar.

The aim of these exercises is to explore the similarities and differences to these three approaches to modelling. We will be comparing the results (and their interpretation) as we proceed through the exercises.

Load the diet data using time-on-study as the timescale.

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)
    library(dplyr)    # for data manipulation
    library(car)      # for car::linearHypothesis -> biostat3::lincom

Load the melanoma data and explore it.

    ## Read melanoma data

    ## Create a new dataset with only localised cancer
    melanoma.l <- filter(biostat3::melanoma, stage=="Localised")
    head( melanoma.l )
    summary(melanoma.l)

Rates can be modelled on different timescales, e.g., attained age, time-since-entry, calendar time. Plot the CHD incidence rates both by attained age and by time-since-entry. Is there a difference? Do the same for CHD hazard by different energy intakes (hieng).

## (a)

### i.

Plot Kaplan-Meier estimates of cause-specific survival as a function of calendar period of diagnosis.

    ## Plot Kaplan-Meier curve using survfit
    ## Create a new event indicator
    melanoma.l <- mutate(melanoma.l,
                       death_cancer = as.numeric(status=="Dead: cancer") )

    ## Create a fitted object for our subcohort
    ## using survfit
    sfit7a1 <- survfit(Surv(surv_mm, event=death_cancer) ~ year8594,
                       data = melanoma.l )

    ## Have a look at the fitted object
    str(sfit7a1, 1)

    ## Plot the survival curve (with some bells and whistles)
    plot(sfit7a1,
         ## No automatic labelling of the curve (we do that ourselves)
         mark.time=F,
         ## Time is measured in months,  but we want to see it in years
         xscale=12,
         ## Make the plot prettier
         xlab="Years since diagnosis",
         ylab="S(t)",
         col=c("blue","red"),
         lty=c("solid","dashed"))
    ## Add legend too
    legend("bottomleft",legend=levels(melanoma.l$year8594),col=c("blue","red"),lty=c("solid","dashed"), bty="n")

    ### TRY IF YOU WANT ###
    if (FALSE) {
        library(survMisc)
        ## Note: `autoplot(sfit7a1)` was broken; I have submitted a pull request to fix this
        ## autoplot(sfit7a1)
        ## alternatively:
        autoplot(sfit7a1, timeTicks = "custom", times= seq(0, 20*12, 5*12))
    }

### ii.

Now plot the estimated hazard function (cause-specific mortality rate) as a function of calendar period of diagnosis.

    ## To plot smoothed hazards, we use the muhaz package (using the muhaz2 wrapper)

    plot(muhaz2(Surv(surv_mm/12, status == "Dead: cancer") ~ year8594, data=melanoma.l),
         xlab="Years since diagnosis", col=c("blue","red"), lty=1:2)

During which calendar period (the early or the latter) is mortality the lowest?

### iii.

Is the interpretation (with respect to how prognosis depends on period) based on the hazard consistent with the interpretation of the survival plot?

    ## Compare with Kaplan-Meier plot
    par(mfrow=c(1,2)) ## Two graphs in the same window

    plot(sfit7a1,
         ## No automatic labelling of the curve (we do that ourselves)
         mark.time=F,
         ## Time is measured in months,  but we want to see it in years
         xscale=12,
         ## Make the plot prettier
         xlab="Years since diagnosis",
         ylab="S(t)",
         col=c("blue","red"),
         lty=c("solid","dashed"))

    plot(muhaz2(Surv(surv_mm/12, status == "Dead: cancer") ~ year8594, data=melanoma.l),
         xlab="Years since diagnosis", col=c("blue","red"),lty=c("solid","dashed"))

## (b)

Estimate the cause-specific mortality rate for each calendar period.

    survRate(Surv(surv_mm, death_cancer) ~ year8594, data=melanoma.l)

During which calendar period (the early or the latter) is mortality the lowest? Is this consistent with what you found earlier? If not, why the inconsistency?

## (c)

The reason for the inconsistency between parts 7a and 7b was confounding by time since diagnosis. The comparison in part 7a was adjusted for time since diagnosis (since we compare the differences between the curves at each point in time) whereas the comparison in part 7b was not. Understanding this concept is central to the remainder of the exercise so please ask for help if you don’t follow.

Two approaches for controlling for confounding are ‘restriction’ and ‘statistical adjustment’. We will first use restriction to control for confounding. We will restrict the potential follow-up time to a maximum of 120 months. Individuals who survive more than 120 months are censored at 120 months.

### i.

Estimate the cause-specific mortality rate for each calendar period.

    ## Calculate the incidence rate by time of diagnosis
    ## but with new variables
    melanoma.l2 <- mutate(melanoma.l,
                          ## Update the death indicator (only count deaths within 120 months)
                          ## death_cancer = death_cancer * as.numeric(surv_mm<=120),
                          death_cancer = ifelse(surv_mm<=120 & status == "Dead: cancer",1,0),
                          ## Create a new time variable
                          ## surv_mm = pmin(surv_mm, 120)
                          surv_mm = ifelse(surv_mm<=120, surv_mm, 120)
                          )

    ## Calculate the rates on the truncated data
    rates_by_diag_yr2 <- survRate(Surv(surv_mm, death_cancer) ~ year8594, data=melanoma.l2)
    rates_by_diag_yr2

During which calendar period (the early of the latter) is mortality the lowest? Is this consistent with what you found in part 7b?

### ii.

Calculate by hand the ratio (85–94/75–84) of the two mortality rates (i.e., a mortality rate ratio) and interpret the estimate (i.e., during which period is mortality higher/lower and by how much).

    ## MRR full data
    rates_by_diag_yr2[2, "rate"] / rates_by_diag_yr2[1, "rate"]
    with(rates_by_diag_yr2[2:1,], poisson.test(event, tstop))

### iii.

Now use Poisson regression to estimate the same mortality rate ratio. Write the linear predictor using pen and paper and draw a graph of the fitted hazard rates.

    ## Use glm to estimate the rate ratios
    ## we scale the offset term to 1000 person-years
    poisson7c <- glm( death_cancer ~ year8594 + offset( log( surv_mm/12/1000 ) ), family=poisson, data=melanoma.l2 )
    summary( poisson7c )

    ## also for collapsed data
    summary(glm( event ~ year8594 + offset( log( tstop/12/1000 ) ), family=poisson, data=rates_by_diag_yr2))

    ## IRR
    eform(poisson7c)

    ## Note that the scaling of the offset term only has an impact on the intercept
    summary( glm( death_cancer ~ year8594 + offset( log( surv_mm ) ),
                 family=poisson, data=melanoma.l2 ) )

## (7d)

In order to adjust for time since diagnosis (i.e., adjust for the fact that we expect mortality to depend on time since diagnosis) we need to split the data by this timescale. We will restrict our analysis to mortality up to 10 years following diagnosis.

    ## Add a new variable for year
    melanoma.l2 <- mutate( melanoma.l2, surv_yy1 = surv_mm/12)

    ## Split follow up by year
    melanoma.spl <- survSplit(melanoma.l2, cut=0:9, end="surv_yy1", start="start",
                               event="death_cancer")

    ## Calculate persontime and
    ## recode start time as a factor
    melanoma.spl <- mutate(melanoma.spl,
                           pt = surv_yy1 - start,
                           fu = as.factor(start) )

## (e)

Now tabulate (and produce a graph of) the rates by follow-up time.

    ## Calculate the incidence rate by observation year
    yearly_rates <- survRate(Surv(pt/1000,death_cancer)~fu, data=melanoma.spl)

    ## Plot by year
    with(yearly_rates, matplot(fu,
                               cbind(rate, lower,
                                 upper),
                               lty=c("solid","dashed","dashed"),
                               col=c("black","gray","gray"),
                               type="l",
                               main="Cancer deaths by years since diagnosis",
                               ylab="Incidence rate per 1000 person-years",
                               xlab="Years since diagnosis") )

Mortality appears to be quite low during the first year of follow-up. Does this seem reasonable considering the disease with which these patients have been diagnosed?

## (f)

Compare the plot of the estimated rates to a plot of the hazard rate as a function of continuous time.

    # Plot smoothed hazards

    par(mfrow=c(1,2))
    with(yearly_rates, matplot(as.numeric(as.character(fu))+0.5,
                               cbind(rate, lower,
                                     upper),
                               ylim=c(0,max(upper)),
                               lty=c("solid","dashed","dashed"),
                               col=c("black","gray","gray"),
                               type="l",
                               main="Cancer deaths by time since diagnosis",
                               ylab="Mortality rate per 1000 person-years",
                               xlab="Years since diagnosis") )

    hazfit7f <- muhaz2(Surv(surv_mm/12, status == "Dead: cancer") ~ 1, data = melanoma.l)
    ## scale hazard by 1000
    plot(hazfit7f, xlab="Years since diagnosis",col="blue",lty="solid", haz.scale=1000, xlim=c(0,10))

Is the interpretation similar? Do you think it is sufficient to classify follow-up time into annual intervals or might it be preferable to use, for example, narrower intervals?

## (g)

Use Poisson regression to estimate incidence rate ratios as a function of follow-up time. Write the linear predictor using pen and paper.

    ## Run Poisson regression
    summary(poisson7g <- glm( death_cancer ~ fu + offset( log(pt) ),
                             family = poisson,
                             data = melanoma.spl ))
    ## IRR
    eform(poisson7g)

Does the pattern of estimated incident rate ratios mirror the pattern you observed in the plots? Draw a graph of the fitted hazard rate using pen and paper.

Write out the regression equation.

## (h)

Now estimate the effect of calendar period of diagnosis while adjusting for time since diagnosis. Before fitting this model, predict what you expect the estimated effect to be (i.e., will it be higher, lower, or similar to the value we obtained in part c). Write the linear predictor using pen and paper and draw a graph of the fitted hazard rates.

    summary(poisson7h <- glm( death_cancer ~ fu + year8594 + offset( log(pt) ),
                             family = poisson,
                             data = melanoma.spl ))
    ## IRR
    eform(poisson7h)

    # Add interaction term
    summary(poisson7h2 <- glm( death_cancer ~ fu*year8594 + offset( log(pt) ), family=poisson, data=melanoma.spl ))
    ## IRR
    eform(poisson7h2)

Is the estimated effect of calendar period of diagnosis consistent with what you expected? Add an interaction between follow-up and calendar period of diagnosis and interpret the results.

## (i)

Now control for age, sex, and calendar period. Write the linear predictor using pen and paper.

### i.

Interpret the estimated hazard ratio for the parameter labelled agegrp 2, including a comment on statistical significance. ### ii. ###
Is the effect of calendar period strongly confounded by age and sex? That is, does the inclusion of sex and age in the model change the estimate for the effect of calendar period? ### iii. ###
Perform a Wald test of the overall effect of age and interpret the results.

    summary(poisson7i <- glm( death_cancer ~ fu + year8594 + sex + agegrp + offset( log(pt) ), family=poisson, data=melanoma.spl ))

    ## IRR
    eform(poisson7i)

    ## Test if the effect of age is significant using a likelihood ratio test
    drop1(poisson7i, ~agegrp, test="Chisq")
    ## For this we can also use the car package and a Wald test
    linearHypothesis(poisson7i,c("agegrp45-59 = 0","agegrp60-74 = 0","agegrp75+ = 0"))
    ## ADVANCED:
    ## Alternative approach for the likelihood ratio test
    # poisson7i_2 <- update(poisson7i,. ~ . - agegrp)
    # anova(poisson7i_2,poisson7i,test="Chisq")

## (j)

Is the effect of sex modified by calendar period (whilst adjusting for age and follow-up)? Fit an appropriate interaction term to test this hypothesis. Write the linear predictor using pen and paper.

    summary(poisson7j <- glm( death_cancer ~ fu + agegrp + year8594*sex + offset( log(pt) ), family=poisson, data=melanoma.spl ))

    ## IRR
    eform(poisson7j)

## (k)

Based on the interaction model you fitted in exercise 7j, estimate the hazard ratio for the effect of sex (with 95% confidence interval) for each calendar period.

ADVANCED: Do this with each of the following methods and confirm that the results are the same:

### i.

Using hand-calculation on the estimates from exercise 7j.

    # hand calculations
    hz7k <- exp(coef(poisson7j))
    hz7k["sexFemale"]
    hz7k["sexFemale"]*hz7k["year8594Diagnosed 85-94:sexFemale"]

### ii.

Using the estimates from exercise 7j.

    ## You will need the "car" package to use lincom. If it is not already installed:
    ## install.packages("car")
    lincom(poisson7j,c("sexFemale + year8594Diagnosed 85-94:sexFemale"),eform=TRUE)

### iii.

Creating appropriate dummy variables that represent the effects of sex for each calendar period.

    ## Create dummies and Poisson regression
    melanoma.spl <- melanoma.spl %>%
        ## Add confidence intervals for the rates
        mutate(femaleEarly = sex=="Female" & year8594=="Diagnosed 75-84",
               femaleLate = sex=="Female" & year8594=="Diagnosed 85-94")

    summary(poisson7k <- glm( death_cancer ~ fu + agegrp + year8594 + femaleEarly +
                             femaleLate + offset( log(pt) ), family=poisson,
                             data=melanoma.spl ))

    ## IRR
    eform(poisson7k)

Write the linear predictor using pen and paper.

### iv.

Using the formula to specify the interactions to repeat the previous model.

    ## Add interaction term
    summary(poisson7k2 <- glm( death_cancer ~ fu + agegrp + year8594 + year8594:sex +
                             offset( log(pt) ), family=poisson,
                             data=melanoma.spl ))
    eform(poisson7k2)

Using the formula to specify the interactions, repeat the previous model.

## (l)

Now fit a separate model for each calendar period in order to estimate the hazard ratio for the effect of sex (with 95% confidence interval) for each calendar period.

Why do the estimates differ from those you obtained in the previous part?

    summary( poisson7l.early <- glm( death_cancer ~ fu + agegrp + sex + offset( log(pt) ),
                           family = poisson, data = melanoma.spl,
                           subset = year8594 == "Diagnosed 75-84" ) )
    eform(poisson7l.early)

    summary( poisson7l.late <- glm( death_cancer ~ fu + agegrp + sex + offset( log(pt) ),
                           family = poisson, data = melanoma.spl,
                           subset = year8594 == "Diagnosed 85-94" ) )
    eform(poisson7l.late)

    # compare with results in i
    eform(poisson7i)

    # compare with results in j
    eform(poisson7j)

    # Poisson-regression with effects specific for diagnose period
    summary(poisson7l2 <- glm( death_cancer ~ fu + fu:year8594 + agegrp + agegrp:year8594
                              + sex*year8594 + offset( log(pt) ),
                              family=poisson, data=melanoma.spl ))
    eform(poisson7l2)

Can you fit a single model that reproduces the estimates you obtained from the stratified models?

## (m)

Split by month and fit a model to smooth for time using natural splines, adjusting for age group and calendar period. Plot the baseline hazard.

    ## Split follow up by month
    library(splines)
    time.cut <- seq(0,10,by=1/12)
    nrow(biostat3::melanoma)
    melanoma.spl <- survSplit(Surv(surv_mm/12,status=="Dead: cancer")~., data=biostat3::melanoma,
                              cut=time.cut,
                              subset=stage=="Localised")
    nrow(melanoma.spl)
    melanoma.spl <- transform(melanoma.spl, mid=(tstop+tstart)/2, risk_time=tstop-tstart)
    poisson7m <- glm(event ~ ns(mid,df=6) + agegrp + year8594 +
                         offset(log(risk_time)),
                     family=poisson,
                     data=melanoma.spl)
    df <- data.frame(agegrp="0-44", year8594="Diagnosed 75-84",
                     mid=time.cut[-1], risk_time=1)
    ## plot the rate at the baseline values
    plot(df$mid, predict(poisson7m, newdata=df, type="response"),
         type="l", ylab="Rate", xlab="Time since diagnosis (years)",
         ylim=c(0,0.05))

## (n)

Split by month and fit a model to smooth for time using natural splines, adjusting for age group and calendar period, with a time-varying hazard ratio for calendar period. Plot the time-varying hazard ratio.

    ## using melanoma.spl and df from previous chunk
    poisson7n <- glm(event ~ ns(mid,df=4) + agegrp + year8594 +
                         ifelse(year8594=="Diagnosed 85-94",1,0):ns(mid,df=3) +
                         offset(log(risk_time)),
                     family=poisson,
                     data=melanoma.spl)
    library(rstpm2)
    library(ggplot2)
    ## get log(RR) confidence interval using predictnl (delta method)
    pred <- predictnl(poisson7n, function(object)
        log(predict(object, newdata=transform(df, year8594="Diagnosed 85-94"),
                    type="response") /
            predict(object, newdata=df, type="response")))
    pred2 <- transform(pred, time = df$mid, rr = exp(fit), ci = exp(confint(pred)))
    ggplot(pred2, aes(x=time, y=rr, ymin=ci.2.5.., ymax=ci.97.5..)) +
        ggplot2::geom_line() + ggplot2::geom_ribbon(alpha=0.6) +
        xlab("Time since diagnosis (years)") +
        ylab("Rate ratio")

    ## Calculate the rate difference
    band <- function(x,yy,col="grey")
        polygon(c(x,rev(x)), c(yy[,1], rev(yy[,2])), col=col, border=col)
    pred <- predictnl(poisson7n, function(object)
        predict(object, newdata=transform(df, year8594="Diagnosed 85-94"),
                    type="response") -
        predict(object, newdata=df, type="response"))
    rd <- pred$fit
    ci <- confint(pred)
    matplot(df$mid,
            ci,
            type="n", # blank plot area
            xlab="Time since diagnosis (years)",
            ylab="Rate difference")
    band(df$mid,ci) # add confidence band
    lines(df$mid, rd) # add rate difference