NONMEM Dataset Example (free, with R code)

NONMEM Dataset Example (free, with R code)

I’m making available here a basic NONMEM dataset example, along with the R code used to create it. Hopefully it will be helpful to someone in future!

CSV download: http://jvdl.me/downloads/nonmem/dummy_nonmem.csv

XPT download: http://jvdl.me/downloads/nonmem/dummy_nonmem.xpt

library(dplyr)
library(tidyverse)

library(linpk)
library(haven)

rm(list = ls())

# basic structure
stdy <- c(1, 2, 3)
sex <- c(0, 1)
race <- c(1, 2, 3)
ntad <- c(0, 0.5, 1, 2, 4, 8, 12, 16, 24, 48, 72)
# end basic structure

# create dummy foundation with random wt, ht, bmi, age
foundation <- data.frame(stdy) %>% 
    full_join(data.frame(sex), by = character()) %>%
    full_join(data.frame(race), by = character()) %>%
    mutate(
        c = NA,
        id = row_number(),
        wt = runif(row_number(), 70, 100),
        ht = runif(row_number(), 150, 200),
        bmi = wt / ((ht / 100) ^ 2),
        age = runif(row_number(), 30, 60)
    ) %>% 
    mutate_at(
        vars(wt, ht, bmi), list(~ round(., 2))
    ) %>%
    mutate_at(
        vars(age), list(~ trunc(.))
    ) %>%
    full_join(data.frame(ntad), by = character())

# prepare pc
foundation.pc <- foundation %>%
    mutate(
        evid = 0L,
        cmt = 2L,
        mdv = 0, 
        amt = NA
    )

# prepare ex
foundation.ex <- foundation %>%
    filter(ntad == 0) %>%
    mutate(
        evid = 1L,
        cmt = 1L,
        addl = NA,
        ii = NA, 
        mdv = 1,
        amt = case_when(
            stdy == 1 ~ 100L,
            stdy == 2 ~ 200L,
            stdy == 3 ~ 300L,
            TRUE      ~ -99L
        )
    )

# set pc and ex and locf amt as dose
foundation.nonmem <- bind_rows(foundation.pc, foundation.ex) %>%
    arrange(stdy, id, ntad, evid) %>%
    mutate(
        dose = amt,
        tad = case_when(
            ntad == 0 & evid == 0 ~ -0.05,
            TRUE                  ~ ntad
        ),
    ) %>%
    group_by(stdy, id) %>%
    tidyr::fill(dose, .direction = c("downup")) %>%
    ungroup() 

# populate dummy concs with pklin::pkprofile
foundation.nonmem$dv = pkprofile(foundation.nonmem$tad, cl=0.5, vc=11, ka=1.3, dose = list(amt = foundation.nonmem$dose))

#introduce some randomness to pk concs
foundation.nonmem <- foundation.nonmem %>%
    mutate(
        dv = as.numeric(dv) * runif(row_number(), 1.1, 1.25) * (dose / 100),
        dv = ifelse(evid == 0, dv, NA), 
        lndv = case_when(
            dv == 0 ~ 0,
            TRUE    ~ log(dv)
        )
    ) %>%
    mutate_at(vars(dv, lndv), list(~ round(., digits = 2)))

# housekeeping, replace NAs, column names to uppercase, etc.
foundation.nonmem.final <- foundation.nonmem %>% 
    mutate(
        across(everything(), ~replace_na(.x, "."))
    ) %>%
    select(c, stdy, id, tad, ntad, dose, amt, addl, ii, evid, cmt, dv, lndv, mdv, sex, race, age, wt, ht, bmi)

names(foundation.nonmem.final) <- stringr::str_to_upper(names(foundation.nonmem.final))

# export to csv and xpt
setwd("~/R")
write_csv(foundation.nonmem.final, file = "dummy_nonmem.csv")
write_xpt(foundation.nonmem.final, path = "dummy_nonmem.xpt", name = "NM", version = 5)

Leave a Reply