From 954f7514144a281b73e9b47ac88a6b13e8799f31 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Thu, 15 Oct 2020 10:25:48 +0200 Subject: Reintroduce saemix helper functions --- .travis.yml | 2 + DESCRIPTION | 6 +-- NAMESPACE | 5 ++ NEWS.md | 6 ++- R/saemix.R | 134 +++++++++++++++++++++++++++++++++++++++++++++++++ build.log | 2 +- man/saemix.Rd | 54 ++++++++++++++++++++ vignettes/FOCUS_D.html | 21 +++++--- vignettes/FOCUS_L.html | 65 +++++++++++++----------- vignettes/mkin.html | 25 ++++++--- 10 files changed, 273 insertions(+), 47 deletions(-) create mode 100644 R/saemix.R create mode 100644 man/saemix.Rd diff --git a/.travis.yml b/.travis.yml index a376a45f..e24cc7e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,8 @@ addons: apt: packages: - gcc +r_github_packages: + - saemixdevelopment/saemixextension script: - | R CMD build . diff --git a/DESCRIPTION b/DESCRIPTION index 3dec4d1d..80ba23d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: mkin Type: Package Title: Kinetic Evaluation of Chemical Degradation Data -Version: 0.9.50.3 -Date: 2020-10-08 +Version: 0.9.50.4 +Date: 2020-10-14 Authors@R: c(person("Johannes", "Ranke", role = c("aut", "cre", "cph"), email = "jranke@uni-bremen.de", comment = c(ORCID = "0000-0003-4371-6538")), @@ -17,7 +17,7 @@ Description: Calculation routines based on the FOCUS Kinetics Report (2006, note that no warranty is implied for correctness of results or fitness for a particular purpose. Imports: stats, graphics, methods, deSolve, R6, inline, parallel, numDeriv, - lmtest, pkgbuild, nlme (>= 3.1-149), purrr + lmtest, pkgbuild, nlme (>= 3.1-149), purrr, saemix (>= 3.1.9000) Suggests: knitr, rbenchmark, tikzDevice, testthat, rmarkdown, covr, vdiffr, benchmarkme, tibble, stats4 License: GPL diff --git a/NAMESPACE b/NAMESPACE index 7231cd46..0a04d6c3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -72,6 +72,8 @@ export(parms) export(plot_err) export(plot_res) export(plot_sep) +export(saemix_data) +export(saemix_model) export(sigma_twocomp) export(transform_odeparms) import(deSolve) @@ -88,6 +90,8 @@ importFrom(parallel,mclapply) importFrom(parallel,parLapply) importFrom(pkgbuild,has_compiler) importFrom(purrr,map_dfr) +importFrom(saemix,saemixData) +importFrom(saemix,saemixModel) importFrom(stats,AIC) importFrom(stats,BIC) importFrom(stats,aggregate) @@ -112,5 +116,6 @@ importFrom(stats,residuals) importFrom(stats,rnorm) importFrom(stats,shapiro.test) importFrom(stats,update) +importFrom(stats,var) importFrom(utils,getFromNamespace) importFrom(utils,write.table) diff --git a/NEWS.md b/NEWS.md index bb560699..e7ee2d5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,8 @@ -# mkin 0.9.50.3 +# mkin 0.9.50.4 (unreleased) + +- 'saemix_model', 'saemix_data': Helper functions to fit nonlinear mixed-effects models for mmkin row objects using the saemix package + +# mkin 0.9.50.3 (2020-10-08) - 'parms': Add a method for mmkin objects diff --git a/R/saemix.R b/R/saemix.R new file mode 100644 index 00000000..24c0f0d0 --- /dev/null +++ b/R/saemix.R @@ -0,0 +1,134 @@ +#' Create saemix models from mmkin row objects +#' +#' This function sets up a nonlinear mixed effects model for an mmkin row +#' object for use with the saemix package. An mmkin row object is essentially a +#' list of mkinfit objects that have been obtained by fitting the same model to +#' a list of datasets. +#' +#' Starting values for the fixed effects (population mean parameters, argument psi0 of +#' [saemix::saemixModel()] are the mean values of the parameters found using +#' mmkin. Starting variances of the random effects (argument omega.init) are the +#' variances of the deviations of the parameters from these mean values. +#' +#' @param object An mmkin row object containing several fits of the same model to different datasets +#' @param cores The number of cores to be used for multicore processing. +#' On Windows machines, cores > 1 is currently not supported. +#' @rdname saemix +#' @importFrom saemix saemixData saemixModel +#' @importFrom stats var +#' @examples +#' ds <- lapply(experimental_data_for_UBA_2019[6:10], +#' function(x) subset(x$data[c("name", "time", "value")])) +#' names(ds) <- paste("Dataset", 6:10) +#' sfo_sfo <- mkinmod(parent = mkinsub("SFO", "A1"), +#' A1 = mkinsub("SFO")) +#' \dontrun{ +#' f_mmkin <- mmkin(list("SFO-SFO" = sfo_sfo), ds, quiet = TRUE) +#' library(saemix) +#' m_saemix <- saemix_model(f_mmkin) +#' d_saemix <- saemix_data(f_mmkin) +#' saemix_options <- list(seed = 123456, +#' save = FALSE, save.graphs = FALSE, displayProgress = FALSE, +#' nbiter.saemix = c(200, 80)) +#' f_saemix <- saemix(m_saemix, d_saemix, saemix_options) +#' plot(f_saemix, plot.type = "convergence") +#' } +#' @return An [saemix::SaemixModel] object. +#' @export +saemix_model <- function(object, cores = parallel::detectCores()) { + if (nrow(object) > 1) stop("Only row objects allowed") + + mkin_model <- object[[1]]$mkinmod + analytical <- is.function(mkin_model$deg_func) + + degparms_optim <- mean_degparms(object) + psi0 <- matrix(degparms_optim, nrow = 1) + colnames(psi0) <- names(degparms_optim) + + degparms_fixed <- object[[1]]$bparms.fixed + + odeini_optim_parm_names <- grep('_0$', names(degparms_optim), value = TRUE) + odeini_fixed_parm_names <- grep('_0$', names(degparms_fixed), value = TRUE) + + odeparms_fixed_names <- setdiff(names(degparms_fixed), odeini_fixed_parm_names) + odeparms_fixed <- degparms_fixed[odeparms_fixed_names] + + odeini_fixed <- degparms_fixed[odeini_fixed_parm_names] + names(odeini_fixed) <- gsub('_0$', '', odeini_fixed_parm_names) + + model_function <- function(psi, id, xidep) { + + uid <- unique(id) + + res_list <- parallel::mclapply(uid, function(i) { + transparms_optim <- psi[i, ] + names(transparms_optim) <- names(degparms_optim) + + odeini_optim <- transparms_optim[odeini_optim_parm_names] + names(odeini_optim) <- gsub('_0$', '', odeini_optim_parm_names) + + odeini <- c(odeini_optim, odeini_fixed)[names(mkin_model$diffs)] + + ode_transparms_optim_names <- setdiff(names(transparms_optim), odeini_optim_parm_names) + odeparms_optim <- backtransform_odeparms(transparms_optim[ode_transparms_optim_names], mkin_model, + transform_rates = object[[1]]$transform_rates, + transform_fractions = object[[1]]$transform_fractions) + odeparms <- c(odeparms_optim, odeparms_fixed) + + xidep_i <- subset(xidep, id == i) + + if (analytical) { + out_values <- mkin_model$deg_func(xidep_i, odeini, odeparms) + } else { + + i_time <- xidep_i$time + i_name <- xidep_i$name + + out_wide <- mkinpredict(mkin_model, + odeparms = odeparms, odeini = odeini, + solution_type = object[[1]]$solution_type, + outtimes = sort(unique(i_time))) + + out_index <- cbind(as.character(i_time), as.character(i_name)) + out_values <- out_wide[out_index] + } + return(out_values) + }, mc.cores = cores) + res <- unlist(res_list) + return(res) + } + + raneff_0 <- mean_degparms(object, random = TRUE)$random$ds + var_raneff_0 <- apply(raneff_0, 2, var) + + res <- saemixModel(model_function, psi0, + "Mixed model generated from mmkin object", + transform.par = rep(0, length(degparms_optim)), + omega.init = diag(var_raneff_0) + ) + return(res) +} + +#' @rdname saemix +#' @param \dots Further parameters passed to [saemix::saemixData] +#' @return An [saemix::SaemixData] object. +#' @export +saemix_data <- function(object, ...) { + if (nrow(object) > 1) stop("Only row objects allowed") + ds_names <- colnames(object) + + ds_list <- lapply(object, function(x) x$data[c("time", "variable", "observed")]) + names(ds_list) <- ds_names + ds_saemix_all <- purrr::map_dfr(ds_list, function(x) x, .id = "ds") + ds_saemix <- data.frame(ds = ds_saemix_all$ds, + name = as.character(ds_saemix_all$variable), + time = ds_saemix_all$time, + value = ds_saemix_all$observed, + stringsAsFactors = FALSE) + + res <- saemixData(ds_saemix, + name.group = "ds", + name.predictors = c("time", "name"), + name.response = "value", ...) + return(res) +} diff --git a/build.log b/build.log index b94e6450..d101d06b 100644 --- a/build.log +++ b/build.log @@ -5,5 +5,5 @@ * creating vignettes ... OK * checking for LF line-endings in source and make files and shell scripts * checking for empty or unneeded directories -* building ‘mkin_0.9.50.3.tar.gz’ +* building ‘mkin_0.9.50.4.tar.gz’ diff --git a/man/saemix.Rd b/man/saemix.Rd new file mode 100644 index 00000000..eedbf537 --- /dev/null +++ b/man/saemix.Rd @@ -0,0 +1,54 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/saemix.R +\name{saemix_model} +\alias{saemix_model} +\alias{saemix_data} +\title{Create saemix models from mmkin row objects} +\usage{ +saemix_model(object, cores = parallel::detectCores()) + +saemix_data(object, ...) +} +\arguments{ +\item{object}{An mmkin row object containing several fits of the same model to different datasets} + +\item{cores}{The number of cores to be used for multicore processing. +On Windows machines, cores > 1 is currently not supported.} + +\item{\dots}{Further parameters passed to \link[saemix:saemixData]{saemix::saemixData}} +} +\value{ +An \link[saemix:SaemixModel-class]{saemix::SaemixModel} object. + +An \link[saemix:SaemixData-class]{saemix::SaemixData} object. +} +\description{ +This function sets up a nonlinear mixed effects model for an mmkin row +object for use with the saemix package. An mmkin row object is essentially a +list of mkinfit objects that have been obtained by fitting the same model to +a list of datasets. +} +\details{ +Starting values for the fixed effects (population mean parameters, argument psi0 of +\code{\link[saemix:saemixModel]{saemix::saemixModel()}} are the mean values of the parameters found using +mmkin. Starting variances of the random effects (argument omega.init) are the +variances of the deviations of the parameters from these mean values. +} +\examples{ +ds <- lapply(experimental_data_for_UBA_2019[6:10], + function(x) subset(x$data[c("name", "time", "value")])) +names(ds) <- paste("Dataset", 6:10) +sfo_sfo <- mkinmod(parent = mkinsub("SFO", "A1"), + A1 = mkinsub("SFO")) +\dontrun{ +f_mmkin <- mmkin(list("SFO-SFO" = sfo_sfo), ds, quiet = TRUE) +library(saemix) +m_saemix <- saemix_model(f_mmkin) +d_saemix <- saemix_data(f_mmkin) +saemix_options <- list(seed = 123456, + save = FALSE, save.graphs = FALSE, displayProgress = FALSE, + nbiter.saemix = c(200, 80)) +f_saemix <- saemix(m_saemix, d_saemix, saemix_options) +plot(f_saemix, plot.type = "convergence") +} +} diff --git a/vignettes/FOCUS_D.html b/vignettes/FOCUS_D.html index ea6acdbe..8aec3a45 100644 --- a/vignettes/FOCUS_D.html +++ b/vignettes/FOCUS_D.html @@ -11,7 +11,7 @@ - + Example evaluation of FOCUS Example Dataset D @@ -210,6 +210,15 @@ color: #d14; + + + + + +