-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathggforest-show-interactions-hazard-ratio.Rmd
86 lines (60 loc) · 1.95 KB
/
ggforest-show-interactions-hazard-ratio.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---
title: "ggforest: How to Show Interactions Hazard Ratio"
author: "Alboukadel Kassambara, Przemyslaw Biecek"
output:
html_document:
mathjax: default
fig_caption: true
toc: true
section_numbering: true
css: ggsci.css
vignette: >
%\VignetteIndexEntry{ggforest-show-interactions-hazard-ratio}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Survival plots have never been so informative}
%\VignetteEncoding{UTF-8}
---
```{r include = FALSE}
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
```{r setup}
library(survminer)
library(survival)
```
# Compute a Cox model with interaction terms
```{r}
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()`.
```{r ggforest, fig.width=5, fig.height=4}
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:
```{r}
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:
```{r}
res.cox2 <- coxph(Surv(time, status) ~ ph.karno + age + ph.karno_age, data = lung)
summary(res.cox2 , conf.int = FALSE)
```
```{r ggforest-with-interactions, fig.width=5, fig.height=4}
ggforest(res.cox2, data=lung)
```