Skip to content

Fix/bug in lead fusionID fix#144

Merged
robitalec merged 3 commits intomainfrom
fix/fusion-id-tg-diff-bug
Mar 6, 2026
Merged

Fix/bug in lead fusionID fix#144
robitalec merged 3 commits intomainfrom
fix/fusion-id-tg-diff-bug

Conversation

@robitalec
Copy link
Member

@robitalec robitalec commented Mar 6, 2026

Missing one by = dyadID in recent fix #142 to consider lead fusions causes fusions to mix across observations within dyadIDs. One way to pick them up is looking at the difference between subsequent timegroups within a fusionID:

library(spatsoc)
library(data.table)

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

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
#>    ---                                                                         
#> 14293:      J 700616.5 5509069 2017-02-28 14:00:54          1       0      1393
#> 14294:      J 700622.6 5509065 2017-02-28 16:00:11          1       0      1394
#> 14295:      J 700657.5 5509277 2017-02-28 18:00:55          1       0      1440
#> 14296:      J 700610.3 5509269 2017-02-28 20:00:48          1       0      1395
#> 14297:      J 700744.0 5508782 2017-02-28 22:00:39          1       0      1396

edges <- edge_dist(
  DT,
  threshold = 100,
  id = 'ID',
  coords = c('X', 'Y'),
  timegroup = 'timegroup',
  returnDist = TRUE,
  fillNA = TRUE
)

dyad_id(edges, 'ID1', 'ID2')
#> Key: <timegroup, ID1>
#>        timegroup    ID1    ID2  distance dyadID
#>            <int> <char> <char>     <num> <char>
#>     1:         1      A   <NA>        NA   <NA>
#>     2:         1      B      G  5.782904    B-G
#>     3:         1      C   <NA>        NA   <NA>
#>     4:         1      D   <NA>        NA   <NA>
#>     5:         1      E      H 65.061671    E-H
#>    ---                                         
#> 22985:      1440      G   <NA>        NA   <NA>
#> 22986:      1440      H   <NA>        NA   <NA>
#> 22987:      1440      I      C  2.831071    C-I
#> 22988:      1440      I      F  7.512922    F-I
#> 22989:      1440      J   <NA>        NA   <NA>

n_min_length <- 3
n_max_missing <- 1
allow_split <- TRUE


fusions <- fusion_id(
  edges,
  threshold = 50,
  n_min_length = n_min_length,
  n_max_missing = n_max_missing,
  allow_split = allow_split
)

check <- fusions[, max(timegroup - shift(timegroup), na.rm = TRUE), by = fusionID]
check[V1 > (n_min_length + n_max_missing)]
#>     fusionID    V1
#>        <int> <int>
#>  1:        1  1339
#>  2:        2   117
#>  3:        6   933
#>  4:        7    46
#>  5:        9   262
#>  6:       13    32
#>  7:       14   217
#>  8:       17  1254
#>  9:       18  1215
#> 10:       19   357
#> 11:       25   417
#> 12:       26   515
#> 13:       36   450
#> 14:       39   460
#> 15:       43   456
#> 16:       48   409
#> 17:       66   528
#> 18:       77   133
#> 19:       78   388
#> 20:       82   468
#> 21:       84   176
#> 22:       86   176
#> 23:       91   347
#> 24:      103   395
#> 25:      110   214
#> 26:      112   344
#> 27:      114   405
#> 28:      116   258
#> 29:      127   136
#> 30:      128   239
#> 31:      129    10
#> 32:      142   283
#> 33:      151    89
#> 34:      157    53
#> 35:      160   131
#> 36:      169   722
#> 37:      170   641
#> 38:      172   299
#> 39:      174   232
#> 40:      175   187
#> 41:      197   342
#> 42:      201   123
#> 43:      202   214
#> 44:      203   120
#> 45:      204   110
#> 46:      207   101
#> 47:      211   154
#> 48:      212    59
#> 49:      229   137
#> 50:      232   140
#> 51:      241   151
#> 52:      246   426
#> 53:      269    16
#> 54:      287    70
#> 55:      288   475
#> 56:      290    54
#> 57:      295   171
#> 58:      299   294
#> 59:      303    73
#> 60:      338   350
#> 61:      350   452
#> 62:      353   348
#> 63:      400   306
#> 64:      408    23
#> 65:      445   309
#> 66:      447   256
#> 67:      466   154
#> 68:      477    98
#> 69:      514    19
#> 70:      545    12
#>     fusionID    V1

Created on 2026-03-05 with reprex v2.1.1

@robitalec robitalec added the status: confirmed bug is reproducible label Mar 6, 2026
@robitalec robitalec merged commit 922de3b into main Mar 6, 2026
8 checks passed
@robitalec robitalec added status: completed and removed status: confirmed bug is reproducible labels Mar 6, 2026
@robitalec robitalec deleted the fix/fusion-id-tg-diff-bug branch March 6, 2026 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant