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.5099030           1                1
#> 2          2               1 0.1163163           2                1
#> 3          3               1 0.1552133           3                1
#> 4          4               1 0.1397605           4                1
#> 5          5               1 0.3031391           5                1
#> 6          6               1 0.4383819           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.5099030           1 -0.7517233 -0.2122360
#> 2          2               1 0.1163163           2  1.4558414 -0.9063402
#> 3          3               1 0.1552133           3 -0.8286035 -2.1021525
#> 4          4               1 0.1397605           4  0.2897745  1.8933605
#> 5          5               1 0.3031391           5 -0.4800535 -0.9681258
#> 6          6               1 0.4383819           6 -0.6048294 -0.1026030
#>   destination_name destination_x destination_y
#> 1                1    -0.7517233     -0.212236
#> 2                1    -0.7517233     -0.212236
#> 3                1    -0.7517233     -0.212236
#> 4                1    -0.7517233     -0.212236
#> 5                1    -0.7517233     -0.212236
#> 6                1    -0.7517233     -0.212236