njtierney / maxcovr

Tools in R to make it easier to solve the Maximal Coverage Location Problem
http://maxcovr.njtierney.com/
GNU General Public License v3.0
42 stars 11 forks source link

include ability to add dist matrices for #77 #88

Closed mpadge closed 4 years ago

mpadge commented 4 years ago

Description

Adds 2 more parameters to the max_coverage function:

  1. d_existing_user; and
  2. d_proposed_user

These are explained in updated man entries

Example

Here's a reprex:

setwd ("/data/mega/code/forks/maxcovr")
devtools::load_all (".", export_all = TRUE)
#> Loading maxcovr
devtools::load_all ("../../repos/atfutures/dodgr", export_all = FALSE)
#> Loading dodgr
suppressMessages (library (dplyr))
dodgr_cache_off ()
net <- dodgr_streetnet_sc ("york, england") %>%
    weight_streetnet (wt_profile = "foot")
#> Loading required namespace: geodist
v <- dodgr_vertices (net)

existing_facility <- york_selected <- york %>% filter(grade == "I")
proposed_facility <- york_unselected <- york %>% filter(grade != "I")
user <- york_crime 

from <- match_points_to_graph (v, existing_facility [, c ("long", "lat")])
to <- match_points_to_graph (v, user [, c ("long", "lat")])
d_existing <- dodgr_dists (net, from = from, to = to)

from <- match_points_to_graph (v, proposed_facility [, c ("long", "lat")])
d_proposed <- dodgr_dists (net, from = from, to = to)

mc_result <- max_coverage(existing_facility = york_selected,
                          proposed_facility = york_unselected,
                          user = york_crime,
                          distance_cutoff = 100,
                          n_added = 20)
mc_result_dmats <- max_coverage(existing_facility = york_selected,
                                proposed_facility = york_unselected,
                                user = york_crime,
                                distance_cutoff = 100,
                                n_added = 20,
                                d_existing_user = d_existing,
                                d_proposed_user = d_proposed)
summary (mc_result)
#> 
#> ------------------------------------------- 
#> Model Fit: maxcovr fixed location model 
#> ------------------------------------------- 
#> Distance Cutoff: 100m 
#> Facilities: 
#>     Added:       20 
#> Coverage (Previous): 
#>     # Users:     540    (339) 
#>     Proportion:  0.2977 (0.1869) 
#> Distance (m) to Facility (Previous): 
#>     Avg:         886 (1400) 
#>     SD:          986 (1597) 
#> -------------------------------------------
summary (mc_result_dmats)
#> 
#> ------------------------------------------- 
#> Model Fit: maxcovr fixed location model 
#> ------------------------------------------- 
#> Distance Cutoff: 100m 
#> Facilities: 
#>     Added:       20 
#> Coverage (Previous): 
#>     # Users:     490    (339) 
#>     Proportion:  0.2701 (0.1869) 
#> Distance (m) to Facility (Previous): 
#>     Avg:         1092 (1400) 
#>     SD:          1193 (1597) 
#> -------------------------------------------

Created on 2019-11-08 by the reprex package (v0.3.0)

All measures are reduced, simply because 100m of street distance is equivalent to using a lower value of dist_threshold in the first model. Alternatively, just put in dist_threshold = 125 in the model with the distance matrices and you'll get the same number of users covered (540). Distances to facilities nevertheless remain greater, again coz of street distances.

Tests

... sorry, ain't got no tests, because that requires some kind of distance matrices to be used. We can discuss that later.

njtierney commented 4 years ago

🎉 Looks good to me, thanks Mark! :)

mpadge commented 4 years ago

Pleasure. I'm going to update dodgr so the facility data can just be submitted straight to dodgr_dists, without needing the separate match_points_to_graph calls. I'll get another dodgr on CRAN pretty soon, and then be able to simplify the examples