Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessibilty() always returns an integer #235

Closed
c-voulgaris opened this issue Mar 7, 2022 · 4 comments
Closed

accessibilty() always returns an integer #235

c-voulgaris opened this issue Mar 7, 2022 · 4 comments
Assignees
Labels
v1.0 Features to be included in r5r 1.0

Comments

@c-voulgaris
Copy link

Brief description of the problem:

I love the ability to specify a non-step decay function in the accessibility function, but we lose some of the benefit when the resulting accessibility values are apparently rounded to the integer.

In the example below, I think I've specified a decay function that should decrease linearly from 1 to zero as travel time increases from 0 to 20 minutes. Since opportunities are never fully-weighted in this case, it seems like the accessibility values should vary continuously and return very few, if any, whole numbers. But with the rounding that's apparently happening, the results are mostly the same as what I'd get with a step function with a cutoff at 10 minutes.

I realize this is almost definitely an r5/Conveyal issue rather than an r5r issue, so maybe there's nothing to be done (other than maybe calculating travel times with r5, but applying the decay function outside of r5).

Reproducible example here

library(r5r)
library(dplyr)

# build transport network
data_path <- system.file("extdata/poa", package = "r5r")
r5r_core <- setup_r5(data_path = data_path, temp_dir = TRUE, verbose = FALSE)

# load origin/destination points
points <- read.csv(file.path(data_path, "poa_hexgrid.csv"))

access_linear_decay <- accessibility(r5r_core, 
                        origins = points, 
                        destinations = points,
                        opportunities_colname = "schools",
                        decay_function = "linear",
                        cutoffs = 10,
                        decay_value = 20, 
                        verbose=FALSE) %>%
  rename(linear_decay = accessibility)

access_step <- accessibility(r5r_core, 
                        origins = points, 
                        destinations = points,
                        opportunities_colname = "schools",
                        decay_function = "step",
                        cutoffs = 10, 
                        verbose=FALSE) %>%
  rename(step_decay = accessibility)

comparison <- full_join(access_linear_decay, access_step) %>%
  mutate(diff = step_decay - linear_decay)

head(comparison, 20)

table(comparison$diff)
@mvpsaraiva
Copy link
Collaborator

mvpsaraiva commented Mar 15, 2022

Hi @c-voulgaris. Sorry for the late reply.

Thanks for pointing this out. Indeed, r5 does all calculations using real numbers, but converts the results to integers before passing them along to r5r. I guess the rationale from Conveyal is that, in practice, you can't access a fraction of a school or job. I'll open an issue in r5's repository to check if they can change this behaviour in future versions.

Meanwhile, you'll need to work around this by applying the decay function outside r5r, as you suggested. Unfortunately, this is much more resource intensive, as you need to load the full travel time matrix in memory. Another 'hackish' solution that would allow you to use the accessibility() function is to multiply all opportunity counts of the inputs by 1000, for example, and then divide the accessibility results by 1000. This should produce accessibility results with precision of up to 3 decimal places.

mvpsaraiva added a commit that referenced this issue May 10, 2022
…erent decay functions, fixing issue #235

Obs.: managed to access floating point accessibility values via reflection.
@mvpsaraiva mvpsaraiva added the v1.0 Features to be included in r5r 1.0 label May 10, 2022
@mvpsaraiva
Copy link
Collaborator

I've found a solution for this issue, which is implemented now in the dev branch. It will be on CRAN when version 1.0 is released.

@rafapereirabr
Copy link
Member

Thank you @mvpsaraiva .

@c-voulgaris , the accessibility estimates from accessibility() are now of returned as doubles / class numeric, except when using a step decay function. This is already available in the dev version and will be included in the next release of r5r on CRAN. Closing this issue for now.

@c-voulgaris
Copy link
Author

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v1.0 Features to be included in r5r 1.0
Projects
None yet
Development

No branches or pull requests

4 participants