diff --git a/R/cost_likelihood_biomee.R b/R/cost_likelihood_biomee.R index cd7ae5e0..9b22727a 100644 --- a/R/cost_likelihood_biomee.R +++ b/R/cost_likelihood_biomee.R @@ -16,6 +16,9 @@ #' @param targets A character vector indicating the target variables for which the #' optimization will be done. This should be a subset of \code{c("GPP", "LAI", #' "Density", "Biomass")}. +#' @param verbose A logical specifying whether to print parameter and log-likelihood +#' values during each iteration of the calibration as message. Defaults to +#' \code{FALSE} #' #' @return The log-likelihood of the simulated #' targets by the biomee model versus the observed targets. @@ -46,7 +49,8 @@ cost_likelihood_biomee <- function( par, obs, drivers, - targets + targets, + verbose = FALSE ){ # predefine variables for CRAN check compliance @@ -108,6 +112,18 @@ cost_likelihood_biomee <- function( if(is.nan(ll) || is.na(ll) | ll == 0){ ll <- -Inf } + + if (verbose){ + message( + paste("Par = ", paste(drivers$params_species, collapse = ", ")) + ) + message( + paste("Log-likelihood = ", ll) + ) + message( + paste("--------------------------") + ) + } return(ll) } diff --git a/R/cost_likelihood_pmodel.R b/R/cost_likelihood_pmodel.R index 5aac9304..f483f5bd 100644 --- a/R/cost_likelihood_pmodel.R +++ b/R/cost_likelihood_pmodel.R @@ -28,6 +28,9 @@ #' \code{FALSE}. #' @param ncores An integer specifying the number of cores used for parallel #' computing. Defaults to 2. +#' @param verbose A logical specifying whether to print parameter and log-likelihood +#' values during each iteration of the calibration as message. Defaults to +#' \code{FALSE} #' #' @return The log-likelihood of the observed target values, assuming that they #' are independent, normally distributed and centered on the predictions @@ -78,7 +81,8 @@ cost_likelihood_pmodel <- function( targets, par_fixed = NULL, # non-calibrated model parameters parallel = FALSE, - ncores = 2 + ncores = 2, + verbose = FALSE ){ # predefine variables for CRAN check compliance sitename <- data <- gpp_mod <- NULL @@ -215,6 +219,18 @@ cost_likelihood_pmodel <- function( # trap boundary conditions if(is.nan(ll) | is.na(ll) | ll == 0){ll <- -Inf} + + if (verbose){ + message( + paste("Par = ", paste(params_modl, collapse = ", ")) + ) + message( + paste("Log-likelihood = ", ll) + ) + message( + paste("--------------------------") + ) + } return(ll) } diff --git a/R/cost_rmse_biomee.R b/R/cost_rmse_biomee.R index e68e38f5..34af654e 100644 --- a/R/cost_rmse_biomee.R +++ b/R/cost_rmse_biomee.R @@ -12,6 +12,9 @@ #' @param obs A nested data frame of observations, following the structure of \code{biomee_validation}, #' for example. #' @param drivers A nested data frame of driver data, for example \code{biomee_gs_leuning_drivers}. +#' @param verbose A logical specifying whether to print parameter and cost (RMSE) +#' values during each iteration of the calibration as message. Defaults to +#' \code{FALSE} #' #' @return The root mean squared error (RMSE) between the observed and simulated #' values of \code{'GPP','LAI','Density'} and \code{'Biomass'} (all variables @@ -38,7 +41,8 @@ cost_rmse_biomee <- function( par, obs, - drivers + drivers, + verbose = FALSE ){ # predefine variables for CRAN check compliance @@ -85,5 +89,17 @@ cost_rmse_biomee <- function( ## Calculate cost (RMSE) across the N targets cost <- mean(dff$error_rel^2, na.rm = TRUE) + if (verbose){ + message( + paste("Par = ", paste(drivers$params_species, collapse = ", ")) + ) + message( + paste("RMSE = ", cost) + ) + message( + paste("--------------------------") + ) + } + return(cost) } \ No newline at end of file diff --git a/R/cost_rmse_pmodel.R b/R/cost_rmse_pmodel.R index 7236bfb0..74652c53 100644 --- a/R/cost_rmse_pmodel.R +++ b/R/cost_rmse_pmodel.R @@ -27,6 +27,9 @@ #' \code{FALSE}. #' @param ncores An integer specifying the number of cores used for parallel #' computing. Defaults to 2. +#' @param verbose A logical specifying whether to print parameter and cost (RMSE) +#' values during each iteration of the calibration as message. Defaults to +#' \code{FALSE} #' #' @return The root mean squared error (RMSE) between observed values and P-model #' predictions. The RMSE is computed for each target separately and then aggregated @@ -75,7 +78,8 @@ cost_rmse_pmodel <- function( target_weights = NULL, # if using several targets, how are the individual # RMSE weighted? named vector parallel = FALSE, - ncores = 2 + ncores = 2, + verbose = FALSE ){ # predefine variables for CRAN check compliance @@ -204,6 +208,18 @@ cost_rmse_pmodel <- function( }else{ cost <- mean(rmse, na.rm = TRUE) } + + if (verbose){ + message( + paste("Par = ", paste(params_modl, collapse = ", ")) + ) + message( + paste("RMSE = ", cost) + ) + message( + paste("--------------------------") + ) + } return(cost) } diff --git a/man/cost_likelihood_biomee.Rd b/man/cost_likelihood_biomee.Rd index 2ba354ba..c70c782e 100644 --- a/man/cost_likelihood_biomee.Rd +++ b/man/cost_likelihood_biomee.Rd @@ -4,7 +4,7 @@ \alias{cost_likelihood_biomee} \title{Log-likelihood cost function for BiomeE with different targets} \usage{ -cost_likelihood_biomee(par, obs, drivers, targets) +cost_likelihood_biomee(par, obs, drivers, targets, verbose = FALSE) } \arguments{ \item{par}{A vector containing parameter values for \code{'phiRL', @@ -22,6 +22,10 @@ for example.} \item{targets}{A character vector indicating the target variables for which the optimization will be done. This should be a subset of \code{c("GPP", "LAI", "Density", "Biomass")}.} + +\item{verbose}{A logical specifying whether to print parameter and log-likelihood +values during each iteration of the calibration as message. Defaults to +\code{FALSE}} } \value{ The log-likelihood of the simulated diff --git a/man/cost_likelihood_pmodel.Rd b/man/cost_likelihood_pmodel.Rd index d206d812..7a3a9ab5 100644 --- a/man/cost_likelihood_pmodel.Rd +++ b/man/cost_likelihood_pmodel.Rd @@ -12,7 +12,8 @@ cost_likelihood_pmodel( targets, par_fixed = NULL, parallel = FALSE, - ncores = 2 + ncores = 2, + verbose = FALSE ) } \arguments{ @@ -44,6 +45,10 @@ parameters are passed on to \code{\link{runread_pmodel_f}}.} \item{ncores}{An integer specifying the number of cores used for parallel computing. Defaults to 2.} + +\item{verbose}{A logical specifying whether to print parameter and log-likelihood +values during each iteration of the calibration as message. Defaults to +\code{FALSE}} } \value{ The log-likelihood of the observed target values, assuming that they diff --git a/man/cost_rmse_biomee.Rd b/man/cost_rmse_biomee.Rd index fffe40b7..0c6b10b0 100644 --- a/man/cost_rmse_biomee.Rd +++ b/man/cost_rmse_biomee.Rd @@ -4,7 +4,7 @@ \alias{cost_rmse_biomee} \title{RMSE cost function for BiomeE} \usage{ -cost_rmse_biomee(par, obs, drivers) +cost_rmse_biomee(par, obs, drivers, verbose = FALSE) } \arguments{ \item{par}{A vector containing parameter values for \code{'phiRL', @@ -14,6 +14,10 @@ cost_rmse_biomee(par, obs, drivers) for example.} \item{drivers}{A nested data frame of driver data, for example \code{biomee_gs_leuning_drivers}.} + +\item{verbose}{A logical specifying whether to print parameter and cost (RMSE) +values during each iteration of the calibration as message. Defaults to +\code{FALSE}} } \value{ The root mean squared error (RMSE) between the observed and simulated diff --git a/man/cost_rmse_pmodel.Rd b/man/cost_rmse_pmodel.Rd index ca897fa8..8ee06c69 100644 --- a/man/cost_rmse_pmodel.Rd +++ b/man/cost_rmse_pmodel.Rd @@ -12,7 +12,8 @@ cost_rmse_pmodel( par_fixed = NULL, target_weights = NULL, parallel = FALSE, - ncores = 2 + ncores = 2, + verbose = FALSE ) } \arguments{ @@ -46,6 +47,10 @@ weights are used to compute a weighted average of RMSE across targets.} \item{ncores}{An integer specifying the number of cores used for parallel computing. Defaults to 2.} + +\item{verbose}{A logical specifying whether to print parameter and cost (RMSE) +values during each iteration of the calibration as message. Defaults to +\code{FALSE}} } \value{ The root mean squared error (RMSE) between observed values and P-model diff --git a/vignettes/new_cost_function.Rmd b/vignettes/new_cost_function.Rmd index 137bd123..777264fa 100644 --- a/vignettes/new_cost_function.Rmd +++ b/vignettes/new_cost_function.Rmd @@ -61,7 +61,8 @@ pars_calib_rmse <- calib_sofun( tau_acclim = 30.0, kc_jmax = 0.41 ), - targets = "gpp" # define target variable GPP + targets = "gpp", # define target variable GPP + verbose = FALSE # set to TRUE to print parameter and cost values during each iteration of the calibration ) ```