forked from elong0527/r4csr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlf-ae-spec.Rmd
194 lines (158 loc) · 5.68 KB
/
tlf-ae-spec.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Specific AE
Following [ICH E3 guidance](https://database.ich.org/sites/default/files/E3_Guideline.pdf),
we need to summarize which participants were included in each efficacy
analysis in Section 12.2, Adverse Events (AEs).
```{r}
library(haven) # Read SAS data
library(dplyr) # Manipulate data
library(tidyr) # Manipulate data
library(r2rtf) # Reporting in RTF format
```
In this chapter, we illustrate how to summarize simplified specific AE
information in a study.
```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("tlf/tlf_spec_ae.pdf")
```
The data used to summarize AE information is in `adsl` and `adae` datasets.
```{r}
adsl <- read_sas("adam_data/adsl.sas7bdat")
adae <- read_sas("adam_data/adae.sas7bdat")
```
For illustration purposes, we only provide count in the simplified
table. The percentage of participants for each AE criteria can be
calculated as in Chapter \@ref(aesummary).
In this way, let's focus on the analysis script for two advanced
features for a table layout.
- group content: AE can be summarized in multiple nested layers.
(e.g., by system organ class (SOC, `AESOC`) and specific AE term
(`AEDECOD`))
- pagenization: there are many AE terms that can not be covered in one
page. Column headers and SOC information need to be repeated on
every page.
In the code below, we count the number of subjects in each AE term by
SOC and treatment group, and we create a new variable `order` and set it
as `0` for these counted numbers. The variable `order` will help with
the data manipulation later.
```{r}
ana <- adae %>%
mutate(
AESOC = toTitleCase(tolower(AESOC)),
AEDECOD = toTitleCase(tolower(AEDECOD))
)
t1 <- ana %>%
group_by(TRTAN, AESOC) %>%
summarise(n = fmt_num(n_distinct(USUBJID), digits = 0)) %>%
mutate(AEDECOD = AESOC, order = 0)
t1 %>% head(4)
```
In the code below, we count the number of subjects in each AE term by
SOC, AE term, and treatment group. Here we also create a new variable
`order` and set it as `1` for these counted numbers.
```{r}
t2 <- ana %>%
group_by(TRTAN, AESOC, AEDECOD) %>%
summarise(n = fmt_num(n_distinct(USUBJID), digits = 0)) %>%
mutate(order = 1)
t2 %>% head(4)
```
We prepare reporting data for AE information.
```{r}
t_ae <- bind_rows(t1, t2) %>%
pivot_wider(
id_cols = c(AESOC, order, AEDECOD),
names_from = TRTAN,
names_prefix = "n_",
values_from = n,
values_fill = fmt_num(0, digits = 0)
) %>%
arrange(AESOC, order, AEDECOD) %>%
select(AESOC, AEDECOD, starts_with("n"))
t_ae %>% head(4)
```
We prepare reporting data for analysis population.
```{r}
t_pop <- adsl %>%
filter(SAFFL == "Y") %>%
count_by("TRT01AN", "SAFFL",
var_label = "Participants in population"
) %>%
mutate(
AESOC = "pop",
AEDECOD = var_label
) %>%
select(AESOC, AEDECOD, starts_with("n_"))
t_pop
```
The final report data is saved in `tbl_ae_spec`.
We also add a blank row between population and AE information in the reporting table.
```{r}
tbl_ae_spec <- bind_rows(
t_pop,
data.frame(AESOC = "pop"),
t_ae
) %>%
mutate(AEDECOD = ifelse(AEDECOD == AESOC,
AEDECOD, paste0(" ", AEDECOD)
))
tbl_ae_spec %>% head(4)
```
We start to define the format of the output.
To obtain the nested layout, we use the `page_by` argument in the
`rtf_body` function. By defining `page_by="AESOC"`, `r2rtf` recognize
the variable as a group indicator.
After setting `pageby_row = "first_row"`, the first row is displayed as
group header. If a group of information is broken into multiple pages,
the group header row is repeated on each page by default.
We can also customize the text format by providing a matrix that has the
same dimension as the input dataset (i.e., `tbl_ae_spec`). In the code
below, we illustrate how to display **bold** text for group headers to
highlight the nested structure of the table layout.
```{r}
n_row <- nrow(tbl_ae_spec)
n_col <- ncol(tbl_ae_spec)
id <- tbl_ae_spec$AESOC == tbl_ae_spec$AEDECOD
id <- ifelse(is.na(id), FALSE, id)
text_format <- ifelse(id, "b", "")
```
More discussion on `page_by`, `group_by` and `subline_by`
features can be found in the
[`r2rtf` package website](https://merck.github.io/r2rtf/articles/example-sublineby-pageby-groupby.html.
```{r}
tbl_ae_spec %>%
rtf_title(
"Analysis of Participants With Specific Adverse Events",
"(Safety Analysis Population)"
) %>%
rtf_colheader(" | Placebo | Xanomeline Low Dose| Xanomeline High Dose",
col_rel_width = c(3, rep(1, 3))
) %>%
rtf_colheader(" | n | n | n ",
border_top = "",
border_bottom = "single",
col_rel_width = c(3, rep(1, 3))
) %>%
rtf_body(
col_rel_width = c(1, 3, rep(1, 3)),
text_justification = c("l", "l", rep("c", 3)),
text_format = matrix(text_format, nrow = n_row, ncol = n_col),
page_by = "AESOC",
pageby_row = "first_row"
) %>%
rtf_footnote("Every subject is counted a single time for each applicable row and column.") %>%
rtf_encode() %>%
write_rtf("tlf/tlf_spec_ae.rtf")
```
```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("tlf/tlf_spec_ae.pdf")
```
The procedure to generate a baseline characteristics table can be
summarized as follow:
- Step 1: Read data into R, i.e., `adae` and `adsl`.
- Step 2: Count the number of subjects by SOC and treatment group
(rows with bold text) and save into `t1`.
- Step 3: Count the number of subjects in each AE term by SOC, AE
term, and treatment group (rows without bold text) and save into `t2`.
- Step 4: Bind `t1` and `t2` by row into `t_ae`.
- Step 5: Count the number of subjects in each arm as `t_pop`.
- Step 6: Bind `t_pop` and `t_ae` by row into `tbl_ae_spec`.
- Step 7: Format the output by `r2rtf`.