metrumresearchgroup / bbr

R interface for model and project management
https://metrumresearchgroup.github.io/bbr/
Other
21 stars 2 forks source link

dOFV and AIC columns for summary_log() #677

Open seth127 opened 2 months ago

seth127 commented 2 months ago

We would like to add several columns to the summary_log() output.

AIC

This is a straightforward calculation. Consider if there are any edge cases to catch.

Example code

add_aic <- function(.runlog) {
  ## check summary log appended
  if(any(str_detect(names(.runlog), "param_count")) & 
     any(str_detect(names(.runlog), "ofv"))) {
    return(mutate(.runlog, aic=2*param_count + ofv))
  }
  ## if not, return unchanged
  warning("AIC could not be computed")
  return(.runlog)
}

dOFV

The change in OFV from the parent model. Several considerations:

Example code

## compare ofv to that of the model in based_on
## if multiple models are in based_on, use the first
add_dofv <- function(.runlog) {
  .runlog %>%
    mutate(based_on_join = purrr::map_chr(based_on, function(x) {ifelse(length(x) > 0, x[1], NA)})) %>% 
    left_join(
      select(.runlog, based_on_join=run, based_on_ofv=ofv), 
      by="based_on_join"
    ) %>% 
    mutate(dofv = as.numeric(format(ofv - based_on_ofv, nsmall = 2))) %>% 
    select(-based_on_ofv, -based_on_join)
}

dAIC

If we're adding the above, should we also calculate the difference in AIC?

seth127 commented 2 months ago

Consider also adding a method for easily comparing two model objects (as opposed to a log tibble), similar to the discussion in https://github.com/metrumresearchgroup/bbr/issues/401