-
Notifications
You must be signed in to change notification settings - Fork 19
/
tlf-primary.Rmd
182 lines (161 loc) · 4.48 KB
/
tlf-primary.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
---
title: "CDISC Pilot: Primary Endpoint Analysis: ADAS Cog (11) - Change from Baseline to Week 24 - LOCF"
output:
html_document:
df_print: paged
toc: yes
toc_depth: '2'
toc_float: true
---
```{r, message = FALSE}
# Initiate start-up file
source(file.path(rprojroot::find_root("DESCRIPTION"), "inst/startup.R"))
```
```{r}
# Working directory requires write permission
if(file.access(".", 2) != 0){
warning(
"The working directory '", normalizePath("."), "' is not writable.\n",
"Please change it to a location with write permission."
)
}
```
```{r setup, message=FALSE}
# CRAN package, please using install.packages() to install
library(tidyr)
library(dplyr)
library(Tplyr)
library(pharmaRTF)
# Propitiatory Package, please refer appendix of ADRG to install
library(pilot1wrappers)
```
```{r}
options(huxtable.add_colnames = FALSE)
```
## Step 1: Read in data
```{r}
adas <- haven::read_xpt(file.path(path$adam, "adadas.xpt"))
adsl <- haven::read_xpt(file.path(path$adam, "adsl.xpt"))
```
## Step 2: Data preparation
```{r}
adas <- adas %>%
filter(
EFFFL == "Y",
ITTFL=='Y',
PARAMCD == 'ACTOT',
ANL01FL == 'Y'
)
```
## Step 3: Descriptive Statistics of Endpoint Variable
```{r}
t <- tplyr_table(adas, TRTP) %>%
set_pop_data(adsl) %>%
set_pop_treat_var(TRT01P) %>%
set_pop_where(EFFFL == "Y" & ITTFL == "Y") %>%
set_distinct_by(USUBJID) %>%
set_desc_layer_formats(
'n' = f_str('xx', n),
'Mean (SD)' = f_str('xx.x (xx.xx)', mean, sd),
'Median (Range)' = f_str('xx.x (xxx;xx)', median, min, max)
) %>%
add_layer(
group_desc(AVAL, where= AVISITN == 0, by = "Baseline")
) %>%
add_layer(
group_desc(AVAL, where= AVISITN == 24, by = "Week 24")
) %>%
add_layer(
group_desc(CHG, where= AVISITN == 24, by = "Change from Baseline")
)
hdr <- adas %>% distinct(TRTP, TRTPN) %>% arrange(TRTPN) %>% pull(TRTP)
hdr_ext <- sapply(hdr, FUN = function(x) paste0("|", x, "\\line(N=**", x, "**)"), USE.NAMES = FALSE)
hdr_fin <- paste(hdr_ext, collapse = "")
# Want the header to wrap properly in the RTF file
hdr_fin <- stringr::str_replace_all(hdr_fin, "\\|Xanomeline ", "|Xanomeline\\\\line ")
sum_data <- t %>%
build() %>%
nest_rowlabels() %>%
select(row_label, var1_Placebo, `var1_Xanomeline Low Dose`, `var1_Xanomeline High Dose`) %>%
add_column_headers(
hdr_fin,
header_n(t)
)
```
## Step 4: Statistical Summaries
```{r}
model_portion <- efficacy_models(adas, 'CHG', 24)
```
## Step 5: Style the table
```{r}
final <- bind_rows(sum_data, model_portion)
ht <- huxtable::as_hux(final, add_colnames = FALSE) %>%
huxtable::set_bold(1, 1:ncol(final), TRUE) %>%
huxtable::set_align(1, 1:ncol(final), 'center') %>%
huxtable::set_valign(1, 1:ncol(final), 'bottom') %>%
huxtable::set_bottom_border(1, 1:ncol(final), 1) %>%
huxtable::set_width(1.3) %>%
huxtable::set_escape_contents(FALSE) %>%
huxtable::set_col_width(c(.4, .2, .2, .2))
cat(huxtable::to_screen(ht))
```
## Step 6: Write out the report
```{r}
doc <- rtf_doc(ht) %>%
set_font_size(10) %>%
set_ignore_cell_padding(TRUE) %>%
set_column_header_buffer(top=1) %>%
add_titles(
hf_line(
"Protocol: CDISCPILOT01",
"PAGE_FORMAT: Page %s of %s",
align='split',
bold=TRUE,
italic=TRUE
),
hf_line(
"Population: Efficacy",
align="left",
bold=TRUE,
italic=TRUE
),
hf_line(
"Table 14-3.01",
bold=TRUE,
italic=TRUE
),
hf_line(
"Primary Endpoint Analysis: ADAS Cog (11) - Change from Baseline to Week 24 - LOCF",
bold=TRUE,
italic=TRUE
)
) %>%
add_footnotes(
hf_line(
"[1] Based on Analysis of covariance (ANCOVA) model with treatment and site group as factors and baseline value as a covariate.",
align = "left",
italic=TRUE
),
hf_line(
"[2] Test for a non-zero coefficient for treatment (dose) as a continuous variable",
align = "left",
italic=TRUE
),
hf_line(
"[3] Pairwise comparison with treatment as a categorical variable: p-values without adjustment for multiple comparisons.",
align = "left",
italic=TRUE
),
hf_line(
"FILE_PATH: Source: %s",
"DATE_FORMAT: %H:%M %A, %B %d, %Y",
align = "split",
italic=TRUE
)
)
# Write out the RTF
write_rtf(doc, file=file.path(path$output, 'tlf-primary.rtf'))
```
```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("pdf/tlf-primary.pdf")
```