From c36d8f49a6c046aa76646fd463fad20445d3feb4 Mon Sep 17 00:00:00 2001 From: rsetienne Date: Fri, 28 Nov 2025 14:51:59 +0100 Subject: [PATCH 1/7] Update Roxygen --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index c6686ca..8e0bcbd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -60,6 +60,6 @@ Description: . Also contains functions to simulate the diversity-dependent process. -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 Config/testthat/edition: 3 URL: https://rsetienne.github.io/DDD/ From 4ecc486785ded965495dd574243013427e4e3d48 Mon Sep 17 00:00:00 2001 From: rsetienne Date: Thu, 9 Apr 2026 17:09:00 +0200 Subject: [PATCH 2/7] Add another optimizer to optimizer and extend documentation. --- DESCRIPTION | 1 + R/dd_utils.R | 38 +++++++++++++++++++++++++++++--------- man/optimizer.Rd | 16 ++++++++++------ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8e0bcbd..a5202b9 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Imports: phytools, subplex, DEoptim, + pso, Matrix, expm, SparseM, diff --git a/R/dd_utils.R b/R/dd_utils.R index 3577199..6343539 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -694,15 +694,18 @@ simplex = function(fun, trparsopt, optimpars, ...) invisible(out) } -#' Carries out optimization (finding a minimum) -#' -#' A wrapper to use several optimization routines, currently only 'simplex' (a -#' method adopted from Matlab, or 'subplex', from the R package subplex). The -#' function is called from several packages by the same author. -#' -#' -#' @param optimmethod The method to use for optimization, either 'simplex' or -#' 'subplex' +#' Carries out optimization (finding a maximum) +#' +#' A wrapper to use several optimization routines +#' +#' @param optimmethod The method to use for optimization. There is a choice of: +#' 'simplex', which is a simplex algorithm that shows (when verbose > 0) the +#' optimization trajectory. +#' 'subplex', from the package with the same name +#' 'DEoptim', from the package with the same name +#' 'pso', from the package with the same name +#' 'optim::name_of_algorithm', the algorithm from the optim package (e.g. +#' "Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", or "Brent") #' @param optimpars Parameters of the optimization: 1) relative tolerance in #' function arguments, 2) relative tolerance in function value, 3) absolute #' tolerance in function arguments as well as the function value, 4) @@ -809,6 +812,23 @@ optimizer <- function( fun = fun, ...)) outnew <- list(par = outnew$par, fvalues = -outnew$value, conv = outnew$convergence) + } else if(optimmethod == 'pso') { + minfun4 <- function(trparsopt, fun, ...) + { + return(-fun(trparsopt = trparsopt, ...)) + } + outnew <- suppressWarnings(pso::psoptim(par = trparsopt, + fn = minfun4, + lower = 0, + upper = 1, + control = list(reltol = optimpars[2], + abstol = optimpars[3], + maxit = optimpars[4]), + fun = fun, + ...)) + outnew <- list(par = outnew$par, fvalues = -outnew$value, conv = outnew$convergence) + } else { + stop('The specified optimization method is not supported') } if(cy > 1 & (any(is.na(outnew$par)) | any(is.nan(outnew$par)) | is.na(outnew$fvalues) | is.nan(outnew$fvalues) | outnew$conv != 0)) { diff --git a/man/optimizer.Rd b/man/optimizer.Rd index 0ef3346..eb2be3b 100644 --- a/man/optimizer.Rd +++ b/man/optimizer.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/dd_utils.R \name{optimizer} \alias{optimizer} -\title{Carries out optimization (finding a minimum)} +\title{Carries out optimization (finding a maximum)} \usage{ optimizer( optimmethod = "simplex", @@ -15,8 +15,14 @@ optimizer( ) } \arguments{ -\item{optimmethod}{The method to use for optimization, either 'simplex' or -'subplex'} +\item{optimmethod}{The method to use for optimization. There is a choice of: +'simplex', which is a simplex algorithm that shows (when verbose > 0) the +optimization trajectory. +'subplex', from the package with the same name +'DEoptim', from the package with the same name +'pso', from the package with the same name +'optim::name_of_algorithm', the algorithm from the optim package (e.g. +"Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", or "Brent")} \item{optimpars}{Parameters of the optimization: 1) relative tolerance in function arguments, 2) relative tolerance in function value, 3) absolute @@ -45,9 +51,7 @@ of the optimization routine} optimization converged (\code{conv})}. } \description{ -A wrapper to use several optimization routines, currently only 'simplex' (a -method adopted from Matlab, or 'subplex', from the R package subplex). The -function is called from several packages by the same author. +A wrapper to use several optimization routines } \examples{ From 7ece1de62ec0fe91c0dc8db375dfe2c43542c2ce Mon Sep 17 00:00:00 2001 From: rsetienne Date: Thu, 16 Apr 2026 11:05:59 +0200 Subject: [PATCH 3/7] Print the ML per cycle. --- R/dd_utils.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/dd_utils.R b/R/dd_utils.R index 6343539..9f7a653 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -842,6 +842,7 @@ optimizer <- function( } if(cy > 1) { + cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) if(abs(fvalue[cy] - fvalue[cy - 1]) < optimpars[3]) { if(cy < max_cycles) cat('No more cycles needed.\n') From 76a3cea08e51de385b51c618cde72bb5dab8be91 Mon Sep 17 00:00:00 2001 From: rsetienne Date: Thu, 16 Apr 2026 11:08:12 +0200 Subject: [PATCH 4/7] Also print for cy = 1 --- R/dd_utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/dd_utils.R b/R/dd_utils.R index 9f7a653..d9a8bf4 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -840,9 +840,9 @@ optimizer <- function( trparsopt <- out$par fvalue[cy] <- out$fvalues } + cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) if(cy > 1) { - cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) if(abs(fvalue[cy] - fvalue[cy - 1]) < optimpars[3]) { if(cy < max_cycles) cat('No more cycles needed.\n') From cc5a6c27f081aeeb58e174ef55cbfd37bb64660a Mon Sep 17 00:00:00 2001 From: rsetienne Date: Mon, 20 Apr 2026 12:31:00 +0200 Subject: [PATCH 5/7] Output parameters in optimizer. --- R/dd_utils.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/dd_utils.R b/R/dd_utils.R index d9a8bf4..b75a889 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -841,6 +841,8 @@ optimizer <- function( fvalue[cy] <- out$fvalues } cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) + cat(paste0('The corresponding parameters are: \n')) + cat(untransform_pars(trparsopt)) if(cy > 1) { if(abs(fvalue[cy] - fvalue[cy - 1]) < optimpars[3]) From fde511570d39d8b202c36c4daecd57932d7d57c2 Mon Sep 17 00:00:00 2001 From: rsetienne Date: Mon, 20 Apr 2026 12:34:02 +0200 Subject: [PATCH 6/7] Increase readability --- R/dd_utils.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/dd_utils.R b/R/dd_utils.R index b75a889..74da56f 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -843,6 +843,7 @@ optimizer <- function( cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) cat(paste0('The corresponding parameters are: \n')) cat(untransform_pars(trparsopt)) + cat('\n') if(cy > 1) { if(abs(fvalue[cy] - fvalue[cy - 1]) < optimpars[3]) From f7a8c2818a03d1da3433b21e8c4bf8943a075ecc Mon Sep 17 00:00:00 2001 From: rsetienne Date: Mon, 20 Apr 2026 12:34:02 +0200 Subject: [PATCH 7/7] Increase readability again --- R/dd_utils.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/R/dd_utils.R b/R/dd_utils.R index b75a889..1ff3502 100644 --- a/R/dd_utils.R +++ b/R/dd_utils.R @@ -841,8 +841,9 @@ optimizer <- function( fvalue[cy] <- out$fvalues } cat(paste0('The maximum likelihood is: ', fvalue[cy], '.\n')) - cat(paste0('The corresponding parameters are: \n')) + cat(paste0('The corresponding parameters are: ')) cat(untransform_pars(trparsopt)) + cat('\n') if(cy > 1) { if(abs(fvalue[cy] - fvalue[cy - 1]) < optimpars[3])