ggforest: How to Show Interactions Hazard Ratio

library(knitr)
opts_chunk$set(
    comment = "",
    message = FALSE,
    warning = FALSE,
    tidy.opts = list(
        keep.blank.line = TRUE,
        width.cutoff = 150
        ),
    options(width = 150),
    eval = TRUE
)

Introduction

In general case it may be tricky to automatically extract interactions or variable transformations from model objects. A suggestion would be to create manually new variables that capture desired effects of interactions and add them to the model in an explicit way. This article describe an example of how to do this.

Load required R packages

library(survminer)
library(survival)

Compute a Cox model with interaction terms

res.cox <- coxph(Surv(time, status) ~ ph.karno * age, data=lung)
summary(res.cox, conf.int = FALSE)

Visualization of the hazard ratios using the function ggforest().

ggforest(res.cox, data = lung)

On the plot above, it can be seen that ggforest() ignores the interaction term ph.karno:age.

To fix this, a solution is to create manually the variable that handles the interaction:

lung$ph.karno_age <- lung$ph.karno * lung$age

and now you can fit an additive model and the ggforest() function will include it in the plot:

res.cox2 <- coxph(Surv(time, status) ~ ph.karno + age + ph.karno_age, data = lung)
summary(res.cox2 , conf.int = FALSE)
ggforest(res.cox2, data=lung)


Try the survminer package in your browser

Any scripts or data that you put into this service are public.

survminer documentation built on March 9, 2021, 5:07 p.m.