Skip to contents

Extract the flow matrix from a spatial interaction model object in data frame format

Usage

flows_df(sim, ...)

Arguments

sim

a spatial interaction model object

...

additional parameters (not used currently)

Value

a data frame of flows between origin locations and destination locations with additional content if available (see Details).

Details

This function extracts the flow matrix in a long format. Each row contains the flow between an origin location and a destination location. The resulting data frame has at least three columns:

  • origin_idx: identifies the origin location by its index from 1 to the number of origin locations

  • destination_idx: identifies the destination location by its index from 1 to the number of destination locations

  • flow: the flow between the corresponding location

In addition, if location information is available, it will be included in the data frame as follows:

  • location names are included using columns origin_name or destination_name

  • positions are included using 2 or 3 columns (per location type, origin or destination) depending on the number of dimensions used for the location. The names of the columns are by default origin_x, origin_y and origin_z ( and equivalent names for destination location) unless coordinate names are specified in the location positions. In this latter case, the names are prefixed by origin_ or destination_. For instance, if the destination location position coordinates are named "longitude" and "latitude", the resulting columns will be destination_longitude and destination_latitude.

Examples

positions <- matrix(rnorm(10 * 2), ncol = 2)
distances <- as.matrix(dist(positions))
production <- rep(1, 10)
attractiveness <- c(2, rep(1, 9))
## simple case (no positions and default names)
model <- static_blvim(distances, production, 1.5, 1, attractiveness)
head(flows_df(model))
#>   origin_idx destination_idx       flow origin_name destination_name
#> 1          1               1 0.53416792           1                1
#> 2          2               1 0.36380059           2                1
#> 3          3               1 0.03734417           3                1
#> 4          4               1 0.37889403           4                1
#> 5          5               1 0.12041408           5                1
#> 6          6               1 0.16920466           6                1
## with location data
model <- static_blvim(distances, production, 1.5, 1, attractiveness,
  origin_data = list(positions = positions),
  destination_data = list(positions = positions)
)
head(flows_df(model))
#>   origin_idx destination_idx       flow origin_name    origin_x   origin_y
#> 1          1               1 0.53416792           1  1.10018974 -1.1936412
#> 2          2               1 0.36380059           2  1.20376784 -0.7517233
#> 3          3               1 0.03734417           3 -1.43127078  1.4558414
#> 4          4               1 0.37889403           4  1.38291086 -0.8286035
#> 5          5               1 0.12041408           5  0.00312594  0.2897745
#> 6          6               1 0.16920466           6 -0.07788682 -0.4800535
#>   destination_name destination_x destination_y
#> 1                1       1.10019     -1.193641
#> 2                1       1.10019     -1.193641
#> 3                1       1.10019     -1.193641
#> 4                1       1.10019     -1.193641
#> 5                1       1.10019     -1.193641
#> 6                1       1.10019     -1.193641