From 0e641106d3a9dc58add4499c334cc8e5d30fa7b0 Mon Sep 17 00:00:00 2001 From: Federico Blasi <33967974+blasif@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:35:20 +0000 Subject: [PATCH] 0.1.5 : fix error boundariesV4 when consider a fixed 0 mean --- R/getFunctions.R | 10 ++++++++-- R/optim.R | 21 +++++++++++---------- man/cocoOptim.Rd | 13 +++++++------ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/R/getFunctions.R b/R/getFunctions.R index 29310ee..f863b31 100644 --- a/R/getFunctions.R +++ b/R/getFunctions.R @@ -644,7 +644,11 @@ getBoundariesV4 <- function(coco.object, lower.bound = 2, upper.bound = 2){ mod_DM <- tmp_values$std.covs } - coefs_lm <- stats::coef(stats::lm(term_labels,cbind.data.frame('z' = coco.object@z,mod_DM))) + if(is.formula(coco.object@model.list$mean)){ + coefs_lm <- stats::coef(stats::lm(term_labels,cbind.data.frame('z' = coco.object@z,mod_DM))) + } else{ + coefs_lm <- c() + } boundaries_B <- getBoundariesV2(coco.object = coco.object, mean.limits = c(-Inf, 0, Inf), @@ -655,7 +659,9 @@ getBoundariesV4 <- function(coco.object, lower.bound = 2, upper.bound = 2){ smooth.limits = c(-lower.bound, 0, upper.bound), nugget.limits = c(-lower.bound, 0, upper.bound)) - boundaries_B$theta_init[1:length(coefs_lm)] <- coefs_lm + if(is.formula(coco.object@model.list$mean)){ + boundaries_B$theta_init[1:length(coefs_lm)] <- coefs_lm + } first_sd <- which(names(boundaries_B$theta_init) == "std.dev.limits")[1] n_var <- length(which(names(boundaries_B$theta_init) == "std.dev.limits")) - 1 diff --git a/R/optim.R b/R/optim.R index ebff647..d720cee 100644 --- a/R/optim.R +++ b/R/optim.R @@ -9,7 +9,7 @@ #' optim.type, optim.control) #' #' @param coco.object (\code{S4}) A \link{coco} object. -#' @param boundaries (\code{list}) If provided, a list containing lower, initial, and upper values for the parameters, as defined by \link{getBoundaries}. If not provided, these values are automatically computed with global lower and upper bounds set to -2 and 2. +#' @param boundaries (\code{list}) If provided, a list containing lower, initial, and upper values for the parameters, as defined by \link{getBoundaries}. If not provided, these values are automatically computed via getBoundariesV4 with global lower and upper bounds set to -3 and 3. #' @param ncores (\code{character} or \code{integer}) The number of threads to use for the optimization. If set to `"auto"`, the number of threads is chosen based on system capabilities or a fraction of the available cores. #' @param optim.type (\code{character}) The optimization approach. Options include: #' @param safe (\code{logical}) If `TRUE`, the function avoids Cholesky decomposition errors due to ill-posed covariance matrices by returning a pre-defined large value. Defaults to `TRUE`. @@ -37,15 +37,16 @@ #' 'smooth' = 3/2, #' 'nugget' = -Inf) #' +#' sample_index <- sample(1:dim(holes[[1]])[1],100) +#' #' coco_object <- coco(type = 'dense', -#' data = holes[[1]][1:100,], -#' locs = as.matrix(holes[[1]][1:100,1:2]), -#' z = holes[[1]][1:100,]$z, +#' data = holes[[1]][sample_index, ], +#' locs = as.matrix(holes[[1]][sample_index,1:2]), +#' z = holes[[1]][sample_index, ]$z, #' model.list = model.list) #' #' optim_coco <- cocoOptim(coco_object, -#' boundaries = getBoundaries(coco_object, -#' lower.value = -3, 3)) +#' boundaries = getBoundaries(coco_object) #' #' plotOptimInfo(optim_coco) #' @@ -91,10 +92,10 @@ cocoOptim <- function(coco.object, boundaries = list(), # Check boundaries if(length(boundaries) == 0){ - boundaries <- cocons::getBoundaries( - x = coco.object, - lower.value = -2, - upper.value = 2) + boundaries <- cocons::getBoundariesV4( + coco.object = coco.object, + lower.bound = -3, + upper.bound = 3) } else{ .cocons.check.boundaries(coco.object, boundaries) } diff --git a/man/cocoOptim.Rd b/man/cocoOptim.Rd index aaf12bf..89fe87f 100644 --- a/man/cocoOptim.Rd +++ b/man/cocoOptim.Rd @@ -10,7 +10,7 @@ optim.type, optim.control) \arguments{ \item{coco.object}{(\code{S4}) A \link{coco} object.} -\item{boundaries}{(\code{list}) If provided, a list containing lower, initial, and upper values for the parameters, as defined by \link{getBoundaries}. If not provided, these values are automatically computed with global lower and upper bounds set to -2 and 2.} +\item{boundaries}{(\code{list}) If provided, a list containing lower, initial, and upper values for the parameters, as defined by \link{getBoundaries}. If not provided, these values are automatically computed via getBoundariesV4 with global lower and upper bounds set to -3 and 3.} \item{ncores}{(\code{character} or \code{integer}) The number of threads to use for the optimization. If set to \code{"auto"}, the number of threads is chosen based on system capabilities or a fraction of the available cores.} @@ -40,15 +40,16 @@ model.list <- list('mean' = 0, 'smooth' = 3/2, 'nugget' = -Inf) +sample_index <- sample(1:dim(holes[[1]])[1],100) + coco_object <- coco(type = 'dense', - data = holes[[1]][1:100,], - locs = as.matrix(holes[[1]][1:100,1:2]), - z = holes[[1]][1:100,]$z, + data = holes[[1]][sample_index, ], + locs = as.matrix(holes[[1]][sample_index,1:2]), + z = holes[[1]][sample_index, ]$z, model.list = model.list) optim_coco <- cocoOptim(coco_object, -boundaries = getBoundaries(coco_object, -lower.value = -3, 3)) +boundaries = getBoundaries(coco_object) plotOptimInfo(optim_coco)