Skip to content

Commit 63ebc7d

Browse files
authored
Closes #94 PK Examples post (#99)
* #94 PK Examples post * #94 run styler --------- Co-authored-by: Jeffrey Dickinson <[email protected]>
1 parent 3221e3a commit 63ebc7d

File tree

5 files changed

+333
-1
lines changed

5 files changed

+333
-1
lines changed

inst/WORDLIST.txt

+59-1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,64 @@ VSDY
500500
VSORRES
501501
VSSEQ
502502
VSTESTCD
503+
adnca
504+
adppk
505+
ADPPK
506+
AETHNIC
507+
AETHNICN
508+
ARMCD
509+
BILI
510+
BILIBL
511+
bmi
512+
BMIBL
513+
bsa
514+
BSABL
515+
CKD
516+
codelist
517+
COHORTC
518+
COUNTRYL
519+
COUNTRYN
520+
covar
521+
CRCL
522+
CRCLBL
523+
creat
524+
CREAT
525+
CREATBL
526+
creatu
527+
egfr
528+
EGFRBL
529+
EPI
530+
EXDOSFRM
531+
EXROUTE
532+
FORMN
533+
HTBL
534+
labsbl
535+
LBBLFL
536+
LBSTRESN
537+
LBTESTCD
538+
LBTESTCDB
539+
metatools
540+
Mosteller
541+
pc
542+
pharmacokinetic
543+
pharmaverseadam
544+
poppk
545+
preconfigured
546+
RACEN
547+
ROUTEN
548+
SEXN
549+
SITEIDN
550+
STUDYIDN
551+
SUBJID
552+
SUBJIDN
553+
SUBJTYP
554+
SUBJTYPC
555+
TBILBL
556+
USUBJIDN
557+
VSBLFL
558+
vslb
559+
VSSTRESN
560+
WTBL
503561
analytics
504562
Analytics
505563
csp
@@ -514,7 +572,7 @@ traceRedir
514572
pKobZqjlXChj
515573
si
516574
vehIoJgdA
517-
youtuABLFL
575+
ABLFL
518576
admiraldisc
519577
admiraldiscovery
520578
advs
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# markdown helpers --------------------------------------------------------
2+
3+
markdown_appendix <- function(name, content) {
4+
paste(paste("##", name, "{.appendix}"), " ", content, sep = "\n")
5+
}
6+
markdown_link <- function(text, path) {
7+
paste0("[", text, "](", path, ")")
8+
}
9+
10+
11+
12+
# worker functions --------------------------------------------------------
13+
14+
insert_source <- function(repo_spec, name,
15+
collection = "posts",
16+
branch = "main",
17+
host = "https://github.com",
18+
text = "source code") {
19+
path <- paste(
20+
host,
21+
repo_spec,
22+
"tree",
23+
branch,
24+
collection,
25+
name,
26+
"code_sections.qmd",
27+
sep = "/"
28+
)
29+
return(markdown_link(text, path))
30+
}
31+
32+
insert_timestamp <- function(tzone = Sys.timezone()) {
33+
time <- lubridate::now(tzone = tzone)
34+
stamp <- as.character(time, tz = tzone, usetz = TRUE)
35+
return(stamp)
36+
}
37+
38+
insert_lockfile <- function(repo_spec, name,
39+
collection = "posts",
40+
branch = "main",
41+
host = "https://github.com",
42+
text = "R environment") {
43+
path <- paste(
44+
host,
45+
repo_spec,
46+
"tree",
47+
branch,
48+
collection,
49+
name,
50+
"renv.lock",
51+
sep = "/"
52+
)
53+
return(markdown_link(text, path))
54+
}
55+
56+
57+
58+
# top level function ------------------------------------------------------
59+
60+
insert_appendix <- function(repo_spec, name, collection = "posts") {
61+
appendices <- paste(
62+
markdown_appendix(
63+
name = "Last updated",
64+
content = insert_timestamp()
65+
),
66+
" ",
67+
markdown_appendix(
68+
name = "Details",
69+
content = paste(
70+
insert_source(repo_spec, name, collection),
71+
insert_lockfile(repo_spec, name, collection),
72+
sep = ", "
73+
)
74+
),
75+
sep = "\n"
76+
)
77+
knitr::asis_output(appendices)
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
---
2+
title: "PK Examples"
3+
author:
4+
- name: Jeff Dickinson
5+
description: "Explore PK ADaM Examples on Pharmaverse Examples Page"
6+
date: "2023-12-20"
7+
# please do not use any non-default categories.
8+
# You can find the default categories in the repository README.md
9+
categories: [community, admiral, metacore, metatools, xportr]
10+
# feel free to change the image
11+
image: "pharmaverse.png"
12+
---
13+
14+
<!--------------- typical setup ----------------->
15+
16+
```{r setup, include=FALSE}
17+
long_slug <- "2023-12-20_p_k__examples"
18+
# renv::use(lockfile = "renv.lock")
19+
```
20+
21+
<!--------------- post begins here ----------------->
22+
23+
# Pharmaverse PK Examples
24+
25+
A new [pharmaverse examples website](https://pharmaverse.github.io/examples){target="_blank"} has some exciting new features to explore.
26+
27+
One of these is the ability to launch Posit Cloud to explore the example code and make your own modifications.
28+
This interactive Posit Cloud environment is preconfigured with all required package installations.
29+
Click here: [Launch Posit Cloud](https://posit.cloud/content/7279124){target="_blank"} to explore the examples code.
30+
31+
This sample code here is based on the Population PK Analysis Data (ADPPK) model which follows the recently released [CDISC Implementation Guide](https://www.cdisc.org/standards/foundational/adam/basic-data-structure-adam-poppk-implementation-guide-v1-0){target="_blank"}.
32+
33+
Population PK models generally make use of nonlinear mixed effects models that require numeric variables.
34+
The data used in the models will include both dosing and concentration records, relative time variables, and numeric covariate variables.
35+
For more details see the `{admiral}` [vignette](https://pharmaverse.github.io/admiral/articles/pk_adnca.html){target="_blank"}.
36+
37+
## First Load Packages
38+
39+
First we will load the packages required for our project.
40+
We will use `{admiral}` for the creation of analysis data.
41+
`{admiral}` requires `{dplyr}`, `{lubridate}` and `{stringr}`.
42+
We will use `{metacore}` and `{metatools}` to store and manipulate metadata from our specifications.
43+
We will use `{xportr}` to perform checks on the final data and export to a transport file.
44+
45+
The source SDTM data will come from the CDISC pilot study data stored in `{pharmaversesdtm}` and the ADaM ADSL data will come from `{pharmaverseadam}`.
46+
47+
```{r echo=TRUE, message=FALSE}
48+
#| label: Load Packages
49+
# Load Packages
50+
library(admiral)
51+
library(dplyr)
52+
library(lubridate)
53+
library(stringr)
54+
library(metacore)
55+
library(metatools)
56+
library(xportr)
57+
library(readr)
58+
library(pharmaversesdtm)
59+
library(pharmaverseadam)
60+
```
61+
62+
## Next Load Specifications for Metacore
63+
64+
We have saved our specifications in an Excel file and will load them into `{metacore}` with the `metacore::spec_to_metacore()` function.
65+
66+
```{r echo=TRUE, message=FALSE}
67+
#| label: Load Specs
68+
#| warning: false
69+
# ---- Load Specs for Metacore ----
70+
metacore <- spec_to_metacore("pk_spec.xlsx") %>%
71+
select_dataset("ADPPK")
72+
```
73+
74+
## Load Source Datasets
75+
76+
We will load our SDTM data from `{pharmaversesdtm}`.
77+
The main components of the Population PK will be exposure data from `EX` and pharmacokinetic concentration data from `PC`.
78+
Here we will use `ADSL` from `{pharmaverseadam}` for baseline characteristics and we will derive additional baselines from vital signs `VS` and laboratory data `LB`.
79+
80+
```{r}
81+
#| label: Load Source
82+
# ---- Load source datasets ----
83+
# Load PC, EX, VS, LB and ADSL
84+
data("pc")
85+
data("ex")
86+
data("vs")
87+
data("lb")
88+
data("adsl")
89+
90+
ex <- convert_blanks_to_na(ex)
91+
pc <- convert_blanks_to_na(pc)
92+
vs <- convert_blanks_to_na(vs)
93+
lb <- convert_blanks_to_na(lb)
94+
```
95+
96+
## Derive Covariates Using `{metatools}`
97+
98+
In this step we will create our numeric covariates using the `metatools::create_var_from_codelist()` function.
99+
100+
```{r}
101+
#| label: Covariates
102+
#---- Derive Covariates ----
103+
# Include numeric values for STUDYIDN, USUBJIDN, SEXN, RACEN etc.
104+
105+
covar <- adsl %>%
106+
create_var_from_codelist(metacore, input_var = STUDYID, out_var = STUDYIDN) %>%
107+
create_var_from_codelist(metacore, input_var = SEX, out_var = SEXN) %>%
108+
create_var_from_codelist(metacore, input_var = RACE, out_var = RACEN) %>%
109+
create_var_from_codelist(metacore, input_var = ETHNIC, out_var = AETHNIC) %>%
110+
create_var_from_codelist(metacore, input_var = AETHNIC, out_var = AETHNICN) %>%
111+
create_var_from_codelist(metacore, input_var = ARMCD, out_var = COHORT) %>%
112+
create_var_from_codelist(metacore, input_var = ARMCD, out_var = COHORTC) %>%
113+
create_var_from_codelist(metacore, input_var = COUNTRY, out_var = COUNTRYN) %>%
114+
create_var_from_codelist(metacore, input_var = COUNTRY, out_var = COUNTRYL) %>%
115+
mutate(
116+
STUDYIDN = as.numeric(word(USUBJID, 1, sep = fixed("-"))),
117+
SITEIDN = as.numeric(word(USUBJID, 2, sep = fixed("-"))),
118+
USUBJIDN = as.numeric(word(USUBJID, 3, sep = fixed("-"))),
119+
SUBJIDN = as.numeric(SUBJID),
120+
ROUTE = unique(ex$EXROUTE),
121+
FORM = unique(ex$EXDOSFRM),
122+
REGION1 = COUNTRY,
123+
REGION1N = COUNTRYN,
124+
SUBJTYPC = "Volunteer",
125+
) %>%
126+
create_var_from_codelist(metacore, input_var = FORM, out_var = FORMN) %>%
127+
create_var_from_codelist(metacore, input_var = ROUTE, out_var = ROUTEN) %>%
128+
create_var_from_codelist(metacore, input_var = SUBJTYPC, out_var = SUBJTYP)
129+
```
130+
131+
### Derive Additional Baselines
132+
133+
Next we add additional baselines from vital signs and laboratory data.
134+
Several common variables are computed using some of the built in functions in `{admiral}`.
135+
136+
```{r}
137+
#| label: Baselines
138+
139+
labsbl <- lb %>%
140+
filter(LBBLFL == "Y" & LBTESTCD %in% c("CREAT", "ALT", "AST", "BILI")) %>%
141+
mutate(LBTESTCDB = paste0(LBTESTCD, "BL")) %>%
142+
select(STUDYID, USUBJID, LBTESTCDB, LBSTRESN)
143+
144+
covar_vslb <- covar %>%
145+
derive_vars_merged(
146+
dataset_add = vs,
147+
filter_add = VSTESTCD == "HEIGHT",
148+
by_vars = exprs(STUDYID, USUBJID),
149+
new_vars = exprs(HTBL = VSSTRESN)
150+
) %>%
151+
derive_vars_merged(
152+
dataset_add = vs,
153+
filter_add = VSTESTCD == "WEIGHT" & VSBLFL == "Y",
154+
by_vars = exprs(STUDYID, USUBJID),
155+
new_vars = exprs(WTBL = VSSTRESN)
156+
) %>%
157+
derive_vars_transposed(
158+
dataset_merge = labsbl,
159+
by_vars = exprs(STUDYID, USUBJID),
160+
key_var = LBTESTCDB,
161+
value_var = LBSTRESN
162+
) %>%
163+
mutate(
164+
BMIBL = compute_bmi(height = HTBL, weight = WTBL),
165+
BSABL = compute_bsa(
166+
height = HTBL,
167+
weight = HTBL,
168+
method = "Mosteller"
169+
),
170+
CRCLBL = compute_egfr(
171+
creat = CREATBL, creatu = "SI", age = AGE, weight = WTBL, sex = SEX,
172+
method = "CRCL"
173+
),
174+
EGFRBL = compute_egfr(
175+
creat = CREATBL, creatu = "SI", age = AGE, weight = WTBL, sex = SEX,
176+
method = "CKD-EPI"
177+
)
178+
) %>%
179+
rename(TBILBL = BILIBL)
180+
```
181+
182+
This covariate section of the code will be combined with the dosing and observation records from `EX` and `PC`.
183+
184+
The rest of the code can be seen on the [pharmaverse examples website](https://pharmaverse.github.io/examples){target="_blank"} or in the [Posit Cloud environment](https://posit.cloud/content/7279124){target="_blank"}.
185+
186+
Happy exploring!
187+
188+
<!--------------- appendices go here ----------------->
189+
190+
```{r, echo=FALSE}
191+
source("appendix.R")
192+
insert_appendix(
193+
repo_spec = "pharmaverse/blog",
194+
name = long_slug
195+
)
196+
```
Loading
39.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)