Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ggforest() doesn't show interactions HRs #496

Closed
Generalized opened this issue Sep 29, 2020 · 9 comments
Closed

ggforest() doesn't show interactions HRs #496

Generalized opened this issue Sep 29, 2020 · 9 comments

Comments

@Generalized
Copy link

Generalized commented Sep 29, 2020

Dear Authors,
I noticed, that the ggforest() function doesn't plot interactions. The version from CRAN doesn't do that, so I downloaded and installed the most fresh code from the GitHub via devtools::install_github("kassambara/survminer", build_vignettes = FALSE). Same result.

Expected behavior

An interaction should be shown

Actual behavior

Only the main effects are shown

Steps to reproduce the problem

Fit any Cox model with an interaction.
Run ggforest on it.

> ggforest( fit <- coxph(Surv(time, status) ~ ph.karno * age, data=lung), data=lung)
> summary(fit, conf.int = FALSE)
Call:
coxph(formula = Surv(time, status) ~ ph.karno * age, data = lung)

  n= 227, number of events= 164 
   (1 observation deleted due to missingness)

                   coef  exp(coef)   se(coef)      z Pr(>|z|)  
ph.karno     -0.1211782  0.8858761  0.0486092 -2.493   0.0127 *
age          -0.1206758  0.8863212  0.0610426 -1.977   0.0481 *
ph.karno:age  0.0016586  1.0016600  0.0007525  2.204   0.0275 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Concordance= 0.598  (se = 0.025 )
Likelihood ratio test= 14.52  on 3 df,   p=0.002
Wald test            = 13.42  on 3 df,   p=0.004
Score (logrank) test = 13.44  on 3 df,   p=0.004

obraz

session_info()

tools::session_info()
- Session info ---------------------------------------------------------------
 setting  value                       
 version  R version 4.0.2 (2020-06-22)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       Rgui                        
 language (EN)                        
 date     2020-09-29                  

- Packages -------------------------------------------------------------------
 package     * version   date       lib source                               
 abind         1.4-5     2016-07-21 [1] CRAN (R 4.0.0)                       
 assertthat    0.2.1     2019-03-21 [1] CRAN (R 4.0.2)                       
 backports     1.1.10    2020-09-15 [1] CRAN (R 4.0.2)                       
 broom         0.7.0     2020-07-09 [1] CRAN (R 4.0.2)                       
 callr         3.4.4     2020-09-07 [1] CRAN (R 4.0.2)                       
 car           3.0-9     2020-08-11 [1] CRAN (R 4.0.2)                       
 carData       3.0-4     2020-05-22 [1] CRAN (R 4.0.0)                       
 cellranger    1.1.0     2016-07-27 [1] CRAN (R 4.0.2)                       
 cli           2.0.2     2020-02-28 [1] CRAN (R 4.0.2)                       
 colorspace    1.4-1     2019-03-18 [1] CRAN (R 4.0.2)                       
 cowplot       1.1.0     2020-09-08 [1] CRAN (R 4.0.2)                       
 crayon        1.3.4     2017-09-16 [1] CRAN (R 4.0.2)                       
 curl          4.3       2019-12-02 [1] CRAN (R 4.0.2)                       
 data.table    1.13.0    2020-07-24 [1] CRAN (R 4.0.2)                       
 desc          1.2.0     2018-05-01 [1] CRAN (R 4.0.2)                       
 devtools      2.3.2     2020-09-18 [1] CRAN (R 4.0.2)                       
 digest        0.6.25    2020-02-23 [1] CRAN (R 4.0.2)                       
 dplyr         1.0.2     2020-08-18 [1] CRAN (R 4.0.2)                       
 ellipsis      0.3.1     2020-05-15 [1] CRAN (R 4.0.2)                       
 fansi         0.4.1     2020-01-08 [1] CRAN (R 4.0.2)                       
 farver        2.0.3     2020-01-16 [1] CRAN (R 4.0.2)                       
 forcats       0.5.0     2020-03-01 [1] CRAN (R 4.0.2)                       
 foreign       0.8-80    2020-05-24 [1] CRAN (R 4.0.2)                       
 fs            1.5.0     2020-07-31 [1] CRAN (R 4.0.2)                       
 generics      0.0.2     2018-11-29 [1] CRAN (R 4.0.2)                       
 ggplot2     * 3.3.2     2020-06-19 [1] CRAN (R 4.0.2)                       
 ggpubr      * 0.4.0     2020-06-27 [1] CRAN (R 4.0.2)                       
 ggsignif      0.6.0     2019-08-08 [1] CRAN (R 4.0.2)                       
 glue          1.4.2     2020-08-27 [1] CRAN (R 4.0.2)                       
 gridExtra     2.3       2017-09-09 [1] CRAN (R 4.0.2)                       
 gtable        0.3.0     2019-03-25 [1] CRAN (R 4.0.2)                       
 haven         2.3.1     2020-06-01 [1] CRAN (R 4.0.2)                       
 hms           0.5.3     2020-01-08 [1] CRAN (R 4.0.2)                       
 km.ci         0.5-2     2009-08-30 [1] CRAN (R 4.0.2)                       
 KMsurv        0.1-5     2012-12-03 [1] CRAN (R 4.0.0)                       
 knitr         1.30      2020-09-22 [1] CRAN (R 4.0.2)                       
 labeling      0.3       2014-08-23 [1] CRAN (R 4.0.0)                       
 lattice       0.20-41   2020-04-02 [1] CRAN (R 4.0.2)                       
 lifecycle     0.2.0     2020-03-06 [1] CRAN (R 4.0.2)                       
 magrittr      1.5       2014-11-22 [1] CRAN (R 4.0.2)                       
 Matrix        1.2-18    2019-11-27 [1] CRAN (R 4.0.2)                       
 memoise       1.1.0     2017-04-21 [1] CRAN (R 4.0.2)                       
 munsell       0.5.0     2018-06-12 [1] CRAN (R 4.0.2)                       
 openxlsx      4.2.2     2020-09-17 [1] CRAN (R 4.0.2)                       
 pillar        1.4.6     2020-07-10 [1] CRAN (R 4.0.2)                       
 pkgbuild      1.1.0     2020-07-13 [1] CRAN (R 4.0.2)                       
 pkgconfig     2.0.3     2019-09-22 [1] CRAN (R 4.0.2)                       
 pkgload       1.1.0     2020-05-29 [1] CRAN (R 4.0.2)                       
 prettyunits   1.1.1     2020-01-24 [1] CRAN (R 4.0.2)                       
 processx      3.4.4     2020-09-03 [1] CRAN (R 4.0.2)                       
 ps            1.3.4     2020-08-11 [1] CRAN (R 4.0.2)                       
 purrr         0.3.4     2020-04-17 [1] CRAN (R 4.0.2)                       
 R6            2.4.1     2019-11-12 [1] CRAN (R 4.0.2)                       
 Rcpp          1.0.5     2020-07-06 [1] CRAN (R 4.0.2)                       
 readxl        1.3.1     2019-03-13 [1] CRAN (R 4.0.2)                       
 remotes       2.2.0     2020-07-21 [1] CRAN (R 4.0.2)                       
 rio           0.5.16    2018-11-26 [1] CRAN (R 4.0.2)                       
 rlang         0.4.7     2020-07-09 [1] CRAN (R 4.0.2)                       
 rprojroot     1.3-2     2018-01-03 [1] CRAN (R 4.0.2)                       
 rstatix       0.6.0     2020-06-18 [1] CRAN (R 4.0.2)                       
 scales        1.1.1     2020-05-11 [1] CRAN (R 4.0.2)                       
 sessioninfo   1.1.1     2018-11-05 [1] CRAN (R 4.0.2)                       
 stringi       1.5.3     2020-09-09 [1] CRAN (R 4.0.2)                       
 stringr       1.4.0     2019-02-10 [1] CRAN (R 4.0.2)                       
 survival    * 3.2-3     2020-06-13 [1] CRAN (R 4.0.2)                       
 survminer   * 0.4.8.999 2020-09-29 [1] Github (kassambara/survminer@acff36f)
 survMisc      0.5.5     2018-07-05 [1] CRAN (R 4.0.2)                       
 testthat      2.3.2     2020-03-02 [1] CRAN (R 4.0.2)                       
 tibble        3.0.3     2020-07-10 [1] CRAN (R 4.0.2)                       
 tidyr         1.1.2     2020-08-27 [1] CRAN (R 4.0.2)                       
 tidyselect    1.1.0     2020-05-11 [1] CRAN (R 4.0.2)                       
 usethis       1.6.3     2020-09-17 [1] CRAN (R 4.0.2)                       
 vctrs         0.3.4     2020-08-29 [1] CRAN (R 4.0.2)                       
 withr         2.3.0     2020-09-22 [1] CRAN (R 4.0.2)                       
 xfun          0.17      2020-09-09 [1] CRAN (R 4.0.2)                       
 xtable        1.8-4     2019-04-21 [1] CRAN (R 4.0.2)                       
 zip           2.1.1     2020-08-27 [1] CRAN (R 4.0.2)                       
 zoo           1.8-8     2020-05-02 [1] CRAN (R 4.0.2)                       

[1] C:/tmp/r4/library
@zh-zhang1984
Copy link

I also encounter this issue; Hope the authors can address this issue in the future version.

@Generalized
Copy link
Author

Dear Authors and Contributors, @kassambara , @MarcinKosinski, @zzawadz , @pbiecek Do you plan to fix this issue in the near future? I am sorry for asking, but I will need to consider switching to another package or write necessary procedure on my own and message my Professor about the need for additional work on that. Please, let me just know.

@pbiecek
Copy link
Contributor

pbiecek commented Dec 9, 2020

Dear @Generalized
In general case it may be tricky to automatically extract interactions or variable transformations from model objects.
So my advice would be to create manually new variables that capture desired effects of interactions and add them to the model in an explicit way.

Here is an example how to do this.

You have:

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

                   coef  exp(coef)   se(coef)      z Pr(>|z|)  
ph.karno     -0.1211782  0.8858761  0.0486092 -2.493   0.0127 *
age          -0.1206758  0.8863212  0.0610426 -1.977   0.0481 *
ph.karno:age  0.0016586  1.0016600  0.0007525  2.204   0.0275 *

What you can do is to create the variable that handles interaction manually:

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

and now you can fir an additive model and the ggforest will include it in the plot

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

                   coef  exp(coef)   se(coef)      z Pr(>|z|)  
ph.karno     -0.1211782  0.8858761  0.0486092 -2.493   0.0127 *
age          -0.1206758  0.8863212  0.0610426 -1.977   0.0481 *
ph.karno_age  0.0016586  1.0016600  0.0007525  2.204   0.0275 *

Please note that here you have two numerical variables, this is why I've just multiplied them.
If you have factors you should concatenate factors for bot variables

Screenshot 2020-12-09 at 9 54 27 AM

@Generalized
Copy link
Author

Thank you. It's a perfect solution. Maybe, instead of "fixing" that automatically, especially if it requires some tricky coding, you could use this answer to make an example in a vignette? I think this may be rather a common issue, that people look for the interaction they explicitly specified yet which is missing in the output. Especially, that I saw a similar topic here on the list, but much earlier. This solution requires some extra work from the user, but gives the expected output, which is priceless, as we do not have switch to other packages to complete this task. Thank you!

@pbiecek
Copy link
Contributor

pbiecek commented Dec 10, 2020

Good idea,
will do this!

@kassambara
Copy link
Owner

New ggforest vignette added now

@ronammar
Copy link

How would one generalize this solution to interactions between categorical variables?

@benjaminwnelson
Copy link

This doesn't seem to work for an interaction between categorical variables. Is this a feature that might be added? Thanks!

@trentleslie
Copy link

This doesn't seem to work for an interaction between categorical variables. Is this a feature that might be added? Thanks!

If you have factors you should concatenate factors for bot variables

If you have categorical variables, you can concatenate them to make the combined, new categorical variables.

Cat 1 Cat 2 Concat
A C AC
A D AD
B D BD

Then use the "Concat" column as your interaction variable. Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants