Search code examples
rr-spr-gridod

get the most common source-destination trajectories in R


I have two spatial points dataset, one for origins and one for destinations.

I'd like to take the most recurring trajectories from these coordinates.

> salidas
class       : SpatialPointsDataFrame 
features    : 4385 
extent      : -8.694846, -8.339238, 41.00827, 41.25749  (xmin, xmax, ymin, ymax)
crs         : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 3
names       :               cod, duracion, franja_h 
min values  : 1.37263685362e+18,      315,        1 
max values  : 1.37274729362e+18,    13830,       96 

> llegadas
class       : SpatialPointsDataFrame 
features    : 4385 
extent      : -8.756604, -7.739523, 40.48858, 41.4262  (xmin, xmax, ymin, ymax)
crs         : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables   : 3
names       :               cod, duracion, franja_h 
min values  : 1.37263685362e+18,      315,        1 
max values  : 1.37274729362e+18,    13830,       96 

I think the points should be kept discreet, as they are not too specific and do not give too much information, so I've made a grid of X and Y coordinates for it.

> GridSalidas
          X       Y Count
1   -8.3375 41.1975     1
2   -8.5125 41.2025     1
3   -8.5325 41.1425     1
4   -8.5325 41.2075     1
5   -8.5325 41.2225     1
6   -8.5475 41.2025     1
7   -8.5475 41.2075     1
8   -8.5475 41.2325     1
9   -8.5525 41.2075     1
10  -8.5525 41.2175     1

> GridLlegadas
          X       Y Count
1   -7.7375 41.2975     1
2   -7.8625 40.4875     1
3   -8.1475 41.1875     1
4   -8.3075 41.1975     1
5   -8.4725 41.3225     1
6   -8.4875 41.1875     1
7   -8.4925 41.1925     1
8   -8.4975 41.1875     2
9   -8.5025 41.0425     1
10  -8.5025 41.1925     1

As a result, I'd like to find out which trajectories are more common depending on the origin and the destination.

Thanks!


Solution

  • All you are asking for is some multi-dimensional binning.

    I generated a random dataset dt of origin and destination for the purpose of demonstration. The output result is a data.table that gives the following information about the most frequent trajectory:

    • Lower and upper limits of the x-y coordinates that define the source grid
    • Lower and upper limits of the x-y coordinates that define the destination grid
    • Count
    library(data.table)
    library(magrittr)
    
    N <- 5000
    set.seed(123)
    gp <- 0.1 #grid precision
    
    # Generate an example dataset -----
    {
      dt <- data.table(
        origin_x = rnorm(N, 1, 0.1),
        origin_y = rnorm(N, 2, 0.1),
        destination_x = rnorm(N, 11, 0.1),
        destination_y = rnorm(N, 12, 0.1)
      )
    }
    
    # Grid formation ----
    {
      ## Defining the ranges (LL and UL stand for lower and upper limits, respectively) ----
      {
        origin_x_LL <- dt[, origin_x] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
        origin_x_UL <- dt[, origin_x] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
        origin_y_LL <- dt[, origin_y] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
        origin_y_UL <- dt[, origin_y] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
        destination_x_LL <- dt[, destination_x] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
        destination_x_UL <- dt[, destination_x] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
        destination_y_LL <- dt[, destination_y] %>% min %>% divide_by(gp) %>% floor %>% multiply_by(gp)
        destination_y_UL <- dt[, destination_y] %>% max %>% divide_by(gp) %>% ceiling %>% multiply_by(gp)
      }
      ## Forming the breaks for binning ----
      {
        origin_x_brks <- seq(origin_x_LL, origin_x_UL, by = gp)
        origin_y_brks <- seq(origin_y_LL, origin_y_UL, by = gp)
        destination_x_brks <- seq(destination_x_LL, destination_x_UL, by = gp)
        destination_y_brks <- seq(destination_y_LL, destination_y_UL, by = gp)
      }
      ## Computing the number of bins ----
      {
        origin_x_Nbin <- length(origin_x_brks) - 1L
        origin_y_Nbin <- length(origin_y_brks) - 1L
        destination_x_Nbin <- length(destination_x_brks) - 1L
        destination_y_Nbin <- length(destination_y_brks) - 1L
      }
      ## Binning ----
      {
        origin_x_bin <- .bincode(dt[, origin_x], origin_x_brks, include.lowest = T)
        origin_y_bin <- .bincode(dt[, origin_y], origin_y_brks, include.lowest = T)
        destination_x_bin <- .bincode(dt[, destination_x], destination_x_brks, include.lowest = T)
        destination_y_bin <- .bincode(dt[, destination_y], destination_y_brks, include.lowest = T)
      }
    }
    
    # Counting grid frequency ----
    {
      grid_count <-
        lapply(seq(origin_x_Nbin), function(i) {
          lapply(seq(origin_y_Nbin), function(j) {
            lapply(seq(destination_x_Nbin), function(m) {
              lapply(seq(destination_y_Nbin), function(n) {
                this_count = which(origin_x_bin == i & origin_y_bin == j & destination_x_bin == m & destination_y_bin == n) %>% length
                return(data.table(origin_x_LL = origin_x_brks[i], origin_x_UL = origin_x_brks[i + 1],
                                  origin_y_LL = origin_y_brks[j], origin_y_UL = origin_y_brks[j + 1],
                                  destination_x_LL = destination_x_brks[m], destination_x_UL = destination_x_brks[m + 1],
                                  destination_y_LL = destination_y_brks[n], destination_y_UL = destination_y_brks[n + 1],
                                  count = this_count))
              }) %>% rbindlist
            }) %>% rbindlist
          }) %>% rbindlist
        }) %>% rbindlist
    }
    
    # Getting the most frequent grid ----
    {
      print(grid_count[count == max(count)])
    }