The spatsoc package provides functionality for analyzing animal relocation data in time and space to identify potential interactions among individuals and build gambit-of-the-group data for constructing social networks.
The package contains grouping functions that are used for identifying spatially and temporally explicit groups from input data. In addition, we provide a function for randomizing individual identifiers within groups, designed to test whether social networks generated from animal relocation data were based on non-random social proximity among individuals.
The functions were developed for application across animal relocation data, for example, proximity based social network analyses and spatial and temporal clustering of points.
spatsoc expects a data.table for all of its functions. If you have a data.frame, you can use data.table::setDT() to convert it by reference. If your data is a CSV, you can use data.table::fread() to import it as a data.table.
The data consist of relocations of 10 individuals over 365 days. Using these data, we can compare the various grouping methods available in spatsoc.
library(spatsoc)
library(data.table)
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))
DT[, datetime := as.POSIXct(datetime,
tz = 'UTC')]
| ID | X | Y | datetime | population |
|---|---|---|---|---|
| F | 705455.5 | 5510309 | 2017-02-19 10:00:54 | 1 |
| B | 700180.2 | 5508577 | 2016-11-21 20:00:41 | 1 |
| F | 703576.8 | 5511196 | 2017-02-08 12:00:25 | 1 |
| B | 697908.9 | 5510673 | 2017-02-26 04:00:47 | 1 |
| A | 703460.9 | 5503296 | 2016-12-14 22:00:54 | 1 |
group_timesThe group_times function is used to group relocations temporally. It is flexible to a threshold provided in units of minutes, hours or days. Since GPS fixes taken at regular intervals have some level of variability, we will provide a time threshold (threshold), to consider all fixes within this threshold taken at the same time. Alternatively, we may want to understand different scales of grouping, perhaps daily movement trajectories or seasonal home range overlap.
group_times(DT, datetime = 'datetime', threshold = '5 minutes')
| ID | X | Y | datetime | minutes | timegroup |
|---|---|---|---|---|---|
| I | 711042.0 | 5506384 | 2016-11-01 00:00:24 | 0 | 1 |
| C | 710205.4 | 5505888 | 2016-11-01 00:00:44 | 0 | 1 |
| D | 700875.0 | 5490954 | 2016-11-01 00:00:47 | 0 | 1 |
| E | 701671.9 | 5504286 | 2016-11-01 00:00:48 | 0 | 1 |
| F | 705583.0 | 5513813 | 2016-11-01 00:00:48 | 0 | 1 |
| G | 699635.5 | 5509635 | 2016-11-01 00:00:48 | 0 | 1 |
| H | 701724.1 | 5504325 | 2016-11-01 00:00:49 | 0 | 1 |
| A | 715851.4 | 5505340 | 2016-11-01 00:00:54 | 0 | 1 |
| J | 707568.6 | 5500406 | 2016-11-01 00:00:56 | 0 | 1 |
A message is returned when group_times is run again on the same DT, as the columns already exist in the input DT and will be overwritten.
group_times(DT, datetime = 'datetime', threshold = '2 hours')
## minutes, timegroup columns found in input DT and will be overwritten by this function
| ID | X | Y | datetime | hours | timegroup |
|---|---|---|---|---|---|
| I | 711042.0 | 5506384 | 2016-11-01 00:00:24 | 0 | 1 |
| C | 710205.4 | 5505888 | 2016-11-01 00:00:44 | 0 | 1 |
| D | 700875.0 | 5490954 | 2016-11-01 00:00:47 | 0 | 1 |
| E | 701671.9 | 5504286 | 2016-11-01 00:00:48 | 0 | 1 |
| F | 705583.0 | 5513813 | 2016-11-01 00:00:48 | 0 | 1 |
| G | 699635.5 | 5509635 | 2016-11-01 00:00:48 | 0 | 1 |
| H | 701724.1 | 5504325 | 2016-11-01 00:00:49 | 0 | 1 |
| A | 715851.4 | 5505340 | 2016-11-01 00:00:54 | 0 | 1 |
| J | 707568.6 | 5500406 | 2016-11-01 00:00:56 | 0 | 1 |
group_times(DT, datetime = 'datetime', threshold = '5 days')
## hours, timegroup columns found in input DT and will be overwritten by this function
| ID | X | Y | datetime | block | timegroup |
|---|---|---|---|---|---|
| I | 711124.0 | 5506407 | 2016-11-01 04:00:44 | 1 | 1 |
| B | 699240.3 | 5510142 | 2016-11-02 04:00:54 | 1 | 1 |
| H | 703319.4 | 5503448 | 2016-11-02 22:00:36 | 1 | 1 |
| F | 706078.6 | 5512317 | 2016-11-09 12:00:48 | 2 | 2 |
| C | 701022.8 | 5505840 | 2016-11-10 14:00:08 | 2 | 2 |
| B | 697988.4 | 5511889 | 2016-11-10 20:00:47 | 2 | 2 |
| F | 707980.6 | 5512335 | 2016-11-11 20:00:49 | 3 | 3 |
| J | 705627.0 | 5498892 | 2016-11-11 20:00:53 | 3 | 3 |
| E | 702667.3 | 5504936 | 2016-11-12 16:00:33 | 3 | 3 |
group_ptsThe group_pts function compares the relocations of all individuals in each timegroup and groups individuals based on a distance threshold provided by the user.
group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes')
group_pts(DT, threshold = 50, id = 'ID', coords = c('X', 'Y'))
## block, timegroup columns found in input DT and will be overwritten by this function
| ID | X | Y | timegroup | group |
|---|---|---|---|---|
| A | 715851.4 | 5505340 | 1 | 1 |
| A | 715822.8 | 5505289 | 2 | 2 |
| A | 715872.9 | 5505252 | 3 | 3 |
| A | 715820.5 | 5505231 | 4 | 4 |
| A | 715830.6 | 5505227 | 5 | 5 |
| A | 715829.4 | 5505332 | 6 | 6 |
| A | 715306.6 | 5505837 | 7 | 7 |
| A | 715349.7 | 5505644 | 8 | 8 |
| A | 715224.1 | 5505661 | 9 | 9 |
group_linesThe group_lines function groups individuals whose trajectories intersect in a specified time interval. This represents a coarser grouping method than group_pts which can help understand shared space at daily, weekly or other temporal resolutions.
utm <- '+proj=utm +zone=21 ellps=WGS84'
group_times(DT = DT, datetime = 'datetime', threshold = '1 day')
group_lines(DT, threshold = 50, projection = utm,
id = 'ID', coords = c('X', 'Y'),
timegroup = 'timegroup', sortBy = 'datetime')
## minutes, timegroup columns found in input DT and will be overwritten by this function
## group column will be overwritten by this function
| ID | X | Y | timegroup | group |
|---|---|---|---|---|
| A | 715851.4 | 5505340 | 1 | 1 |
| A | 715822.8 | 5505289 | 1 | 1 |
| A | 715872.9 | 5505252 | 1 | 1 |
| A | 715820.5 | 5505231 | 1 | 1 |
| A | 715830.6 | 5505227 | 1 | 1 |
| A | 715829.4 | 5505332 | 1 | 1 |
| A | 715306.6 | 5505837 | 1 | 1 |
| A | 715349.7 | 5505644 | 1 | 1 |
| A | 715224.1 | 5505661 | 1 | 1 |
group_polysThe group_polys function groups individuals whose home ranges intersect. This represents the coarsest grouping method, to provide a measure of overlap across seasons, years or all available relocations. It can either return the proportion of home range area overlapping between individuals or simple groups. Home ranges are calculated using adehabitatHR::kernelUD or adehabitatHR::mcp. Alternatively, a SpatialPolygonsDataFrame can be input to the spPolys argument.
utm <- '+proj=utm +zone=21 ellps=WGS84'
group_times(DT = DT, datetime = 'datetime', threshold = '8 days')
group_polys(DT = DT, area = TRUE, hrType = 'mcp',
hrParams = list('percent' = 95),
projection = utm,
coords = c('X', 'Y'), id = 'ID')
## timegroup columns found in input DT and will be overwritten by this function
| ID1 | ID2 | area | proportion |
|---|---|---|---|
| A | A | 145142204 | 1.0000000 |
| A | B | 9731424 | 0.0670475 |
| A | C | 110069503 | 0.7583563 |
| A | E | 60057115 | 0.4137812 |
| A | F | 87608610 | 0.6036053 |
| A | G | 12182087 | 0.0839321 |
| A | H | 61089896 | 0.4208968 |
| A | I | 125675100 | 0.8658757 |
| A | J | 101995862 | 0.7027306 |
| B | A | 9731424 | 0.9167682 |
| B | B | 10614922 | 1.0000000 |
| B | C | 9554942 | 0.9001425 |
| B | E | 9906676 | 0.9332783 |
| B | F | 9509693 | 0.8958796 |
| B | G | 10573553 | 0.9961027 |
| B | H | 10145705 | 0.9557964 |
| B | I | 9711953 | 0.9149339 |
| B | J | 10384186 | 0.9782630 |
| C | A | 110069503 | 0.9996112 |
| C | B | 9554942 | 0.0867745 |
| C | C | 110112310 | 1.0000000 |
| C | E | 54497018 | 0.4949221 |
| C | F | 82198072 | 0.7464930 |
| C | G | 12003003 | 0.1090069 |
| C | H | 55343937 | 0.5026135 |
| C | I | 109925578 | 0.9983042 |
| C | J | 88882259 | 0.8071964 |
| D | D | 11667275 | 1.0000000 |
| E | A | 60057115 | 0.9628674 |
| E | B | 9906676 | 0.1588291 |
| E | C | 54497018 | 0.8737250 |
| E | E | 62373195 | 1.0000000 |
| E | F | 45418786 | 0.7281780 |
| E | G | 12323593 | 0.1975784 |
| E | H | 61624267 | 0.9879928 |
| E | I | 56208280 | 0.9011608 |
| E | J | 61868357 | 0.9919062 |
| F | A | 87608610 | 0.9384981 |
| F | B | 9509693 | 0.1018716 |
| F | C | 82198072 | 0.8805382 |
| F | E | 45418786 | 0.4865440 |
| F | F | 93349803 | 1.0000000 |
| F | G | 11958427 | 0.1281034 |
| F | H | 46287984 | 0.4958552 |
| F | I | 83430540 | 0.8937409 |
| F | J | 61213465 | 0.6557428 |
| G | A | 12182087 | 0.9320482 |
| G | B | 10573553 | 0.8089797 |
| G | C | 12003003 | 0.9183466 |
| G | E | 12323593 | 0.9428748 |
| G | F | 11958427 | 0.9149361 |
| G | G | 13070233 | 1.0000000 |
| G | H | 12563547 | 0.9612336 |
| G | I | 12162428 | 0.9305441 |
| G | J | 12803712 | 0.9796086 |
| H | A | 61089896 | 0.7535271 |
| H | B | 10145705 | 0.1251445 |
| H | C | 55343937 | 0.6826523 |
| H | E | 61624267 | 0.7601184 |
| H | F | 46287984 | 0.5709496 |
| H | G | 12563547 | 0.1549679 |
| H | H | 81071930 | 1.0000000 |
| H | I | 57514743 | 0.7094286 |
| H | J | 66161291 | 0.8160814 |
| I | A | 125675100 | 0.9993982 |
| I | B | 9711953 | 0.0772317 |
| I | C | 109925578 | 0.8741542 |
| I | E | 56208280 | 0.4469816 |
| I | F | 83430540 | 0.6634594 |
| I | G | 12162428 | 0.0967185 |
| I | H | 57514743 | 0.4573709 |
| I | I | 125750781 | 1.0000000 |
| I | J | 93471355 | 0.7433064 |
| J | A | 101995862 | 0.7697949 |
| J | B | 10384186 | 0.0783727 |
| J | C | 88882259 | 0.6708224 |
| J | E | 61868357 | 0.4669400 |
| J | F | 61213465 | 0.4619973 |
| J | G | 12803712 | 0.0966336 |
| J | H | 66161291 | 0.4993401 |
| J | I | 93471355 | 0.7054578 |
| J | J | 132497451 | 1.0000000 |
Package dependencies for spatsoc are sp, rgeos, igraph, adehabitatHR and data.table. data.table provides efficient methods for manipulating large (or small) datasets. As a result, input DT for all spatsoc functions must be a data.table and if it isn't, you can simply use data.table::setDT(df) to convert it by reference.
In addition, since the rgeos package is used in most functions (group_lines and group_polys) the input DT's coordinate system is important. rgeos expects planar coordinates and this requirement is carried forward for spatsoc. Since rgeos is used, system dependencies include GEOS.