Closed juanfung closed 2 weeks 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...
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])
}
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.
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.
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')
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)
}
Completed per above commits
Updating input schema to include:
Need to update:
preprocess_model
function, which assumes a different schemaNeed 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:
preprocess_model
creates nested submodels for analyses; currently submodels are separated bynum_stories
but can also nest that withinsystem
: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)