0

I fitted a model and had a significant interaction effect. How can I plot this in a graphic?

It follows a toy example (only for illustration purposes):

library(survival)
# includes bladder data set
library(survminer)
fit2 <- coxph(Surv(stop, event) ~ rx*enum, data = bladder )
# It plots only one single curve
ggadjustedcurves(fit2, data = bladder, variable = "rx")

I would like something like these:

ggadjustedcurves(fit2, data = bladder, variable = "rx") +
  facet_wrap(~enum)
ggadjustedcurves(fit2, data = bladder, variable = c("enum","rx"))

It would be nice that the answer would work both for categoricalxcategorical interaction and categorical versus continuous interaction.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • As it's written it seems too broad. There are significant issues in interpretation that this question doesn't acknowledge. The request for answers for both cat-cat and cat-continuous is really a multipart question (not on-topic for SO) – IRTFM Mar 24 '19 at 03:35
  • Can you show an example - a graph like the one you are trying to achieve? – Oka Mar 24 '19 at 09:41
  • It is an interesting question: how one can visualize interaction for survival analysis. There are not much about it on SO nor SE. – Oka Mar 24 '19 at 10:35
  • @Oka: When I was visualizing continuous by continuous interactions in the past, I used a synthetic grid with `predict`. I see no reason why `expand.grid` with predetermnined levels of the continuous variable couldn't be used to construct a variable to be fit and then handed to the method you illustrated below. I always used the `rms` package to make lattice-graphs. I'm not a big fan of the ggplot code base or its look-and-feel. When I have problems I cannot get "inside" ggplot objects and tinker with the innards. – IRTFM Mar 24 '19 at 15:05
  • @42- Thank you. I use both, ggplot and lattice and ggplot can sometimes be frustrating (like in this example it seems quite tricky to get the linetypes changed). – Oka Mar 24 '19 at 16:49

1 Answers1

1

Categorical x categorical

If you consider your variables categorical, in variable "rx" you have 2 groups and in variable "enum" 4 groups, which gives you a total of 8 curves.

(1) One way to visualize them would be to plot all curves on the same graph:

bladder$rx_enum <- paste(as.character(bladder$rx), as.character(bladder$enum), sep="_")
ggadjustedcurves(fit2, data = bladder, method='average', variable = "rx_enum")

This is probably not the most elegant way, and you would also have to adjust the colours/linetypes to look nicer. I would probably try to set the line type according to "rx" and color according to "enum" in this case. Modifying color is relatively easy with palette-argument:

ggadjustedcurves(fit2, data = bladder, method='average', palette = c(1,2,3,4,1,2,3,4), variable = "rx_enum")

...while modifying line type is probably more tricky.

(2) Obviously, you can also make separate panels for different levels of either of variables. With "rx" variable you´ll have a panel for dataframe subset where "rx"==1 and another where "rx"==2. I probably wouldn´t use separate panels/graphs because you can visually represent all of the information on one plot - unless it is necessary/justified by your narrative. But if you want to go that way, let me know.

Categorical x Continuous

The same approach will work with continuous variable as well, if you categorize it. I am not sure how one could make a KM for continuous variable while keeping it continuous (not sure how it is even possible).

NB: This answer considered only the KM-plots which are the most common for survival analysis, but there are probably other options as well.

Oka
  • 1,318
  • 6
  • 11
  • 1
    This code throws an error. It's just a guess and it's wrong. – IRTFM Mar 24 '19 at 03:44
  • @42- what error do you get? It gives an ok graph on my setup (though gives some warnings)? and which part is wrong? – Oka Mar 24 '19 at 09:30
  • @42- thank you for your comment. I corrected the code: apparently one have to specify the method to avoid warnings. – Oka Mar 24 '19 at 10:42
  • 1
    OK. Fixed. The new `rx_enum` could have also been created with `interaction` which is designed for cat-cat interactions. – IRTFM Mar 24 '19 at 14:56