forked from Bayer-Group/sas2r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-plots-examples-survival.Rmd
117 lines (103 loc) · 3.7 KB
/
11-plots-examples-survival.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
### Survival Plot
#### Kaplan-Meier statistics and plotting
In the following example the _survfit_ function from the _survival_ package is used to calculate Kaplan-Meier statistics from time-to-event data. The model statistics can be inspected (using _broom_ - a generic function to extract statistics from R models).
Passing the model to the _ggsurvplot_ function from the _survminer_ package creates a Kaplan-Meier curve. The configuration tries to mimic the known SAS output as close as possible (e.g. number at risk table style etc.).
#### Packages and Sample Data
```{r, message = FALSE, warning = FALSE, error = FALSE}
# Packages
library(survminer)
library(survival)
library(broom)
library(flextable)
# Data
adtte <- haven::read_xpt(
paste0("https://github.com/phuse-org/TestDataFactory/",
"raw/main/Updated/TDF_ADaM/adtte.xpt"))
```
Filter time-to-event parameter and select required variables. The piping command directly passed filtered and selected data to the `survfit` function and creates the model.
>**Note:** The event parameter from survival function (survival::Surv) is the status indicator, normally 0=alive, 1=death; while ADTTE.CNSR=0 usually means event occurred(e.g., death), ADTTE.CNSR=1 represents censoring. Thus, “1-CNSR” is used here to accommodate the CDISC ADaM standard.
::: {.attention}
This section is under construction
:::
```{r}
surv_model <- adtte %>%
filter(PARAMCD == "TTDE") %>%
select(STUDYID, USUBJID, PARAMCD, AVAL, CNSR, TRTA) %>%
survfit(Surv(AVAL, 1-CNSR) ~ TRTA, data = .)
```
#### Inspecting fitted survival model
```{r, eval = F, echo = T}
head(tidy(surv_model))
```
```{r, echo = F, eval = T}
head(tidy(surv_model)) %>%
kableExtra::kable(align = 'c')
```
#### Plotting custom configured Kaplan-Meier curves without confidence intervals
>**Note:** Let´s assume a month is defined as 30.4375 (days);
The xscale parameter d_m converts days (input) to month.
If six months breaks are required for the numbers-at-risk table this would mean:
break.x.by = 182.625 (6*30.4375).
```{r, fig.width=15, fig.height=10, out.width="680px"}
ggsurvplot(
fit = surv_model,
data = adtte,
risk.table = TRUE,
#ylab = ylabs,
xlab = "Months",
linetype = "strata",
conf.int = F,
legend.title = "Randomized Treatment",
legend = c(0.1, 0.1),
#palette = c(color_trt1,color_trt2),
risk.table.title = "Number of subjects at risk",
risk.table.y.text = F,
risk.table.height = .15,
censor.shape = 1,
censor.size = 3,
ncensor.plot = F,
xlim = c(0,250),
xscale = "d_m",
break.x.by = 30.4375,
break.y.by = .1,
ggtheme = theme_survminer(
font.main = c(10, "bold"),
font.submain = c(10, "bold"),
font.x = c(12, "bold"),
font.y = c(12, "bold"),
) + theme(panel.border = element_rect(fill = NA)),
tables.theme = theme_cleantable()
)
```
#### Plotting custom configured Kaplan-Meier curves with confidence intervals
```{r, fig.width=15, fig.height=10, out.width="680px"}
ggsurvplot(
fit = surv_model,
data = adtte,
risk.table = TRUE,
#ylab = ylabs,
xlab = "Months",
linetype = "strata",
conf.int = T,
legend.title = "Randomized Treatment",
legend = c(0.1, 0.1),
#palette = c(color_trt1,color_trt2),
risk.table.title = "Number of subjects at risk",
risk.table.y.text = F,
risk.table.height = .15,
censor.shape = 1,
censor.size = 3,
ncensor.plot = F,
#xlim = c(0,250),
xscale = "d_m",
break.x.by = 30.4375,
break.y.by = .1,
ggtheme = theme_survminer(
font.main = c(10, "bold"),
font.submain = c(10, "bold"),
font.x = c(12, "bold"),
font.y = c(12, "bold"),
) + theme(panel.border = element_rect(fill = NA)),
tables.theme = theme_cleantable()
)
```