|
75 | 75 | #' @export |
76 | 76 | g3_iterative <- function(gd, wgts = 'WGTS', |
77 | 77 | model, params.in, |
78 | | - grouping = list(), |
| 78 | + grouping = g3_iterative_default_grouping(params.in), |
79 | 79 | use_parscale = TRUE, |
80 | 80 | method = 'BFGS', |
81 | 81 | control = list(), |
@@ -374,6 +374,54 @@ g3_iterative <- function(gd, wgts = 'WGTS', |
374 | 374 | return(params_final) |
375 | 375 | } |
376 | 376 |
|
| 377 | +# Generate default grouping, combine all fleet likelihoods into one group |
| 378 | +# NB: For this to work, nll_names need to be in the form (nll_source)_(nll_dist), where (nll_dist) matches one of the (nll_dist_names) |
| 379 | +#' @param params.in Initial parameters to use with the model |
| 380 | +#' @param nll_dist_names Character vector of postfixes to consider when looking for groupings |
| 381 | +#' @return |
| 382 | +#' \subsection{g3_iterative_default_grouping}{ |
| 383 | +#' A list of component groups to component names, as required by the \var{grouping} parameter |
| 384 | +#' } |
| 385 | +#' @details |
| 386 | +#' \subsection{g3_iterative_default_grouping}{ |
| 387 | +#' This assumes that your likelihood component names are of the form ``(nll_group)_(nll_dist)``, |
| 388 | +#' where ``(nll_dist)`` matches one of the regexes in \var{nll_dist_names}. |
| 389 | +#' For example, ``afleet_ldist``, ``afleet_aldist``, ``bfleet_ldist``. ``afleet`` & ``bfleet`` will be the groups used. |
| 390 | +#' } |
| 391 | +#' @rdname g3_iterative |
| 392 | +#' @export |
| 393 | +g3_iterative_default_grouping <- function (params.in, nll_dist_names = c("ldist", "aldist", "matp", "sexdist", "SI", "len\\d+SI")) { |
| 394 | + # Extract all likelihood component weight names from params.in |
| 395 | + weight_re <- paste0( |
| 396 | + "^", |
| 397 | + "(?<dist>.dist|.sparse)_", |
| 398 | + "(?<function>surveyindices_log|[a-z]+)_", |
| 399 | + "(?<nll_source>.+)_", |
| 400 | + "(?<nll_dist>", paste0(nll_dist_names, collapse = "|"), ")_", |
| 401 | + "weight$" |
| 402 | + ) |
| 403 | + |
| 404 | + # Break up names into a data.frame of param_name -> regex groups |
| 405 | + weight_names <- grep(weight_re, rownames(params.in), value = TRUE, perl = TRUE) |
| 406 | + weight_parts <- as.data.frame(do.call(rbind, regmatches(weight_names, regexec(weight_re, weight_names, perl = TRUE)))) |
| 407 | + names(weight_parts)[[1]] <- "param_name" |
| 408 | + weight_parts$value <- params.in[weight_parts$param_name, "value"] |
| 409 | + |
| 410 | + # Remove any zero-weighted parameters |
| 411 | + zero_value <- weight_parts[weight_parts$value == 0, "param_name"] |
| 412 | + if (length(zero_value) > 0) { |
| 413 | + warning("Parameters ", paste(zero_value, collapse = ", ") , " have a value of 0, removing from grouping") |
| 414 | + weight_parts <- weight_parts[weight_parts$value > 0,] |
| 415 | + } |
| 416 | + |
| 417 | + # Group rows together into a list of nll_source -> vector of (nll_source)_(nll_dist) |
| 418 | + sapply( |
| 419 | + unique(weight_parts$nll_source), |
| 420 | + function (nll_source) paste0(nll_source, "_", weight_parts[weight_parts$nll_source == nll_source, "nll_dist"]), |
| 421 | + simplify = FALSE |
| 422 | + ) |
| 423 | +} |
| 424 | + |
377 | 425 | #' @title Initial parameters for iterative re-weighting |
378 | 426 | #' @param lik_out A likelihood summary dataframe. The output of g3_lik_out(model, param) |
379 | 427 | #' @param grouping A list describing how to group likelihood components for iterative re-weighting |
|
0 commit comments