---
title: "Additional data formats"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Additional data formats}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup}
library(spatsoc)
library(data.table)
```

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

## Multispecies interactions

Multispecies data can be used with `spatsoc` to estimate 
interspecific interactions, eg. predator-prey dyanmics. 

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


```{r}
predator <- fread(system.file("extdata", "DT_predator.csv", package = "spatsoc"))
prey <- fread(system.file("extdata", "DT_prey.csv", package = "spatsoc"))

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)]

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