knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Introduction

This vignette demonstrates extracting parameter estimates and labels into a table that can be used for diagnostics, or to generate reports. Note, this vignette exclusively deals with NONMEM models.

If you are new to bbr, the "Getting Started with bbr" vignette will take you through some basic scenarios for modeling with NONMEM using bbr, introducing you to its standard workflow and functionality.

Setup

There is some initial set up necessary for using bbr. Please refer to the "Getting Started" vignette, mentioned above, if you have not done this yet. Once this is done, load the library.

# NOTE: if running chunks interactively we need to load the package first
#   because renv isolation prevents us from finding an bbr installation
if (interactive()) {
  devtools::load_all()
}
library(bbr)
library(dplyr)
library(readr)
source("../tests/testthat/setup-param-labels-ref.R")
REF_OMEGA <- PARAM_BLOCK_REF$PEX_BLOCK32S$ctl

# set bbi execution path
if (Sys.which("bbi") == "") {
  options('bbr.bbi_exe_path' = read_bbi_path())
}

Parameter labels

Create Model Object

We have a control stream and output directory, because the model has already been run.

MODEL_DIR <- system.file("test-refs", "param-labels", "example-model", package = "bbr")

mod1 <- read_model(file.path(MODEL_DIR, "510"))
class(mod1)

The model object you have just created can now be passed to the post-processing functions to create your tables.

Extract Parameter labels

Currently, the param_labels() function parses labels from the comments in the control stream. Here is the relevant section of our example control stream.

mod1 %>% 
  get_model_path() %>%                          # get control stream file path
  read_lines(skip = 15, n_max = 18) %>%  # read in only parameter section
  paste(collapse = "\n") %>%
  cat()

This control stream is parsed into the tibble below, following the syntax defined in the "Details" section of the ?param_labels documentation.

label_df <- mod1 %>% 
  param_labels() %>% 
  apply_indices(.omega = block(2))
label_df

Note, there are some subtleties to the apply_indices() function that will be addressed in the next section.

Add Parameter Estimates

The user can also extract parameter estimates using the model_summary() and param_estimates() functions.

param_df <- mod1 %>% 
  model_summary() %>% 
  param_estimates()
param_df

These two tibbles can be joined together to create a table for including in reports.

report_df <- inner_join(
  label_df %>% select(-param_type),
  param_df %>% select(parameter_names, estimate, stderr),
  by = "parameter_names"
)
report_df

Applying indices

Because there are numerous ways of specifying the diagonal and off-diagonal elements of an $OMEGA or $SIGMA block in a control stream, automatically parsing the structure of these blocks can be brittle and error prone. For this reason, indices are not automatically added to the output of the param_labels() function and are instead added with the apply_indices() function.

Default behavior: All Diagonal

By default apply_indices() assumes that all $OMEGA and $SIGMA elements are diagonal. If this is the case, you do not need to pass anything to .omega or .sigma arguments described below. However, be careful that you do not accidentally overlook this because your indices will be incorrectly returned as simply (1,1), (2,2), (3,3), etc.

Specifying Block Structure

Block structure is specified with two arguments, .omega and .sigma which each take a logical vector. Each element in this vector corresponds to a parameter specified in the control stream and denotes whether that element is a diagonal in the relevant matrix.

For example, an $OMEGA BLOCK(3) would be represented with the vector .omega = c(TRUE, FALSE, TRUE, FALSE, FALSE, TRUE) because the first, third, and sixth elements are the diagonals and the others represent the off-diagonals. However, the user doesn't need to type this explicitly because bbr has a helper function block() that generates these vectors.

cat(paste("block(1): ", paste(block(1), collapse = ", "), "\n"))
cat(paste("block(2): ", paste(block(2), collapse = ", "), "\n"))
cat(paste("block(3): ", paste(block(3), collapse = ", "), "\n"))
cat(paste("block(4): ", paste(block(4), collapse = ", "), "\n"))

Notice the use of .omega = block(2) in the example from the previous section.

Mixed structure

More complicated block structures can be represented by concatenating these logical vectors together. For example, the following omega block:

cat(REF_OMEGA)
REF_OMEGA %>%
  param_labels() %>%
  apply_indices(
      .omega = c(block(3), block(2), block(1), block(1))
    )

It is equally valid to replace any of these with logical vectors as well. For instance, instead of c(..., block(1), block(1)) it may be easier to write c(..., rep(TRUE, 2)), especially if there are a number of BLOCK(1) lines in a row in the control stream.

SAME blocks

It is also worth noting here that blocks which use SAME (for example, $OMEGA BLOCK(1) SAME above) will inherit the label from the block to which SAME refers. This is true even if there are multiple SAME blocks in a row.



metrumresearchgroup/rbabylon documentation built on April 21, 2024, 3:26 a.m.