`simulation.Rmd`

The purpose of this vignette is to demonstrate how to edit a build-in model and then simulate it.

The model demonstrated is a two-compartment model with an IV bolus. It involves both plasma and urine compartments with the urine compartment reset to zero right after each observation.

It is assumed that directory with NLME Executables is given as an environment variable (INSTALLDIR), which can be checked with the following commands:

```
# loading the package
library(Certara.RsNLME)
library(data.table)
library(ggplot2)
library(dplyr)
# Check the environment variable
Sys.getenv("INSTALLDIR")
1] "C:\\Program Files\\Certara\\NLME_Engine" [
```

First we will create the simulation input dataset

Next, we will define the model and the associate column mappings:

- Create a basic two-compartment PK model with IV bolus and define the associated column mappings
- Change initial values for fixed effects tvV, tvV2, and tvCl2, to 5, 3, 0.5, respectively
- Change the covariance matrix of random effects to be a diagonal matrix with all the diagonal elements being 0.1
- Change the standard deviation of residual error variable corresponding to observed variable A0Obs to 0.2

```
ModelName <- "TwoCpt_IVBolus_1stOrderElim_PlasmaUrineObs"
model <- pkmodel(numCompartments = 2, hasEliminationComp = TRUE, isClosedForm = FALSE
, data = dt_SimInputData, ID = "ID", Time = "Time", A1 = "Dose"
, modelName = ModelName
) %>%
fixedEffect(effect = c("tvV", "tvV2", "tvCl2"), value = c(5, 3, 0.5)) %>%
randomEffect(effect = c("nV", "nCl", "nV2", "nCl2"), value = rep(0.1, 4)) %>%
residualError(predName = "A0", SD = 0.2)
```

Let’s edit model through `editModel`

function from
`Certara.RsNlme`

package.

` model <- editModel(model)`

To reset the urine compartment to 0 right after each observation, add “, doafter = {A0 = 0}” inside the observe statement for A0Obs; that is, change the following statement:

` observe(A0Obs=A0 * ( 1 + A0Eps))`

to

` observe(A0Obs=A0 * ( 1 + A0Eps), doafter = {A0 = 0})`

and then click the “Save” button to update the model.

We will define simulation tables for plasma and urine observations, set the number of replicates to be 50, and change the staring seed to 1:

```
## Define simulation tables for plasma observations
SimTableCObs <- NlmeSimTableDef(name = "SimTableCObs.csv"
, timesList = c(0, 0.5, 1, 2, 4, 8, 12, 16, 20, 24)
, variablesList = c("C", "CObs")
)
## Define simulation tables for urine observations
SimTableA0Obs <- NlmeSimTableDef(name = "SimTableA0Obs.csv"
, timesList = c(12, 24)
, variablesList = "A0Obs"
)
## Simulation setup
SimSetup <- NlmeSimulationParams(numReplicates = 50
, seed = 1
, simulationTables = c(SimTableCObs, SimTableA0Obs)
)
```

Next, we will run the model using the simulation setup given above
through simmodel
function from `Certara.RsNlme`

package.

```
<- simmodel(model, SimSetup)
job
Host argument is not given.
Using localhost without parallelization.
NLME Jobin the host class instance.
sharedDirectory is not given :
Valid NLME_ROOT_DIRECTORY is not given, using current working directory:/Users/jcraig/Documents/GitHub/R-RsNLME/vignettes
C
1 of 1 NLME models
Compiling
The model compiled
Trying to generate job results...
Finished summarizing results. Transferring data and loading the results...
Done generating job results.
/Simulation results are ready in C:/Users/jcraig/Documents/GitHub/R-RsNLME/vignettes/TwoCpt_IVBolus_1stOrderElim_PlasmaUrineObs
VPC
Loading the results
Loading SimTableCObs.csv
Loading SimTableA0Obs.csv Loading predout.csv
```

The returned `job`

object contains all the simulation
tables defined. We will build plots for both simulated drug
concentration at the central compartment and simulated urine
observations at each dose level specified:

```
## Simulated drug concentration at the central compartment
dt_CObs <- job$SimTableCObs
setnames(dt_CObs, c("# repl"), c("Replicate"))
dt_CObs$Replicate <- as.factor(dt_CObs$Replicate)
dt_CObs$id5 <- as.factor(dt_CObs$id5)
levels(dt_CObs$id5) = c(paste0("Dose = ", dt_SimInputData$Dose[1]),
paste0("Dose = ", dt_SimInputData$Dose[2]))
## Plot simulated drug concentration at the central compartment
ggplot(dt_CObs, aes(x = time, y = CObs, group = Replicate, color = Replicate)) +
scale_y_log10() +
geom_line() +
geom_point() +
ylab("Drug Concentration \n at the central compartment") +
facet_grid(.~ id5)
```

```
## Simulated urine observations
dt_A0Obs <- job$SimTableA0Obs
setnames(dt_A0Obs, c("# repl"), c("Replicate"))
dt_A0Obs$Replicate <- as.factor(dt_A0Obs$Replicate)
dt_A0Obs$id5 <- as.factor(dt_A0Obs$id5)
levels(dt_A0Obs$id5) = c(paste0("Dose = ", dt_SimInputData$Dose[1]),
paste0("Dose = ", dt_SimInputData$Dose[2]))
## Plot simulated urine observations
ggplot(dt_A0Obs, aes(x = time, y = A0Obs, group = Replicate, color = Replicate)) +
geom_point() +
facet_grid(.~ id5)
```