## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(nlmixr2targets)

## ----multimodel-plan, eval = FALSE--------------------------------------------
# library(targets)
# library(tarchetypes)
# library(nlmixr2targets)
# 
# pheno_base <- function() {
#   ini({
#     lcl <- log(0.008); label("Typical clearance")
#     lvc <- log(0.6); label("Typical volume of distribution")
#     etalcl + etalvc ~ c(1, 0.01, 1)
#     cpaddSd <- 0.1; label("Additive residual SD")
#   })
#   model({
#     cl <- exp(lcl + etalcl)
#     vc <- exp(lvc + etalvc)
#     kel <- cl / vc
#     d / dt(central) <- -kel * central
#     cp <- central / vc
#     cp ~ add(cpaddSd)
#   })
# }
# 
# list(
#   tar_nlmixr_multimodel(
#     name = candidate_fits,
#     data = nlmixr2data::pheno_sd,
#     est  = "saem",
#     "Base"                              = pheno_base,
#     "Base + tighter residual prior"     = pheno_base |> ini(cpaddSd = 0.05),
#     "Base + alternate residual"         = pheno_base |> model({
#       cp ~ prop(cpaddSd)
#     }, append = TRUE)
#   ),
#   tar_target(
#     aic_table,
#     data.frame(
#       model = names(candidate_fits),
#       aic   = vapply(candidate_fits, AIC, numeric(1)),
#       bic   = vapply(candidate_fits, BIC, numeric(1)),
#       ofv   = vapply(candidate_fits, function(f) f$objDf$OBJF[1], numeric(1))
#     )
#   )
# )

## ----param-extract, eval = FALSE----------------------------------------------
# tar_target(
#   clearance_estimates,
#   data.frame(
#     model = names(candidate_fits),
#     lcl   = vapply(
#       candidate_fits,
#       function(f) f$ui$iniDf$est[f$ui$iniDf$name == "lcl"],
#       numeric(1)
#     )
#   )
# )

## ----within-list-piping, eval = FALSE-----------------------------------------
# tar_nlmixr_multimodel(
#   name = candidate_fits,
#   data = nlmixr2data::pheno_sd,
#   est  = "saem",
#   "Base"            = pheno_base,
#   "Tighter residual" =
#     candidate_fits[["Base"]] |> ini(cpaddSd = 0.05)
# )

