-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
278 lines (206 loc) · 9.12 KB
/
README.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
---
output: github_document
---
# campsisnca <img src='man/figures/r_package_campsisnca.png' align="right" width="120"/>
<!-- badges: start -->
[](https://github.com/Calvagone/campsisnca/actions)
[](https://app.codecov.io/gh/Calvagone/campsisnca)
<!-- badges: end -->
Analyse your simulation output using non-compartmental analysis.
## Installation
Install the latest stable release as follows:
```{r, eval=FALSE}
devtools::install_github("Calvagone/campsisnca")
```
## Basic use
First import the `campsisnca` and `gtsummary` packages as follows:
```{r, message=FALSE}
library(campsisnca)
library(gtsummary)
library(gt)
```
### Example 1: PK metrics at Day 1 and Day 7
Assume some results were simulated with Campsis (see `campsis` dataframe) :
```{r, message=FALSE}
campsis <- generateTestData()
campsis
```
Let's calculate PK metrics at Day 1 and Day 7 as follows:
```{r, message=FALSE}
# Day 1
ncaD1 <- NCAMetrics(x=campsis %>% timerange(0, 24), variable="Y", scenario=c(day="Day 1")) %>%
add(c(AUC(unit="ng/mL*h"), Cmax(unit="ng/mL"), Tmax(unit="h"), Ctrough(unit="ng/mL"))) %>%
calculate()
# Day 7
ncaD7 <- NCAMetrics(x=campsis %>% timerange(144, 168, rebase=TRUE), variable="Y", scenario=c(day="Day 7")) %>%
add(c(AUC(), Cmax(), Tmax(), Ctrough())) %>%
calculate()
```
These 2 metrics may be imported into a metrics table object, as follows:
```{r, message=FALSE}
table <- NCAMetricsTable() %>%
add(c(ncaD1, ncaD7))
```
This table can be exported:
1. To a dataframe using the `export` function:
```{r, message=FALSE}
table %>% export(dest="dataframe")
```
When `type` is not specified, default value is `summary`. Argument `type` can also be `summary_wide` or `summary_pretty`. In the latter case, summary statistics are exported according to the arguments `stat_display` and `digits` provided for each metric.
2. To an HTML table using `gt`:
```{r, message=FALSE}
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
Please note the individual metrics can also be exported to a dataframe using the `export` function as follows:
```{r, message=FALSE}
table %>% export(dest="dataframe", type="individual_wide")
```
### Example 2: PK metrics at Day 1 and Day 7 for different body weight ranges
```{r, message=FALSE}
library(dplyr)
campsis_bw_50_75 <- campsis %>% filter(BW > 50 & BW < 75)
campsis_bw_75_100 <- campsis %>% filter(BW >= 75 & BW < 100)
scenarioD1_a <- c(day="Day 1", bw_range="BW range: 50-75")
ncaD1_a <- NCAMetrics(x=campsis_bw_50_75 %>% timerange(0, 24), variable="Y", scenario=scenarioD1_a) %>%
add(c(AUC(unit="ng/mL*h"), Cmax(unit="ng/mL"), Tmax(unit="h"), Ctrough(unit="ng/mL"))) %>%
calculate()
scenarioD7_a <- c(day="Day 7", bw_range="BW range: 50-75")
ncaD7_a <- NCAMetrics(x=campsis_bw_50_75 %>% timerange(144, 168, rebase=T), variable="Y", scenario=scenarioD7_a) %>%
add(c(AUC(), Cmax(), Tmax(), Ctrough())) %>%
calculate()
scenarioD1_b <- c(day="Day 1", bw_range="BW range: 75-100")
ncaD1_b <- NCAMetrics(x=campsis_bw_75_100 %>% timerange(0, 24), variable="Y", scenario=scenarioD1_b) %>%
add(c(AUC(), Cmax(), Tmax(), Ctrough())) %>%
calculate()
scenarioD7_b <- c(day="Day 7", bw_range="BW range: 75-100")
ncaD7_b <- NCAMetrics(x=campsis_bw_75_100 %>% timerange(144, 168, rebase=T), variable="Y", scenario=scenarioD7_b) %>%
add(c(AUC(), Cmax(), Tmax(), Ctrough())) %>%
calculate()
table <- NCAMetricsTable() %>%
add(c(ncaD1_a, ncaD7_a, ncaD1_b, ncaD7_b))
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
```{r, message=FALSE}
# Alternatively, first stratification variable can be seen in columns (use of 'tbl_merge' within gtsummary)
table %>% export(dest="gt", subscripts=TRUE, combine_with="tbl_merge") %>% as_raw_html()
```
### Example 3: Calculate 2-compartment half-life metrics
```{r, message=FALSE}
nca <- NCAMetrics(x=campsis %>% mutate(DOSE=1000, TAU=24), variable="Y") %>%
add(c(Thalf.2cpt.dist(), Thalf.2cpt.eff(), Thalf.2cpt.z())) %>%
calculate()
table <- NCAMetricsTable() %>%
add(nca)
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
### Example 4: Compute terminal half-live based on data
```{r, message=FALSE}
nca <- NCAMetrics(x=campsis, variable="Y") %>%
add(c(Thalf(x=campsis %>% timerange(7*24, 10*24)))) %>%
calculate()
table <- NCAMetricsTable() %>%
add(nca)
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
### Example 5: Round your PK metrics
```{r, message=FALSE}
# Day 1
ncaD1 <- NCAMetrics(x=campsis %>% timerange(0, 24), variable="Y", scenario=c(day="Day 1")) %>%
add(AUC(digits=~style_sigfig(.x, 2), name="AUC1")) %>% # At least 2 significant figures (default in gtsummary)
add(AUC(digits=c(1,2,2), name="AUC2")) %>% # Respectively 1/2/2 digit(s) after comma for med, p5 and p95
add(AUC(digits=~signif(.x, 2), name="AUC3")) %>% # 2 significant digits only
add(AUC(digits=list(~plyr::round_any(.x, 5),
~round(.x, 1) ,
~style_number(.x)), name="AUC4")) %>% # 1 specific function for med, p5 and p95
campsisnca::calculate()
# Day 7
ncaD7 <- NCAMetrics(x=campsis %>% timerange(144, 168, rebase=TRUE), variable="Y", scenario=c(day="Day 7")) %>%
add(AUC(name="AUC1")) %>%
add(AUC(name="AUC2")) %>%
add(AUC(name="AUC3")) %>%
add(AUC(name="AUC4")) %>%
campsisnca::calculate()
table <- NCAMetricsTable() %>%
add(c(ncaD1, ncaD7))
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
### Example 6: Export custom metrics (including categorical data)
```{r, message=FALSE}
# Compute Cmax yourself using campsisnca
custom1 <- CustomMetric(fun=~Cmax() %>% iValue(.x, .y), name="Cmax custom", unit="ng/mL")
# Check if Cmax if higher than 12 ng/mL
custom2 <- CustomMetric(fun=~(Cmax() %>% iValue(.x, .y)) > 12, name="Cmax > 12", unit="%", categorical=TRUE)
# Shortcut notation is also accepted
custom3 <- CustomMetric(fun=~Cmax > 13, name="Cmax > 13", unit="%", categorical=TRUE)
# Day 1
ncaD1 <- NCAMetrics(x=campsis %>% timerange(0, 24), variable="Y", scenario=c(day="Day 1")) %>%
add(c(Cmax(unit="ng/mL"), Tmax(unit="h"), custom1, custom2, custom3)) %>%
campsisnca::calculate()
# Day 7
ncaD7 <- NCAMetrics(x=campsis %>% timerange(144, 168, rebase=TRUE), variable="Y", scenario=c(day="Day 7")) %>%
add(c(Cmax(), Tmax(), custom1, custom2, custom3)) %>%
campsisnca::calculate()
table <- NCAMetricsTable() %>%
add(c(ncaD1, ncaD7))
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
# Alternatively, all dichotomous levels can be shown as well:
table %>% export(dest="gt", subscripts=TRUE, all_dichotomous_levels=TRUE) %>% as_raw_html()
```
### Example 7: Geometric mean / Geometric CV
```{r, message=FALSE}
nca <- NCAMetrics(x=campsis, variable="Y") %>%
add(c(AUC(unit="ng/mL*h", stat_display="{geomean} ({geocv}%)"), Cavg(unit="ng/mL", stat_display="{geomean} ({geocv}%)"))) %>%
calculate()
table <- NCAMetricsTable() %>%
add(nca)
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
### Example 8: Statistics on categorical data with more than 2 levels
```{r, message=FALSE}
getCategory <- function(.x, .y) {
values <- Cmax() %>% iValue(.x, .y)
retValue <- dplyr::case_when(
values < 10 ~ "(1) < 10 ng/mL",
values >= 10 & values <= 15 ~ "(2) 10-15 ng/mL",
values > 15 ~ "(3) > 15 ng/mL",
)
return(retValue)
}
# Or equivalently, the 1-line purrr-style lambda expression
# getCategory <- ~case_when(Cmax < 10 ~ "(1) < 10 ng/mL", Cmax >= 10 & Cmax <= 15 ~ "(2) 10-15 ng/mL", Cmax > 15 ~ "(3) > 15 ng/mL")
# Day 1
ncaD1 <- NCAMetrics(x=campsis %>% timerange(0, 24), variable="Y", scenario=c(day="Day 1")) %>%
add(c(Cmax(unit="ng/mL"), CustomMetric(fun=getCategory, name="Cmax categories", unit="%", categorical=TRUE))) %>%
campsisnca::calculate()
# Day 7
ncaD7 <- NCAMetrics(x=campsis %>% timerange(144, 168, rebase=TRUE), variable="Y", scenario=c(day="Day 7")) %>%
add(c(Cmax(), CustomMetric(fun=getCategory, name="Cmax categories", unit="%", categorical=TRUE))) %>%
campsisnca::calculate()
table <- NCAMetricsTable()
table <- table %>%
add(c(ncaD1, ncaD7))
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```
### Example 9: Time above or below a certain threshold
In the example below, we look at the individual time above (or below) 10 ng/mL at Day 1 for the 10 first subjects.
```{r, message=FALSE}
day1 <- campsis %>%
timerange(0, 24) %>%
dplyr::filter(ID %in% (1:10))
campsis::spaghettiPlot(day1 , "Y") +
ggplot2::geom_hline(yintercept=10, linetype="dashed", color="red")
```
```{r, message=FALSE}
nca <- NCAMetrics(x=day1, variable="Y") %>%
add(c(Cmax(unit="ng/mL*h", stat_display="{mean}"),
TimeAboveLimit(limit=10, unit="h", stat_display="{mean}"),
TimeBelowLimit(limit=10, unit="h", stat_display="{mean}"))) %>%
calculate()
table <- NCAMetricsTable() %>%
add(nca)
table %>% export(dest="dataframe", type="individual_wide")
```
Summary statistics can also be exported:
```{r, message=FALSE}
table %>% export(dest="gt", subscripts=TRUE) %>% as_raw_html()
```