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.

Grouping

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 3 individuals over 365 days. Using these data, we can compare the various grouping methods available in spatsoc.

## Load packages
library(spatsoc)
library(data.table)

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

## Subset to only individuals H, I, and J
DT <- DT[ID %in% c('H', 'I', 'J')]

## Cast character column 'datetime' as POSIXct
DT[, datetime := as.POSIXct(datetime,
                            tz = 'UTC')]
ID X Y datetime population
I 711042.0 5506384 2016-11-01 00:00:24 1
H 701724.1 5504325 2016-11-01 00:00:49 1
J 707568.6 5500406 2016-11-01 00:00:56 1
J 707566.5 5500404 2016-11-01 02:00:21 1
H 701648.5 5504276 2016-11-01 02:00:33 1
I 711229.0 5506446 2016-11-01 02:00:33 1

group_times

The 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
H 701724.1 5504325 2016-11-01 00:00:49 0 1
J 707568.6 5500406 2016-11-01 00:00:56 0 1
J 707566.5 5500404 2016-11-01 02:00:21 0 2
H 701648.5 5504276 2016-11-01 02:00:33 0 2
I 711229.0 5506446 2016-11-01 02:00:33 0 2
J 707562.6 5500374 2016-11-01 04:00:41 0 3
I 711124.0 5506407 2016-11-01 04:00:44 0 3
H 701607.2 5504291 2016-11-01 04:00:54 0 3

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
H 701724.1 5504325 2016-11-01 00:00:49 0 1
J 707568.6 5500406 2016-11-01 00:00:56 0 1
J 707566.5 5500404 2016-11-01 02:00:21 2 2
H 701648.5 5504276 2016-11-01 02:00:33 2 2
I 711229.0 5506446 2016-11-01 02:00:33 2 2
J 707562.6 5500374 2016-11-01 04:00:41 4 3
I 711124.0 5506407 2016-11-01 04:00:44 4 3
H 701607.2 5504291 2016-11-01 04:00:54 4 3
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
H 703319.4 5503448 2016-11-02 22:00:36 1 1
H 702320.5 5503328 2016-11-03 14:00:24 1 1
H 701877.1 5505031 2016-11-04 20:00:43 1 1
H 700871.5 5504582 2016-11-07 18:00:25 2 2
J 707927.4 5500058 2016-11-08 06:00:41 2 2
J 705507.5 5499069 2016-11-08 16:00:06 2 2
J 707533.1 5500404 2016-11-11 06:00:24 3 3
H 703133.0 5504843 2016-11-11 22:00:42 3 3
I 704344.4 5505799 2016-11-15 16:00:48 3 3

group_pts

The 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'), timegroup = 'timegroup')
## block, timegroup columns found in input DT and will be overwritten by this function
ID X Y timegroup group
H 699126.1 5508836 771 771
I 699130.0 5508761 771 771
J 699138.0 5508797 771 771
H 699930.5 5508032 772 772
H 700139.2 5507325 773 773
I 700131.7 5507321 773 773
H 700012.2 5508010 774 774
I 700015.0 5508001 774 774
J 700002.3 5508005 774 774

group_lines

The 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 timegroup group
H 1 1
I 1 121
J 1 204
H 2 2
I 2 122
J 2 205
H 3 3
I 3 123
J 3 206

group_polys

The 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
H H 81071930 1.0000000
H I 57514743 0.7094286
H J 66161291 0.8160814
I H 57514743 0.4573709
I I 125750781 1.0000000
I J 93471355 0.7433064
J H 66161291 0.4993401
J I 93471355 0.7054578
J J 132497451 1.0000000

Notes

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.