Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
`fill_missing_ids = TRUE` and `group_by` leave combinations missing.
- `constraint = "total"` no longer adds an index to the user's
`land_use_data`.
- `constraint = "total"` and `"singly"` now treat each `group_by`
combination and each decay value as an independent system. Previously,
`"total"` split the regional total across groups and both blended
several decay values into one result. Detailed results now keep the
`group_by` and `decay_function_arg` columns (#66, @soukhova).
- `cost_to_closest()` no longer returns `Inf` due to integer overflow with
large opportunity counts.
- `cumulative_cutoff()`: cutoffs with floating point noise (e.g. from
Expand Down
15 changes: 14 additions & 1 deletion R/constrained_accessibility.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@
#' This function covers the family of constrained accessibility measures
#' proposed in \insertCite{soukhov2025family;textual}{accessibility}.
#'
#' For these measures, each group or decay value is
#' treated as an independent land-use-transportation system. This is not a multimodal
#' model that splits demand or supply across groups. Instead, each group is
#' calculated as if the function were run separately on that group's rows of
#' `travel_matrix`. The total allocated within a group is the sum of the
#' opportunities (or population) associated with the ids that appear in that
#' group's rows.
#'
#' As well, when `fill_missing_ids = TRUE`, ids that are absent from a particular
#' group's rows are still returned with accessibility equal to 0.
#'
#' ## Total Constrained Accessibility
#'
#' Allocates the system-wide total proportionally based on travel impedance
Expand Down Expand Up @@ -199,7 +210,9 @@
#' and `flow`, holding the demand of each origin followed by the supply of
#' each destination (so an id can appear twice).
#'
#' Detailed results don't include the `group_by` columns.
#' For `constraint = "total"` and `"singly"`, detailed results include the
#' `group_by` columns and, when the decay function has several values,
#' `decay_function_arg`.
#'
#' @references
#' \insertAllCited{}
Expand Down
45 changes: 31 additions & 14 deletions R/singly_constrained.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ singly_constrained <- function(travel_matrix,

# Apply decay
data <- apply_gravity_measure(data, decay_function, travel_cost)
if ("decay_function_arg" %in% names(data)) {
group_by <- c(group_by, "decay_function_arg")
}

detail_cols <- c(
"from_id",
"to_id",
group_by
)

if (active) {
# Supply-constrained (returns units of supply).. V_ij = (O_i f(c_ij) / sum_i O_i f(c_ij)) * D_j
Expand All @@ -73,13 +82,17 @@ singly_constrained <- function(travel_matrix,
data[, singly_access := kappa_singly * get(supply)]

if (detailed_results) {
access <- data[, .(
from_id,
to_id,
kappa_singly,
supply = singly_access,
B_j = data.table::fifelse(denom_j > 0, 1 / denom_j, 0)
)]
access <- data[, c(
detail_cols,
"kappa_singly",
"singly_access",
"denom_j"
), with = FALSE]

data.table::setnames(access, "singly_access", "supply")

access[, B_j := data.table::fifelse(denom_j > 0, 1 / denom_j, 0)]
access[, denom_j := NULL]
} else {
access <- data[, .(supply = sum(singly_access)), by = c("from_id", group_by)]
if (fill_missing_ids) {
Expand All @@ -100,13 +113,17 @@ singly_constrained <- function(travel_matrix,
data[, singly_access := hatkappa_singly * get(demand)]

if (detailed_results) {
access <- data[, .(
from_id,
to_id,
hatkappa_singly,
demand = singly_access,
A_i = data.table::fifelse(denom_i > 0, 1 / denom_i, 0)
)]
access <- data[, c(
detail_cols,
"hatkappa_singly",
"singly_access",
"denom_i"
), with = FALSE]

data.table::setnames(access, "singly_access", "demand")

access[, A_i := data.table::fifelse(denom_i > 0, 1 / denom_i, 0)]
access[, denom_i := NULL]
} else {
access <- data[, .(demand = sum(singly_access)), by = c("to_id", group_by)]
if (fill_missing_ids) {
Expand Down
78 changes: 49 additions & 29 deletions R/total_constrained.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ utils::globalVariables(c(
# common ids / columns
"from_id", "to_id", "id", "opp_weight", "supply", "demand", "weighted_demand", "weighted_supply",
# total-constrained
"kappa_total", "hatkappa_total", "K_total", "hatK_total", "constrained_opportunity",
"kappa_total", "hatkappa_total", "K_total", "hatK_total", "constrained_opportunity", "total_weighted_system",
"total_opportunities_region",
# singly-constrained
"denom_i", "denom_j", "kappa_singly", "hatkappa_singly", "A_i", "B_j", "singly_access",
# doubly-constrained
Expand Down Expand Up @@ -51,11 +52,8 @@ total_constrained <- function(travel_matrix,
)
}
merge_id <- "to_id"
group_id <- "from_id"
weighted_col <- "weighted_supply"
kappa_col <- "kappa_total"
total_col <- "K_total"
result_col <- "supply"
opportunity_col <- supply
} else {
#passive accessibility
Expand All @@ -67,11 +65,8 @@ total_constrained <- function(travel_matrix,
)
}
merge_id <- "from_id"
group_id <- "to_id"
weighted_col <- "weighted_demand"
kappa_col <- "hatkappa_total"
total_col <- "hatK_total"
result_col <- "demand"
opportunity_col <- demand
}

Expand Down Expand Up @@ -103,40 +98,64 @@ total_constrained <- function(travel_matrix,

# Apply decay
data <- apply_gravity_measure(data, decay_function, travel_cost)
if ("decay_function_arg" %in% names(data)) {
group_by <- c(group_by, "decay_function_arg")
}

# Core calculation
data[, (weighted_col) := get(opportunity_col) * opp_weight]
total_weighted_system <- data[, sum(get(weighted_col))]
data[, total_weighted_system := sum(get(weighted_col)), by = group_by]
data[, (kappa_col) := get(weighted_col) / total_weighted_system]

ids_for_total <- unique(data[[merge_id]]) # e.g., A,B,C when active=TRUE
total_opportunities_region <- sum(
land_use_data[[opportunity_col]][match(ids_for_total, land_use_data$id)],
na.rm = TRUE
)
data[
,
total_opportunities_region := sum(get(opportunity_col)[!duplicated(get(merge_id))]),
by = group_by
]

data[, constrained_opportunity := get(kappa_col) * total_opportunities_region]

detail_cols <- c(
"from_id",
"to_id",
group_by
)

if (detailed_results) {
if (!active) {
access <- data[, .(
from_id,
to_id,
weighted_demand,
hatkappa_total,
demand = constrained_opportunity,
hatK_total = total_opportunities_region / total_weighted_system
)]
access <- data[, c(
detail_cols,
"weighted_demand",
"hatkappa_total",
"constrained_opportunity",
"total_weighted_system",
"total_opportunities_region"
), with = FALSE]

data.table::setnames(access, "constrained_opportunity", "demand")

access[, hatK_total := total_opportunities_region / total_weighted_system]
access[, total_weighted_system := NULL]
access[, total_opportunities_region := NULL]

} else {
# supply-side details (active accessibility)
access <- data[, .(
from_id,
to_id,
weighted_supply,
kappa_total,
supply = constrained_opportunity,
K_total = total_opportunities_region / total_weighted_system
)]
access <- data[, c(
detail_cols,
"weighted_supply",
"kappa_total",
"constrained_opportunity",
"total_weighted_system",
"total_opportunities_region"
), with = FALSE]

data.table::setnames(access, "constrained_opportunity", "supply")

access[, K_total := total_opportunities_region / total_weighted_system]
access[, total_weighted_system := NULL]
access[, total_opportunities_region := NULL]


}
} else {
if (active) {
Expand All @@ -150,6 +169,7 @@ total_constrained <- function(travel_matrix,
access_col = "supply"
)
}

} else {
# demand aggregated by destination (to_id)
access <- data[, .(demand = sum(constrained_opportunity)), by = c("to_id", group_by)]
Expand Down
6 changes: 5 additions & 1 deletion man/balancing_cost.Rd

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

21 changes: 19 additions & 2 deletions man/constrained_accessibility.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_to_closest.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/cumulative_cutoff.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/cumulative_interval.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/floating_catchment_area.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/gravity.Rd

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

4 changes: 4 additions & 0 deletions man/roxygen/templates/group_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
#' departure time column, that specifies the departure time of each entry in
#' the data frame, passing `"departure_time"` to this parameter results in
#' accessibility estimates grouped by origin and by departure time.
#'
#' Accessibility is calculated separately for each unique combination of
#' grouping variables. For constrained accessibility measures, each group is
#' treated as an independent land-use-transportation system.
6 changes: 5 additions & 1 deletion man/spatial_availability.Rd

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

Loading
Loading