Skip to content

Commit

Permalink
added verbose option for calibration
Browse files Browse the repository at this point in the history
  • Loading branch information
stineb committed Aug 8, 2024
1 parent 660c6f2 commit 31e69d3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 9 deletions.
18 changes: 17 additions & 1 deletion R/cost_likelihood_biomee.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -46,7 +49,8 @@ cost_likelihood_biomee <- function(
par,
obs,
drivers,
targets
targets,
verbose = FALSE
){

# predefine variables for CRAN check compliance
Expand Down Expand Up @@ -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)
}
18 changes: 17 additions & 1 deletion R/cost_likelihood_pmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
18 changes: 17 additions & 1 deletion R/cost_rmse_biomee.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +41,8 @@
cost_rmse_biomee <- function(
par,
obs,
drivers
drivers,
verbose = FALSE
){

# predefine variables for CRAN check compliance
Expand Down Expand Up @@ -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)
}
18 changes: 17 additions & 1 deletion R/cost_rmse_pmodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion man/cost_likelihood_biomee.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/cost_likelihood_pmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion man/cost_rmse_biomee.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/cost_rmse_pmodel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vignettes/new_cost_function.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
```

Expand Down

0 comments on commit 31e69d3

Please sign in to comment.