Skip to content

PopED_vignette

curtisj edited this page Jun 2, 2016 · 5 revisions

Revision history:

  • curtisj: origin vignette (2015-03-17)
  • kyleb: updated mrgsolve elements for 0.6.1 + some changes to R coding (2016-05-16)

Load mrgsolve and PopED packages

library(mrgsolve)
library(PopED)

Model

  • 1 compartment PK model with oral dosing
  • Note: we could also now write this model with $PKMODEL
code <- '
$PARAM CL=0.02, VC=0.5, KA=0.9
$CMT GUT CENT
$ODE
dxdt_GUT = -KA*GUT;
dxdt_CENT = KA*GUT - (CL/VC)*CENT;
$TABLE table(CP)  = CENT/VC;
'

Compile and load the model with mcode

moda <- mcode("optim", code)

This option is used to make this script run fast but without convergence. This allows you to easily see how things work without waiting for more extended periods of time. Change to FALSE if you want to run each function so that the solutions have converged.

fast <- TRUE 

iNumSimulations <- ifelse(fast,5,100)
EAStepSize <- ifelse(fast,40,1)
rsit <- ifelse(fast,3,300)
sgit <- ifelse(fast,3,150)
ls_step_size <- ifelse(fast,3,50)
iter_max <- ifelse(fast,1,10)

Define the ODE function using mrgsim

ff <- function(model_switch, xt, p, poped.db){
  times_xt <- drop(xt)  
  dose_times <- seq(from=0,to=max(times_xt),by=p[["TAU"]])
  time <- sort(unique(c(times_xt,dose_times)))
  is.dose <- time %in% dose_times
  
  data <- 
    dplyr::data_frame(ID = 1,
                      time = time,
                      amt = ifelse(is.dose,p[["DOSE"]], 0), 
                      cmt = ifelse(is.dose, 1, 0), 
                      evid = cmt,
                      CL = p[["CL"]], VC = p[["VC"]], KA = p[["KA"]])
  
  out <- mrgsim(moda, data=data, maxsteps=10000)
  
  y <-  out$CP
  
  y <- y[match(times_xt,out$time)]
  
  return(list(y=matrix(y,ncol=1),poped.db=poped.db))
  
}

Define the parameters

sfg <- function(x,a,bpop,b,bocc){
  parameters=c( CL=bpop[1]*exp(b[1]),
                VC=bpop[2]*exp(b[2]),
                KA=bpop[3]*exp(b[3]),
                DOSE=a[1],
                TAU=a[2])
  return( parameters ) 
}

Define the residual error component

feps <- function(model_switch,xt,parameters,epsi,poped.db){
  returnArgs <- do.call(poped.db$model$ff_pointer,list(model_switch,xt,parameters,poped.db)) 
  y <- returnArgs[[1]]
  poped.db <- returnArgs[[2]]
  
  y = y*(1+epsi[,1])+epsi[,2]
  
  return(list( y= y,poped.db =poped.db )) 
}

Define the PopED database

poped.db <- create.poped.database(ff_file="ff",
                                  fg_file="sfg",
                                  fError_file="feps",
                                  bpop=c(CL=3.75, VC=72.8, KA=0.25), 
                                  notfixed_bpop=c(1,1,1),
                                  d=c(CL=0.25^2, VC=0.09, KA=0.09),
                                  notfixed_d = c(1,1,1),
                                  sigma=c(0.04,5e-6),
                                  notfixed_sigma=c(0,0),
                                  m=2,
                                  groupsize=20,
                                  xt=c( 1,2,8,240,245), 
                                  minxt=c(0,0,0,240,240), 
                                  maxxt=c(10,15,100,248,248), 
                                  bUseGrouped_xt=1,          
                                  a=cbind(c(20,40),c(24,24)), 
                                  maxa=c(200,24), 
                                  mina=c(0,24))   

Create plot of model without variability

plot_model_prediction(poped.db)

Create plot of model with variability

plot_model_prediction(poped.db,IPRED=TRUE,IPRED.lines=FALSE, IPRED.lines.pctls=FALSE,
                      DV=TRUE, DV.lines=FALSE, DV.points=FALSE, separate.groups=TRUE, PRED=TRUE,
                      sample.times = TRUE, sample.times.IPRED = FALSE, sample.times.DV = FALSE)

Evaluate the FIM

FIM <- evaluate.fim(poped.db, fim.calc.type = 1, deriv.type = 1) 

FIM

.              [,1]        [,2]        [,3]        [,4]        [,5]
. [1,]  37.15758355 -0.05864137  -14.445537    0.000000    0.000000
. [2,]  -0.05864137  0.05337456   -8.685805    0.000000    0.000000
. [3,] -14.44553703 -8.68580523 3002.352962    0.000000    0.000000
. [4,]   0.00000000  0.00000000    0.000000 3412.951090    3.203639
. [5,]   0.00000000  0.00000000    0.000000    3.203639 1000.239983
. [6,]   0.00000000  0.00000000    0.000000    2.292572  312.372715
.            [,6]
. [1,]   0.000000
. [2,]   0.000000
. [3,]   0.000000
. [4,]   2.292572
. [5,] 312.372715
. [6,] 440.144778

det(FIM)

. [1] 3.643077e+12

get_rse(FIM,poped.db)

.   bpop[1]   bpop[2]   bpop[3]    D[1,1]    D[2,2]    D[3,3] 
.  4.399998  8.212718 10.084266 27.387722 39.821268 60.030163

Optimize sample times

# output <- poped_optimize(poped.db,opt_xt=T,
#                          rsit=rsit,sgit=sgit,ls_step_size=ls_step_size,
#                          iter_max=iter_max)
#
Clone this wiki locally