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

## ----setup--------------------------------------------------------------------
library(nlmixr2targets)

## ----using-tar_nlmixr, eval = FALSE-------------------------------------------
# library(targets)
# library(tarchetypes)
# library(nlmixr2targets)
# 
# pheno <- function() {
#   ini({
#     lcl <- log(0.008); label("Typical value of clearance")
#     lvc <- log(0.6); label("Typical value of volume of distribution")
#     etalcl + etalvc ~ c(1,
#                         0.01, 1)
#     cpaddSd <- 0.1; label("residual variability")
#   })
#   model({
#     cl <- exp(lcl + etalcl)
#     vc <- exp(lvc + etalvc)
#     kel <- cl / vc
#     d / dt(central) <- -kel * central
#     cp <- central / vc
#     cp ~ add(cpaddSd)
#   })
# }
# 
# plan_model <-
#   tar_plan(
#     myData = nlmixr2data::pheno_sd,
#     tar_nlmixr(
#       model_pheno,
#       object = pheno,
#       data = myData,
#       est = "saem"
#     )
#   )
# 
# list(
#   plan_model
# )

## ----using-tar_nlmixr_multimodel, eval = FALSE--------------------------------
# library(targets)
# library(tarchetypes)
# library(nlmixr2targets)
# 
# pheno <- function() {
#   ini({
#     lcl <- log(0.008); label("Typical value of clearance")
#     lvc <- log(0.6); label("Typical value of volume of distribution")
#     etalcl + etalvc ~ c(1,
#                         0.01, 1)
#     cpaddSd <- 0.1; label("residual variability")
#   })
#   model({
#     cl <- exp(lcl + etalcl)
#     vc <- exp(lvc + etalvc)
#     kel <- cl / vc
#     d / dt(central) <- -kel * central
#     cp <- central / vc
#     cp ~ add(cpaddSd)
#   })
# }
# 
# pheno2 <- function() {
#   ini({
#     lcl <- log(0.008); label("Typical value of clearance")
#     lvc <- log(0.6); label("Typical value of volume of distribution")
#     etalcl + etalvc ~ c(2,
#                         0.01, 2)
#     cpaddSd <- 3.0; label("residual variability")
#   })
#   model({
#     cl <- exp(lcl + etalcl)
#     vc <- exp(lvc + etalvc)
#     kel <- cl / vc
#     d / dt(central) <- -kel * central
#     cp <- central / vc
#     cp ~ add(cpaddSd)
#   })
# }
# 
# plan_model <-
#   tar_nlmixr_multimodel(
#     all_models,
#     data = nlmixr2data::pheno_sd,
#     est = "saem",
#     "Base model; additive residual error = 1" = pheno,
#     "Base model; additive residual error = 3" = pheno2
#   )
# 
# plan_report <-
#   tar_plan(
#     # Determine the AIC for all tested models
#     aic_list = sapply(X = all_models, FUN = AIC)
#   )
# 
# list(
#   plan_model,
#   plan_report
# )

## ----piping-tar_nlmixr_multimodel, eval = FALSE-------------------------------
# library(targets)
# library(tarchetypes)
# library(nlmixr2targets)
# library(nlmixr2)
# 
# pheno <- function() {
#   ini({
#     lcl <- log(0.008); label("Typical value of clearance")
#     lvc <- log(0.6); label("Typical value of volume of distribution")
#     etalcl + etalvc ~ c(1,
#                         0.01, 1)
#     cpaddSd <- 0.1; label("residual variability")
#   })
#   model({
#     cl <- exp(lcl + etalcl)
#     vc <- exp(lvc + etalvc)
#     kel <- cl / vc
#     d / dt(central) <- -kel * central
#     cp <- central / vc
#     cp ~ add(cpaddSd)
#   })
# }
# 
# plan_model <-
#   tar_nlmixr_multimodel(
#     all_models,
#     data = nlmixr2data::pheno_sd,
#     est = "saem",
#     "Base model; additive residual error = 1" = pheno,
#     "Base model; additive residual error = 3" =
#     all_models[["Base model; additive residual error = 1"]] |> ini(cpaddSd = 3)
#   )
# 
# list(
#   plan_model
# )

