---
title: "Detecting interspecific interactions"
author: "Alec L. Robitaille, Quinn Webber and Eric Vander Wal"
output: 
  rmarkdown::html_vignette:
    number_sections: true
    toc: true
vignette: >
  %\VignetteIndexEntry{Detecting interspecific interactions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r, echo = FALSE, eval = TRUE}
data.table::setDTthreads(1)
```

## Interspecific interactions

Interspecific data can be used with {spatsoc} to estimate interspecific
interactions, eg. predator-prey dynamics. 

---

See the other vignettes for further information:

- [Introduction to spatsoc](https://docs.ropensci.org/spatsoc/articles/intro.html)
  - temporal grouping
  - spatiotemporal grouping with `group_pts`, `group_lines`, `group_polys`
  - distance based edge-list generation with `edge_dist`
- [Frequently asked questions about spatsoc](https://docs.ropensci.org/spatsoc/articles/faq.html)
  - install
  - function details for `group_times`, `group_pts`, `group_lines`, `group_polys`,
  `edge_dist`, `edge_nn`, and `randomizations`
  - package design including modify-by-reference, data.table column allocation
  - calculating summary information
- [Using spatsoc in social network analysis](https://docs.ropensci.org/spatsoc/articles/using-in-sna.html)
  - generating gambit-of-the-group data
  - generating observed networks
  - data stream randomization, randomized networks
  - network metrics
- [Using distance based edge-lists generating functions, dyad_id, and fusion_id](https://docs.ropensci.org/spatsoc/articles/using-edge-and-dyad.html)
  - generate distance based edge-lists with `edge_dist` and `edge_nn`
  - generate dyad identifiers for edge-lists with `dyad_id`
  - identify fusion events with `fusion_id`
- [Geometry interface](https://docs.ropensci.org/spatsoc/articles/geometry-interface-and-spatial-measures.html)
  - using `get_geometry` to setup a geometry column and use the geometry interface
  - details of underlying distance, direction and centroid spatial measures
  - converting to and from related packages
- [Interspecific interactions](https://docs.ropensci.org/spatsoc/articles/interspecific-interactions.html)
  - combine two movement datasets
  - identify interspecific interactions
  

## Interspecific interactions

Given two movement datasets, simply bind them together and use 
the `group_*` functions as usual. 

```{r}
#| fig.alt: >
#|   Predator prey interactions

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

# Load data
predator <- fread(system.file("extdata", "DT_predator.csv", package = "spatsoc"))
prey <- fread(system.file("extdata", "DT_prey.csv", package = "spatsoc"))

# Combine data
DT <- rbindlist(list(predator, prey))

# Set the datetime as a POSIxct
DT[, datetime := as.POSIXct(datetime)]


# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '10 minutes')

# Spatial grouping
group_pts(DT, threshold = 50, id = 'ID', 
          coords = c('X', 'Y'), timegroup = 'timegroup')


# Calculate the number of types within each group
DT[, n_type := uniqueN(type), by = group]
DT[, interact := n_type > 1]

# Prey's perspective
sub_prey <- DT[type == 'prey']
sub_prey[, mean(interact)]


# Plot --------------------------------------------------------------------
# If we subset only where there are interactions
sub_interact <- DT[(interact)]

plot(sub_prey$X, sub_prey$Y, col = 'grey', pch = 21)
points(sub_interact$X, sub_interact$Y, col = factor(sub_interact$type))
```
