Get started with CAMPSIS model

Load example from model library

First import the campsismod package. This step is not required if you have already loaded the campsis package.

library(campsismod)

Load 2-compartment PK model from built-in model library and show content.

model <- model_library$advan4_trans4
show(model)
## [MAIN]
## KA=THETA_KA*exp(ETA_KA)
## CL=THETA_CL*exp(ETA_CL)
## V2=THETA_V2*exp(ETA_V2)
## V3=THETA_V3*exp(ETA_V3)
## Q=THETA_Q*exp(ETA_Q)
## S2=V2
## 
## [ODE]
## d/dt(A_DEPOT)=-KA*A_DEPOT
## d/dt(A_CENTRAL)=KA*A_DEPOT + Q*A_PERIPHERAL/V3 + (-CL/V2 - Q/V2)*A_CENTRAL
## d/dt(A_PERIPHERAL)=-Q*A_PERIPHERAL/V3 + Q*A_CENTRAL/V2
## d/dt(A_OUTPUT)=CL*A_CENTRAL/V2
## F=A_CENTRAL/S2
## 
## [ERROR]
## CP=F
## OBS_CP=CP*(EPS_PROP + 1)
## Y=OBS_CP
## 
## 
## THETA's:
##   name index value   fix
## 1   KA     1     1 FALSE
## 2   CL     2     5 FALSE
## 3   V2     3    80 FALSE
## 4   V3     4    20 FALSE
## 5    Q     5     4 FALSE
## OMEGA's:
##   name index index2 value   fix type same
## 1   KA     1      1 0.025 FALSE  var   NA
## 2   CL     2      2 0.025 FALSE  var   NA
## 3   V2     3      3 0.025 FALSE  var   NA
## 4   V3     4      4 0.025 FALSE  var   NA
## 5    Q     5      5 0.025 FALSE  var   NA
## SIGMA's:
##   name index index2 value   fix type
## 1 PROP     1      1 0.025 FALSE  var
## No variance-covariance matrix
## 
## Compartments:
## A_DEPOT (CMT=1)
## A_CENTRAL (CMT=2)
## A_PERIPHERAL (CMT=3)
## A_OUTPUT (CMT=4)

Write CAMPSIS model

A CAMPSIS model can be persisted on your local drive as follows:

model %>% write(file="path_to_model_folder")
## [1] TRUE
list.files("path_to_model_folder")
## [1] "model.campsis" "omega.csv"     "sigma.csv"     "theta.csv"

As shown, the output directory will contain the model (all code records) and 1 csv file per type of parameter (THETA, OMEGA and SIGMA).

Read CAMPSIS model

To read a CAMPSIS model from your local drive, use the read.campsis function. The exact same model should be retrieved.

model <- read.campsis(file="path_to_model_folder")
show(model)
## [MAIN]
## KA=THETA_KA*exp(ETA_KA)
## CL=THETA_CL*exp(ETA_CL)
## V2=THETA_V2*exp(ETA_V2)
## V3=THETA_V3*exp(ETA_V3)
## Q=THETA_Q*exp(ETA_Q)
## S2=V2
## 
## [ODE]
## d/dt(A_DEPOT)=-KA*A_DEPOT
## d/dt(A_CENTRAL)=KA*A_DEPOT + Q*A_PERIPHERAL/V3 + (-CL/V2 - Q/V2)*A_CENTRAL
## d/dt(A_PERIPHERAL)=-Q*A_PERIPHERAL/V3 + Q*A_CENTRAL/V2
## d/dt(A_OUTPUT)=CL*A_CENTRAL/V2
## F=A_CENTRAL/S2
## 
## [ERROR]
## CP=F
## OBS_CP=CP*(EPS_PROP + 1)
## Y=OBS_CP
## 
## 
## THETA's:
##   name index value   fix
## 1   KA     1     1 FALSE
## 2   CL     2     5 FALSE
## 3   V2     3    80 FALSE
## 4   V3     4    20 FALSE
## 5    Q     5     4 FALSE
## OMEGA's:
##   name index index2 value   fix type same
## 1   KA     1      1 0.025 FALSE  var   NA
## 2   CL     2      2 0.025 FALSE  var   NA
## 3   V2     3      3 0.025 FALSE  var   NA
## 4   V3     4      4 0.025 FALSE  var   NA
## 5    Q     5      5 0.025 FALSE  var   NA
## SIGMA's:
##   name index index2 value   fix type
## 1 PROP     1      1 0.025 FALSE  var
## No variance-covariance matrix
## 
## Compartments:
## A_DEPOT (CMT=1)
## A_CENTRAL (CMT=2)
## A_PERIPHERAL (CMT=3)
## A_OUTPUT (CMT=4)

The MAIN record is the part of the model where your model parameters are defined. The ODE record is where your ordinary differential equations (ODE) go, as well as any equation depending on the simulation time. The ERROR record is the place where the error model is defined. The model parameters are then shown, followed by the all the compartments.

Export CAMPSIS model to RxODE

campsismod has powerful export capabilities to RxODE and mrgsolve, the 2 simulation engines supported by campsis. The following code exports the model to RxODE. Please note that this step is implicit in CAMPSIS when you call the simulate method with your preferred simulation engine.

rxmod <- model %>% export(dest="RxODE")
rxmod
## An object of class "rxode_model"
## Slot "code":
##  [1] "KA=THETA_KA*exp(ETA_KA)"                                                   
##  [2] "CL=THETA_CL*exp(ETA_CL)"                                                   
##  [3] "V2=THETA_V2*exp(ETA_V2)"                                                   
##  [4] "V3=THETA_V3*exp(ETA_V3)"                                                   
##  [5] "Q=THETA_Q*exp(ETA_Q)"                                                      
##  [6] "S2=V2"                                                                     
##  [7] "d/dt(A_DEPOT)=-KA*A_DEPOT"                                                 
##  [8] "d/dt(A_CENTRAL)=KA*A_DEPOT + Q*A_PERIPHERAL/V3 + (-CL/V2 - Q/V2)*A_CENTRAL"
##  [9] "d/dt(A_PERIPHERAL)=-Q*A_PERIPHERAL/V3 + Q*A_CENTRAL/V2"                    
## [10] "d/dt(A_OUTPUT)=CL*A_CENTRAL/V2"                                            
## [11] "F=A_CENTRAL/S2"                                                            
## [12] "CP=F"                                                                      
## [13] "OBS_CP=CP*(EPS_PROP + 1)"                                                  
## [14] "Y=OBS_CP"                                                                  
## 
## Slot "theta":
## THETA_KA THETA_CL THETA_V2 THETA_V3  THETA_Q 
##        1        5       80       20        4 
## 
## Slot "omega":
##        ETA_KA ETA_CL ETA_V2 ETA_V3 ETA_Q
## ETA_KA  0.025  0.000  0.000  0.000 0.000
## ETA_CL  0.000  0.025  0.000  0.000 0.000
## ETA_V2  0.000  0.000  0.025  0.000 0.000
## ETA_V3  0.000  0.000  0.000  0.025 0.000
## ETA_Q   0.000  0.000  0.000  0.000 0.025
## 
## Slot "sigma":
##          EPS_PROP
## EPS_PROP    0.025

Export CAMPSIS model to mrgsolve

The following code exports the model to mrgsolve (text form).

mrgmod <- model %>% export(dest="mrgsolve")
mrgmod
## An object of class "mrgsolve_model"
## Slot "param":
## [1] "[PARAM] @annotated"       "THETA_KA : 1 : THETA_KA" 
## [3] "THETA_CL : 5 : THETA_CL"  "THETA_V2 : 80 : THETA_V2"
## [5] "THETA_V3 : 20 : THETA_V3" "THETA_Q : 4 : THETA_Q"   
## 
## Slot "cmt":
## [1] "[CMT] @annotated"          "A_DEPOT : DEPOT"          
## [3] "A_CENTRAL : CENTRAL"       "A_PERIPHERAL : PERIPHERAL"
## [5] "A_OUTPUT : OUTPUT"        
## 
## Slot "main":
## [1] "[MAIN]"                          "double KA=THETA_KA*exp(ETA_KA);"
## [3] "double CL=THETA_CL*exp(ETA_CL);" "double V2=THETA_V2*exp(ETA_V2);"
## [5] "double V3=THETA_V3*exp(ETA_V3);" "double Q=THETA_Q*exp(ETA_Q);"   
## [7] "double S2=V2;"                  
## 
## Slot "ode":
## [1] "[ODE]"                                                                     
## [2] "dxdt_A_DEPOT=-KA*A_DEPOT;"                                                 
## [3] "dxdt_A_CENTRAL=KA*A_DEPOT + Q*A_PERIPHERAL/V3 + (-CL/V2 - Q/V2)*A_CENTRAL;"
## [4] "dxdt_A_PERIPHERAL=-Q*A_PERIPHERAL/V3 + Q*A_CENTRAL/V2;"                    
## [5] "dxdt_A_OUTPUT=CL*A_CENTRAL/V2;"                                            
## [6] "double F=A_CENTRAL/S2;"                                                    
## 
## Slot "omega":
## [1] "[OMEGA] @annotated @block"     "ETA_KA : 0.025 : ETA_KA"      
## [3] "ETA_CL : 0 0.025 : ETA_CL"     "ETA_V2 : 0 0 0.025 : ETA_V2"  
## [5] "ETA_V3 : 0 0 0 0.025 : ETA_V3" "ETA_Q : 0 0 0 0 0.025 : ETA_Q"
## 
## Slot "sigma":
## [1] "[SIGMA] @annotated @block"   "EPS_PROP : 0.025 : EPS_PROP"
## 
## Slot "table":
## [1] "[TABLE]"                           "capture CP=F;"                    
## [3] "capture OBS_CP=CP*(EPS_PROP + 1);" "capture Y=OBS_CP;"                
## 
## Slot "capture":
## character(0)