Tutorial"

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

Starting

The package tktdjl2r is a companion package to use from R the package TKTDsimulations.jl written in julia.

TKTDsimulations.jl is dedicated to the simulation of a large set of TKTD models using efficient differential equations solvers implemented in julia.

You need to have julia installed in your computer:see here for information about julia installation

If Julia is on your machine, then install and load tktdjl2r library:

library(tktdjl2r)

In order to make the link from julia to R, you have to run the setup command:

# Julia can be quite long to install the first time.
tktdjl2r::tktdjl2r_setup()

This command is going to call the JuliaCall R package and check or install required dependencies.

In the console, you should have (for instance on windows):

Julia version 1.5.0 at location C:\Users\...\AppData\Local\Programs\JULIA1~1.0\bin will be used.
Loading setup script for JuliaCall...
Finish loading setup script for JuliaCall.

Toxicokinetics

The first time this code is run can be long. But after the first run, the run should be highly faster.

single_runTK = runTK(c(0,1,2,3), c(0,1,2,2), 0.5)
kdVector = rbeta(4,10,10)

mcmc_runTK = runTK_MCMC(c(0,1,2,3), c(0,1,2,2), kdVector)

TKTD, Stochastic Death type SD

single_runSD = runSD(c(0,1,2,3), c(0,1,2,2), 0.5, 0.2, 1, 0.4)
paramDF = data.frame(
  kd = rbeta(4,10,10),
  hb = rbeta(4,10,10),
  z = rbeta(4,10,10),
  kk = rbeta(4,10,10))
mcmc_runSD = runSD_MCMC(c(0,1,2,3), c(0,1,2,2), paramDF$kd, paramDF$hb, paramDF$z, paramDF$kk)

TKTD, Individual Tolerance type IT

single_runIT = runIT(c(0,1,2,3), c(0,1,2,2),0.5, 0.2, 1, 0.4)
paramDF = data.frame(
  kd = rbeta(4,10,10),
  hb = rbeta(4,10,10),
  alpha = rbeta(4,10,10),
  beta = rbeta(4,10,10))
mcmc_runIT = runIT_MCMC(c(0,1,2,3), c(0,1,2,2), paramDF$kd, paramDF$hb, paramDF$alpha, paramDF$beta)

Comparison with pacakge 'deSolve'

Test for toxicokinetics, TK

library(deSolve)

model_TK <- function(t, State, parms, input) {
  with(as.list(c(parms, State)), {
    list(kd*(input(t) - State))    # internal damage
  })
}

deSolve_TK <- function(time, conc, listParameters){
  signal <- data.frame(times = time,
                       import = conc)

  sigimp <- stats::approxfun(signal$times,
                             signal$import,
                             method = "linear",
                             rule = 2)

  ## values for steady state
  xstart <- c(D = 0)
  ## model
  out <- ode(y = xstart,
             times = time,
             func = model_TK,
             parms = listParameters,
             input = sigimp)

  data.frame(
    time = time,
    exposure = conc,
    TK = out[,2]
  )

}

Plot both

listParameters = list(kd = 0.5)
testR_runTK = deSolve_TK(c(0,1,2,3), c(0,1,2,2), list(kd=0.5))
plot(c(0,1,2,3), single_runTK$TK, type = "l", lwd = 3)
lines(c(0,1,2,3), testR_runTK$TK, col = "red", lwd = 2)

Test speed

library(microbenchmark)

microbenchmark::microbenchmark(
  testJulia_runTK = runTK(c(0,1,2,3), c(0,1,2,2), 0.5),
  testR_runTK = deSolve_TK(c(0,1,2,3), c(0,1,2,2), list(kd=0.5)),
  times = 10
)


Try the tktdjl2r package in your browser

Any scripts or data that you put into this service are public.

tktdjl2r documentation built on Jan. 13, 2021, 5:08 a.m.