knitr::opts_chunk$set(message = TRUE, warning = FALSE, eval = FALSE, echo = FALSE)
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 and edge list generating functions that are used for identifying spatially and temporally explicit groups from input data. In addition, we provide social network analysis functions 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 and for generating group by individual matrices.
The functions were developed for application across animal relocation data, for example, proximity based social network analyses and spatial and temporal clustering of points.
# Load packages library(spatsoc) library(data.table)
data.table::setDTthreads(1)
# Read in spatsoc's example data DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc")) # Use subset of individuals DT <- DT[ID %in% c('H', 'I', 'J')] # Cast character column 'datetime' as POSIXct DT[, datetime := as.POSIXct(datetime, tz = 'UTC')] DT <- DT[ID %chin% c('H', 'I', 'J')]
knitr::kable(DT[, .SD[1:2], ID][order(ID)])
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 r DT[, uniqueN(ID)]
individuals over r DT[, max(yday(datetime)) - min(yday(datetime))]
days. Using these data, we can compare the various grouping methods available in spatsoc
. Note: these examples will use a subset of the data, only individuals H, I and J.
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')
nRows <- 9
knitr::kable( group_times(DT, threshold = '5 minutes', datetime = 'datetime')[, .(ID, X, Y, datetime, minutes, timegroup)][ order(datetime)][ 1:nRows])
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')
knitr::kable( group_times(DT, threshold = '2 hours', datetime = 'datetime')[, .(ID, X, Y, datetime, hours, timegroup)][ order(datetime)][ 1:nRows])
group_times(DT, datetime = 'datetime', threshold = '5 days')
knitr::kable( group_times(DT, threshold = '5 days', datetime = 'datetime')[, .SD[sample(.N, 3)], by = .(timegroup, block)][order(datetime)][ 1:nRows, .(ID, X, Y, datetime, block, timegroup)])
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. The group_pts
function uses the "chain rule" where three or more individuals that are all within the defined threshold distance of at least one other individual are considered in the same group. For point based spatial grouping with a distance threshold that does not use the chain rule, see edge_dist
below.
group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') group_pts(DT, threshold = 50, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup')
DT <- group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') DT <- group_pts( DT = DT, threshold = 50, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup') knitr::kable( DT[ between(group, 771, 774)][order(timegroup)][ 1:nRows, .(ID, X, Y, timegroup, group)] )
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 <- 32736 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')
utm <- 32736 DT <- group_times(DT = DT, datetime = 'datetime', threshold = '1 day') DT <- group_lines(DT, threshold = 50, projection = utm, id = 'ID', coords = c('X', 'Y'), sortBy = 'datetime', timegroup = 'timegroup') knitr::kable( unique(DT[, .(ID, timegroup, group)])[, .SD[1:3], ID][order(timegroup)] )
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
simple features POLYGON or MULTIPOLYGON object can be provided to the sfPolys
argument along with an id column.
utm <- 32736 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')
utm <- 32736 DT <- group_times(DT = DT, datetime = 'datetime', threshold = '8 days') knitr::kable( data.frame(group_polys( DT, area = TRUE, hrType = 'mcp', hrParams = list('percent' = 95), projection = utm, coords = c('X', 'Y'), id = 'ID')[ , .(ID1, ID2, area, proportion)]) )
The edge_dist
function calculates the geographic distance between between individuals within each timegroup and returns all paired relocations within the spatial threshold. edge_dist
uses a distance matrix like group_pts, but, in contrast, does not use the chain rule to group relocations.
group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') edge_dist(DT, threshold = 50, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup', fillNA = TRUE)
DT <- group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') edges <- edge_dist(DT, threshold = 50, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup', fillNA = TRUE) knitr::kable( edges[between(timegroup, 158, 160)] )
The edge_nn
function calculates the nearest neighbour to each individual within each time group. If the optional distance threshold is provided, it is used to limit the maximum distance between neighbours. edge_nn
returns an edge list of each individual and their nearest neighbour.
group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') edge_nn(DT, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup')
DT <- group_times(DT = DT, datetime = 'datetime', threshold = '15 minutes') edges <- edge_nn(DT, id = 'ID', coords = c('X', 'Y'), timegroup = 'timegroup') knitr::kable( edges[1:6] )
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.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.