Skip to contents

Given the directional correlation delay, leader_edge_delay calculates the mean directional correlation delay for individuals in a group to identify leadership patterns.

Usage

leader_edge_delay(edges = NULL, threshold = NULL, splitBy = NULL)

Arguments

edges

edge list generated generated by edge_dist or edge_nn, with fusionID column generated by fusion_id

threshold

(optional) threshold difference in direction used to subset rows included in calculation of mean directional delay. eg. threshold = 0.5 corresponds to only rows where direction_diff is less than 0.5. Expects that unit is radians, see edge_delay.

splitBy

(optional) character string or vector of grouping column name(s) upon which the mean directional correlation delay will be calculated

Value

leader_edge_delay returns the input edges appended with a mean_direction_delay_dyad column indicating the mean directional correlation delay between ID1 and ID2 and a mean_direction_delay column indicating the mean directional correlation delay for each individual in 'ID1' column.

Details

The function expects an edge list from edge_delay with columns 'direction_delay' indicating the directional correlation delay between individuals and 'direction_diff' indicating the unsigned difference in movement directions at the temporal delay, columns 'ID1' and 'ID2' indicating individuals and column 'dyadID' indicating the dyad.

The edge must be a data.table. If your data is a data.frame, you can convert it by reference using data.table::setDT or by reassigning using data.table::data.table.

Examples

# Load data.table
library(data.table)

# Read example data
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))

# Select only individuals A, B, C for this example
DT <- DT[ID %in% c('A', 'B', 'C')]

# Cast the character column to POSIXct
DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
#>           ID        X       Y            datetime population
#>       <char>    <num>   <num>              <POSc>      <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1
#>   ---                                                       
#> 4265:      C 702093.6 5510180 2017-02-28 14:00:44          1
#> 4266:      C 702086.0 5510183 2017-02-28 16:00:42          1
#> 4267:      C 702961.8 5509447 2017-02-28 18:00:53          1
#> 4268:      C 703130.4 5509528 2017-02-28 20:00:54          1
#> 4269:      C 702872.3 5508531 2017-02-28 22:00:18          1

# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '20 minutes')
#>           ID        X       Y            datetime population minutes timegroup
#>       <char>    <num>   <num>              <POSc>      <int>   <int>     <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 4265:      C 702093.6 5510180 2017-02-28 14:00:44          1       0      1393
#> 4266:      C 702086.0 5510183 2017-02-28 16:00:42          1       0      1394
#> 4267:      C 702961.8 5509447 2017-02-28 18:00:53          1       0      1440
#> 4268:      C 703130.4 5509528 2017-02-28 20:00:54          1       0      1395
#> 4269:      C 702872.3 5508531 2017-02-28 22:00:18          1       0      1396

# Calculate direction
direction_step(
  DT = DT,
  id = 'ID',
  coords = c('X', 'Y'),
  projection = 32736
)
#>           ID        X       Y            datetime population minutes timegroup
#>       <char>    <num>   <num>              <POSc>      <int>   <int>     <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 4265:      C 702093.6 5510180 2017-02-28 14:00:44          1       0      1393
#> 4266:      C 702086.0 5510183 2017-02-28 16:00:42          1       0      1394
#> 4267:      C 702961.8 5509447 2017-02-28 18:00:53          1       0      1440
#> 4268:      C 703130.4 5509528 2017-02-28 20:00:54          1       0      1395
#> 4269:      C 702872.3 5508531 2017-02-28 22:00:18          1       0      1396
#>               direction
#>                 <units>
#>    1: -2.65649015 [rad]
#>    2:  2.17592086 [rad]
#>    3: -1.98432277 [rad]
#>    4:  1.90650150 [rad]
#>    5: -0.04059949 [rad]
#>   ---                  
#> 4265: -1.16059019 [rad]
#> 4266:  2.24288689 [rad]
#> 4267:  1.09317866 [rad]
#> 4268: -2.91547765 [rad]
#> 4269:          NA [rad]

# Distance based edge list generation
edges <- edge_dist(
  DT,
  threshold = 100,
  id = 'ID',
  coords = c('X', 'Y'),
  timegroup = 'timegroup',
  returnDist = TRUE,
  fillNA = FALSE
)

# Generate dyad id
dyad_id(edges, id1 = 'ID1', id2 = 'ID2')
#>       timegroup    ID1    ID2 distance dyadID
#>           <int> <char> <char>    <num> <char>
#>    1:       116      C      A 61.41036    A-C
#>    2:       116      A      C 61.41036    A-C
#>    3:       117      C      A 38.18977    A-C
#>    4:       117      A      C 38.18977    A-C
#>    5:       119      C      A 18.30832    A-C
#>   ---                                        
#> 1538:      1395      A      C 12.31124    A-C
#> 1539:      1397      C      B 24.45925    B-C
#> 1540:      1397      B      C 24.45925    B-C
#> 1541:      1398      C      B 23.01744    B-C
#> 1542:      1398      B      C 23.01744    B-C

# Generate fusion id
fusion_id(edges, threshold = 100)
#>       timegroup    ID1    ID2 distance dyadID fusionID
#>           <int> <char> <char>    <num> <char>    <int>
#>    1:       116      C      A 61.41036    A-C        1
#>    2:       116      A      C 61.41036    A-C        1
#>    3:       117      C      A 38.18977    A-C        1
#>    4:       117      A      C 38.18977    A-C        1
#>    5:       119      C      A 18.30832    A-C        2
#>   ---                                                 
#> 1538:      1395      A      C 12.31124    A-C      206
#> 1539:      1397      C      B 24.45925    B-C      207
#> 1540:      1397      B      C 24.45925    B-C      207
#> 1541:      1398      C      B 23.01744    B-C      208
#> 1542:      1398      B      C 23.01744    B-C      208

# Directional correlation delay
delay <- edge_delay(
  edges = edges,
  DT = DT,
  window = 3,
  id = 'ID'
)

# Leadership from directional correlation delay
leadership <- leader_edge_delay(
  delay,
  threshold = 0.5
)
print(leadership)
#>       ID1    ID2 dyadID mean_direction_delay_dyad mean_direction_delay
#>    <char> <char> <char>                     <num>                <num>
#> 1:      C      A    A-C               -0.05806452          -0.10256167
#> 2:      A      C    A-C                0.05806452           0.12462049
#> 3:      C      B    B-C               -0.14705882          -0.10256167
#> 4:      B      C    B-C                0.14705882          -0.02205882
#> 5:      B      A    A-B               -0.19117647          -0.02205882
#> 6:      A      B    A-B                0.19117647           0.12462049