juanfung / frbca

Other
0 stars 0 forks source link

Update input table formats for more explicit model information #16

Closed juanfung closed 2 weeks ago

juanfung commented 5 months ago

Updating input schema to include:

Need to update:

Need to decide if analyses will continue to be run for a fixed system (the current implicit assumption) or whether to add something to preprocessing to allow a single analysis of multiple systems:

## OLD:
    stories <- dat |> dplyr::distinct(num_stories) |> pull()
    for (i in 1:length(stories)) {
        models[[i]] <- dat |>
            dplyr::filter(num_stories == stories[i])
    }

## NEW:
    systems <- dat |> dplyr::distinct(system) |> pull()
    stories <- dat |> dplyr::distinct(num_stories) |> pull()
    for (j in 1:length(systems)) {
        for (i in 1:length(stories)) {
            models[[j]][[i]] <- dat |>
                dplyr::filter(system == systems[j] & num_stories == stories[i])
        }
    }

Will need to check if any of the remaining functions assume a single-level submodel structure, in which case they will need to be modified to iterate through all levels, or whether they work as is (unlikely)

juanfung commented 5 months ago

Upon inspection of wrapper function for analysis, the following may be all that is needed:

## OLD:
  for (i in 1:length(models)) {
    models[[i]] <- bca(models[[i]], params)
  }

## NEW:
  for (j in 1:length(models)) {
    for (i in 1:length(models[[j]])) {
      models[[j]][[i]] <- bca(models[[j]][[i]], params)
  }

This can be simplified if preprocess_models passes on lengths of systems and models in which case a single loop is needed that goes through the nested list for each {system, story} combo:

## ASSUMING `len_systems` and `len_stories` are passed on
    for (j in 1:len_systems) {
        for (i in 1:len_stories) {
            models[[j]][[i]] <- bca(models[[j]][[i]], params

But! note that len_stories will actually vary depending on the system...

juanfung commented 5 months ago

This does raise the question if it would be better to have preprocess_model explicitly name the systems and stories in the list of models:

## PROPOSAL:
      for (j in 1:length(systems)) {
        for (i in 1:length(stories)) {
            models[[systems[j]]][[stories[i]]] <- dat |>
                dplyr::filter(system == systems[j] & num_stories == stories[i])
        }
juanfung commented 4 months ago

Since cost estimates have been updated to include more than just structural and nonstructural, pv_cost should be updated too:

## OLD:
pv_cost <- function(model, params) {
    p = params$parameters$base
    return(
        model |>
        ## filter out missing NS cost
        ## preprocess_cost() |>
        dplyr::mutate(
                   pv_s=c_s,
                   pv_ns=c_ns*(1 + 1/(1-p$delta)^(p$T/2))) |>
         dplyr::mutate(pv_total=pv_s+pv_ns)
    )
}

## NEW:
pv_cost <- function(model, params) {
    p = params$parameters$base
    return(
        model |>
        ## filter out missing NS cost
        ## preprocess_cost() |>
        dplyr::mutate(
                   pv_ns=c_ns*(1 + 1/(1-p$delta)^(p$T/2))) |>
         dplyr::mutate(pv_total=pv_s+pv_ns+pv_residual)
    )
}

in other words, since we only need PV calculation for nonstructural costs, we precompute pv_residual as Total cost - nonstructural cost and then add PV(nonstructural) to that residual.

juanfung commented 4 months ago

Also need to double check naming conventions for costs:

## OLD:
preprocess_cost <- function(model) {
    ## Purpose:
    ## ignore rows without nonstructural costs
    return(model |> dplyr::filter(!is.na(c_ns)))
}

Current function assumes two columns: c_s, c_ns, and drops empty columns. This is in case we are considering a nonstructural intervention for which we don't have costs and may be unnecessary going forward.

juanfung commented 4 months ago

Another object to be added: rather than recreating join_cols across multiple functions (eg, preprocess_model, pv_benefit), create an object (may need to create an environment).

join_cols = c('system', 'model', 'num_stories', 'intervention', 'design_s', 'design_ns')
juanfung commented 4 months ago

Going back to the new nested list of inputs under preprocess_model, the other function that needs updating is the wrapper frbca:

## OLD:
frbca <- function(eal, cost, params) {
  models <- preprocess_model(eal, cost, params[['parameters']][['base']])
  for (i in 1:length(models)) {
    models[[i]] <- bca(models[[i]], params)
  }
  models <- dplyr::bind_rows(models)
  return(models)
}

## NEW:
frbca <- function(eal, cost, params) {
  models <- preprocess_model(eal, cost, params[['parameters']][['base']])
  for (i in 1:length(models)) {
    for (j in 1:length(models[[i]]) {
        models[[i]][[j]] <- bca(models[[i]][[j]], params)
    }
  }
  models <- dplyr::bind_rows(models)
  return(models)
}
juanfung commented 2 weeks ago

Completed per above commits