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.

saemix_model(object, cores = parallel::detectCores())

saemix_data(object, ...)

Arguments

object

An mmkin row object containing several fits of the same model to different datasets

cores

The number of cores to be used for multicore processing. On Windows machines, cores > 1 is currently not supported.

...

Further parameters passed to saemix::saemixData

Value

An saemix::SaemixModel object.

An saemix::SaemixData object.

Details

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.

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"))
#> Successfully compiled differential equation model from auto-generated C code.
# \dontrun{ f_mmkin <- mmkin(list("SFO-SFO" = sfo_sfo), ds, quiet = TRUE) library(saemix)
#> Package saemix, version 3.1.9000 #> please direct bugs, questions and feedback to emmanuelle.comets@inserm.fr
m_saemix <- saemix_model(f_mmkin, cores = 1)
#> #> #> The following SaemixModel object was successfully created: #> #> Nonlinear mixed-effects model #> Model function: Mixed model generated from mmkin object Model type: structural #> 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) #> } #> <bytecode: 0x55555c1e7720> #> <environment: 0x55555c1eff38> #> Nb of parameters: 4 #> parameter names: parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> distribution: #> Parameter Distribution Estimated #> [1,] parent_0 normal Estimated #> [2,] log_k_parent normal Estimated #> [3,] log_k_A1 normal Estimated #> [4,] f_parent_ilr_1 normal Estimated #> Variance-covariance matrix: #> parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> parent_0 1 0 0 0 #> log_k_parent 0 1 0 0 #> log_k_A1 0 0 1 0 #> f_parent_ilr_1 0 0 0 1 #> Error model: constant , initial values: a.1=4.97259024646577 #> No covariate in the model. #> Initial values #> parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> Pop.CondInit 86.53449 -3.207005 -3.060308 -1.920449
d_saemix <- saemix_data(f_mmkin)
#> #> #> The following SaemixData object was successfully created: #> #> Object of class SaemixData #> longitudinal data for use with the SAEM algorithm #> Dataset ds_saemix #> Structured data: value ~ time + name | ds #> X variable for graphs: time ()
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)
#> Running main SAEM algorithm #> [1] "Thu Oct 15 11:55:14 2020" #> .. #> Minimisation finished #> [1] "Thu Oct 15 11:57:07 2020"
#> Nonlinear mixed-effects model fit by the SAEM algorithm #> ----------------------------------- #> ---- Data ---- #> ----------------------------------- #> Object of class SaemixData #> longitudinal data for use with the SAEM algorithm #> Dataset ds_saemix #> Structured data: value ~ time + name | ds #> X variable for graphs: time () #> Dataset characteristics: #> number of subjects: 5 #> number of observations: 170 #> average/min/max nb obs: 34.00 / 30 / 38 #> First 10 lines of data: #> ds time name value mdv cens occ ytype #> 1 Dataset 6 0 parent 97.2 0 0 1 1 #> 2 Dataset 6 0 parent 96.4 0 0 1 1 #> 3 Dataset 6 3 parent 71.1 0 0 1 1 #> 4 Dataset 6 3 parent 69.2 0 0 1 1 #> 5 Dataset 6 6 parent 58.1 0 0 1 1 #> 6 Dataset 6 6 parent 56.6 0 0 1 1 #> 7 Dataset 6 10 parent 44.4 0 0 1 1 #> 8 Dataset 6 10 parent 43.4 0 0 1 1 #> 9 Dataset 6 20 parent 33.3 0 0 1 1 #> 10 Dataset 6 20 parent 29.2 0 0 1 1 #> ----------------------------------- #> ---- Model ---- #> ----------------------------------- #> Nonlinear mixed-effects model #> Model function: Mixed model generated from mmkin object Model type: structural #> 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) #> } #> <bytecode: 0x55555c1e7720> #> <environment: 0x55555c1eff38> #> Nb of parameters: 4 #> parameter names: parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> distribution: #> Parameter Distribution Estimated #> [1,] parent_0 normal Estimated #> [2,] log_k_parent normal Estimated #> [3,] log_k_A1 normal Estimated #> [4,] f_parent_ilr_1 normal Estimated #> Variance-covariance matrix: #> parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> parent_0 1 0 0 0 #> log_k_parent 0 1 0 0 #> log_k_A1 0 0 1 0 #> f_parent_ilr_1 0 0 0 1 #> Error model: constant , initial values: a.1=4.97259024646577 #> No covariate in the model. #> Initial values #> parent_0 log_k_parent log_k_A1 f_parent_ilr_1 #> Pop.CondInit 86.53449 -3.207005 -3.060308 -1.920449 #> ----------------------------------- #> ---- Key algorithm options ---- #> ----------------------------------- #> Estimation of individual parameters (MAP) #> Estimation of standard errors and linearised log-likelihood #> Estimation of log-likelihood by importance sampling #> Number of iterations: K1=200, K2=80 #> Number of chains: 10 #> Seed: 123456 #> Number of MCMC iterations for IS: 5000 #> Simulations: #> nb of simulated datasets used for npde: 1000 #> nb of simulated datasets used for VPC: 100 #> Input/output #> save the results to a file: FALSE #> save the graphs to files: FALSE #> ---------------------------------------------------- #> ---- Results ---- #> ---------------------------------------------------- #> ----------------- Fixed effects ------------------ #> ---------------------------------------------------- #> Parameter Estimate SE CV(%) #> parent_0 86.09 1.57 1.8 #> log_k_parent -3.21 0.59 18.5 #> log_k_A1 -4.69 0.31 6.6 #> f_parent_ilr_1 -0.34 0.30 89.2 #> a a.1 4.69 0.27 5.8 #> ---------------------------------------------------- #> ----------- Variance of random effects ----------- #> ---------------------------------------------------- #> Parameter Estimate SE CV(%) #> parent_0 omega2.parent_0 7.07 7.72 109 #> log_k_parent omega2.log_k_parent 1.75 1.11 63 #> log_k_A1 omega2.log_k_A1 0.28 0.28 99 #> f_parent_ilr_1 omega2.f_parent_ilr_1 0.39 0.27 71 #> ---------------------------------------------------- #> ------ Correlation matrix of random effects ------ #> ---------------------------------------------------- #> omega2.parent_0 omega2.log_k_parent omega2.log_k_A1 #> omega2.parent_0 1 0 0 #> omega2.log_k_parent 0 1 0 #> omega2.log_k_A1 0 0 1 #> omega2.f_parent_ilr_1 0 0 0 #> omega2.f_parent_ilr_1 #> omega2.parent_0 0 #> omega2.log_k_parent 0 #> omega2.log_k_A1 0 #> omega2.f_parent_ilr_1 1 #> ---------------------------------------------------- #> --------------- Statistical criteria ------------- #> ---------------------------------------------------- #> Likelihood computed by linearisation #> -2LL= 1064.35 #> AIC = 1082.35 #> BIC = 1078.835 #> #> Likelihood computed by importance sampling #> -2LL= 1063.475 #> AIC = 1081.475 #> BIC = 1077.96 #> ----------------------------------------------------
plot(f_saemix, plot.type = "convergence")
#> Plotting convergence plots
# } # Synthetic data with two-component error sampling_times = c(0, 1, 3, 7, 14, 28, 60, 90, 120) dt50_sfo_in <- c(800, 900, 1000, 1111.11, 1250) k_in <- log(2) / dt50_sfo_in SFO <- mkinmod(parent = mkinsub("SFO")) pred_sfo <- function(k) { mkinpredict(SFO, c(k_parent = k), c(parent = 100), sampling_times) } ds_sfo_mean <- lapply(k_in, pred_sfo) set.seed(123456L) ds_sfo_syn <- lapply(ds_sfo_mean, function(ds) { add_err(ds, sdfunc = function(value) sqrt(1^2 + value^2 * 0.07^2), n = 1)[[1]] }) f_mmkin_syn <- mmkin("SFO", ds_sfo_syn, error_model = "tc", quiet = TRUE) m_saemix_tc <- saemix_model(f_mmkin_syn, cores = 1)
#> #> #> The following SaemixModel object was successfully created: #> #> Nonlinear mixed-effects model #> Model function: Mixed model generated from mmkin object Model type: structural #> 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) #> } #> <bytecode: 0x55555c1e7720> #> <environment: 0x55555be5b848> #> Nb of parameters: 2 #> parameter names: parent_0 log_k_parent #> distribution: #> Parameter Distribution Estimated #> [1,] parent_0 normal Estimated #> [2,] log_k_parent normal Estimated #> Variance-covariance matrix: #> parent_0 log_k_parent #> parent_0 1 0 #> log_k_parent 0 1 #> Error model: combined , initial values: a.1=2.4206146215511 b.1=0.03831266409084 #> No covariate in the model. #> Initial values #> parent_0 log_k_parent #> Pop.CondInit 100.2498 -9.33922
d_saemix_tc <- saemix_data(f_mmkin_syn)
#> #> #> The following SaemixData object was successfully created: #> #> Object of class SaemixData #> longitudinal data for use with the SAEM algorithm #> Dataset ds_saemix #> Structured data: value ~ time + name | ds #> X variable for graphs: time ()
f_saemix_tc <- saemix(m_saemix_tc, d_saemix_tc, saemix_options)
#> Running main SAEM algorithm #> [1] "Thu Oct 15 11:57:25 2020" #> .. #> Minimisation finished #> [1] "Thu Oct 15 11:58:16 2020" #> Error in solve.default(Fth) : #> Lapack routine dgesv: system is exactly singular: U[2,2] = 0 #> Error computing the Fisher Information Matrix: singular system. #> Error in solve.default(FO) : #> Lapack routine dgesv: system is exactly singular: U[2,2] = 0 #> Error computing the Fisher Information Matrix: singular system.
#> Nonlinear mixed-effects model fit by the SAEM algorithm #> ----------------------------------- #> ---- Data ---- #> ----------------------------------- #> Object of class SaemixData #> longitudinal data for use with the SAEM algorithm #> Dataset ds_saemix #> Structured data: value ~ time + name | ds #> X variable for graphs: time () #> Dataset characteristics: #> number of subjects: 5 #> number of observations: 90 #> average/min/max nb obs: 18.00 / 18 / 18 #> First 10 lines of data: #> ds time name value mdv cens occ ytype #> 1 1 0 parent 105.9 0 0 1 1 #> 2 1 0 parent 98.0 0 0 1 1 #> 3 1 1 parent 97.4 0 0 1 1 #> 4 1 1 parent 100.5 0 0 1 1 #> 5 1 3 parent 115.6 0 0 1 1 #> 6 1 3 parent 105.6 0 0 1 1 #> 7 1 7 parent 108.6 0 0 1 1 #> 8 1 7 parent 117.0 0 0 1 1 #> 9 1 14 parent 107.0 0 0 1 1 #> 10 1 14 parent 95.8 0 0 1 1 #> ----------------------------------- #> ---- Model ---- #> ----------------------------------- #> Nonlinear mixed-effects model #> Model function: Mixed model generated from mmkin object Model type: structural #> 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) #> } #> <bytecode: 0x55555c1e7720> #> <environment: 0x55555be5b848> #> Nb of parameters: 2 #> parameter names: parent_0 log_k_parent #> distribution: #> Parameter Distribution Estimated #> [1,] parent_0 normal Estimated #> [2,] log_k_parent normal Estimated #> Variance-covariance matrix: #> parent_0 log_k_parent #> parent_0 1 0 #> log_k_parent 0 1 #> Error model: combined , initial values: a.1=2.4206146215511 b.1=0.03831266409084 #> No covariate in the model. #> Initial values #> parent_0 log_k_parent #> Pop.CondInit 100.2498 -9.33922 #> ----------------------------------- #> ---- Key algorithm options ---- #> ----------------------------------- #> Estimation of individual parameters (MAP) #> Estimation of standard errors and linearised log-likelihood #> Estimation of log-likelihood by importance sampling #> Number of iterations: K1=200, K2=80 #> Number of chains: 10 #> Seed: 123456 #> Number of MCMC iterations for IS: 5000 #> Simulations: #> nb of simulated datasets used for npde: 1000 #> nb of simulated datasets used for VPC: 100 #> Input/output #> save the results to a file: FALSE #> save the graphs to files: FALSE #> ---------------------------------------------------- #> ---- Results ---- #> ---------------------------------------------------- #> ----------------- Fixed effects ------------------ #> ---------------------------------------------------- #> Parameter Estimate SE CV(%) #> parent_0 97.44 <NA> <NA> #> log_k_parent -56.97 <NA> <NA> #> a a.1 -7.12 <NA> <NA> #> b b.1 0.15 <NA> <NA> #> ---------------------------------------------------- #> ----------- Variance of random effects ----------- #> ---------------------------------------------------- #> Parameter Estimate SE CV(%) #> parent_0 omega2.parent_0 2.8 NA NA #> log_k_parent omega2.log_k_parent 750.3 NA NA #> ---------------------------------------------------- #> ------ Correlation matrix of random effects ------ #> ---------------------------------------------------- #> omega2.parent_0 omega2.log_k_parent #> omega2.parent_0 1 0 #> omega2.log_k_parent 0 1 #> ---------------------------------------------------- #> --------------- Statistical criteria ------------- #> ---------------------------------------------------- #> Likelihood computed by linearisation #> -2LL= 623.7744 #> AIC = 635.7744 #> BIC = 633.431 #> #> Likelihood computed by importance sampling #> -2LL= 621.1909 #> AIC = 633.1909 #> BIC = 630.8475 #> ----------------------------------------------------
plot(f_saemix_tc, plot.type = "convergence")
#> Plotting convergence plots