<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/detecting-spatiotemporal-groups-in-relocation-data-with-spatsoc/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ropensci.org/blog/2018/12/04/spatsoc/"> rOpenSci - open tools for open science</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://github.com/ropensci/spatsoc" target="_blank">spatsoc</a> is an R package written by Alec Robitaille, Quinn Webber and Eric Vander Wal of the Wildlife Evolutionary Ecology Lab (<a rel="nofollow" href="https://weel.gitlab.io/" target="_blank">WEEL</a>) at Memorial University of Newfoundland. It is the lab’s first R package and was recently accepted through the rOpenSci onboarding process with a big thanks to reviewers <a rel="nofollow" href="https://github.com/pmnatural" target="_blank">Priscilla Minotti</a> and <a rel="nofollow" href="https://github.com/FilipeamTeixeira" target="_blank">Filipe Teixeira</a>, and editor <a rel="nofollow" href="https://github.com/lmullen" target="_blank">Lincoln Mullen</a>.</p>
<p><code>spatsoc</code> started as a single function (what would eventually become <code>group_pts</code>) written by Alec in 2017 to help answer some of the questions that Quinn and Eric were asking about how animal social structure is related to spatial processes. These ideas were originally proposed by Quinn and Eric in their recent review paper <sup><a rel="nofollow" href="https://ropensci.org/blog/2018/12/04/spatsoc/#ref1" target="_blank">1</a></sup>. After our ideas were published, we began using this early function to determine when GPS collared caribou (<em>Rangifer tarandus</em>) in Newfoundland were recorded within 50 m of one another, within 5 minutes. This spatiotemporal grouping allowed us to build and analyze social association networks with <a rel="nofollow" href="https://sites.google.com/site/drfarine/r-packages/asnipe" target="_blank"><code>asnipe</code></a> and <a rel="nofollow" href="http://igraph.org/r/" target="_blank"><code>igraph</code></a>. Using animal telemetry data with social network analysis allowed us to draw new insights from a long term movement dataset.</p>
<div>
<img src="https://i2.wp.com/ropensci.org/img/blog-images/2018-12-04-spatsoc/Caribou_Juliana_Balluffi-Fry.jpg?w=70%25&ssl=1" data-recalc-dims="1"><figcaption>
<a rel="nofollow" href="https://www.instagram.com/juliana.wildlife/" target="_blank"><i></i></a><br>
<a rel="nofollow" href="https://twitter.com/balluffi" target="_blank"><i></i></a><br>
Juliana Ballufi-Fry<br>
</figcaption></div>
<p></p>
<h3>Social network analysis</h3>
<p>Social network analysis (SNA) is a powerful tool for measuring associations and relationships between individuals with implications for disease transmission, species interactions and community structure. SNA uses graph theory and networks to reconstruct social structures, where nodes represent individuals and edges represent the relationships or associations between them. Social networks built from <code>spatsoc</code> results are explicitly association networks, with the edges being defined by the spatiotemporal grouping of relocations as opposed to observed social interactions. Spiegel et al. <sup><a rel="nofollow" href="https://ropensci.org/blog/2018/12/04/spatsoc/#ref2" target="_blank">2</a></sup> showed that combining social network analysis with telemetry data of sleepy lizards can reveal insights about social structures in which the individuals and their behaviours that shape it are otherwise difficult to directly observe.</p>
<h3>Animal telemetry data</h3>
<p>The quantity and accessibility of animal movement data is increasing. A number of recent articles have suggested we are in a “golden age of animal tracking science” with large, extensive movement datasets being collected for a diversity of species, many of which are publicly available <sup><a rel="nofollow" href="https://ropensci.org/blog/2018/12/04/spatsoc/#ref3" target="_blank">3</a></sup>. <a rel="nofollow" href="https://www.movebank.org/node/2" target="_blank">Movebank</a> is a global data archive of animal movement data, with over 800 taxa and 1 billion relocations. It also hosts an archive of published datasets with accompanying DOI and licenses used in over 100 publications, backed by a formal review process to ensure data quality.</p>
<p>Animal telemetry technology is improving rapidly, as battery systems improve and sensors become smaller. Deployments of more advanced biologging devices on animals means less frequent captures to redeploy and more continuous data. <a rel="nofollow" href="https://www.icarus.mpg.de/" target="_blank">ICARUS</a> (the International Cooperation for Animal Research Using Space) is a collaborative project using GPS and 3D accelerometry loggers powered by solar panels and rechargeable batteries to produce continuous, high temporal and spatial resolution data. The goal of ICARUS is to use this data to better understand movement of animals and selecting forces that act on individuals and contribute to our understanding of health, disease epidemics and even potentially <a rel="nofollow" href="https://www.icarus.mpg.de/28810/animals-warning-sensors" target="_blank">predict volcanoes and earthquakes</a>!</p>
<div>
spatsoc allows us to answer the question: 
<p><b>when and where are individuals close to each other?</b>
</div>
<p>The spatiotemporal grouping of <code>spatsoc</code> can lead to many outcomes. We developed it to build proximity based social association networks from GPS relocation data but others may use it to measure species interactions or shared space use among con- or hetero-specifics. We are not restricted to dyadic interactions since <code>spatsoc</code> groups relocations for all individuals in space and time. spatsoc’s flexible functions let user define spatial and temporal grouping thresholds.</p>
<h4>Package installation</h4>
<p>Install the development version with <a rel="nofollow" href="https://github.com/r-lib/remotes" target="_blank"><code>remotes</code></a>.</p>
<pre><code>remotes::install_github('ropensci/spatsoc')

# or CRAN
install.packages('spatsoc')
</code></pre>
<h4>Temporal and point based spatial grouping</h4>
<p>For this example, we will use bison data from the “Dunn Ranch Bison Tracking Project” (Max Planck Institute and Nature Conservancy Missouri) available on Movebank <a rel="nofollow" href="https://www.movebank.org/panel_embedded_movebank_webapp?gwt_fragment=page%3Dsearch_map_linked%2CindividualIds%3D8070814%2B8070814%2B8070814%2B8070774%2B8070774%2B8070774%2B8070775%2B8070813%2B8070813%2B8070813%2B8070749%2B8057017%2B8070744%2B8070744%2B8070744%2B54137325%2B54137415%2B54137359%2B13848400%2B13848400%2B13848400%2B13848400%2B8070773%2B8070812%2B8070812%2B8070735%2B8070811%2Clat%3D40.45725700561854%2Clon%3D-94.10498513423153%2Cz%3D13" target="_blank">here</a>. We will import the downloaded data (CSV format) with <a rel="nofollow" href="https://github.com/Rdatatable/data.table" target="_blank"><code>data.table</code></a>’s <code>fread</code>. This function is fast when data gets big and <code>data.table</code> is the required format for all <code>DT</code> arguments in spatsoc.</p>
<p>Note: to download the data from Movebank you will need to make an account and accept the terms of the data. We are only going to use a subset of the available data for this example and we have renamed the downloaded file to <code>Bison.csv</code> for brevity. Lastly, select “Add UTM coordinates” on the download page to ensure we have projected coordinates ready for spatsoc.</p>
<pre><code># Load packages
library(spatsoc)
library(data.table)

# Read data as a data.table
DT &lt;- fread('Bison.csv')

# Cast the character column 'timestamp' as a POSIXct, saved as 'datetime'
# GMT/UTC timezone as specific in the data's README.txt
DT[, datetime := as.POSIXct(timestamp, tz = 'GMT')]

# Subset to 2015
DT &lt;- DT[year(datetime) == 2015]

# Temporal grouping with threshold of 5 minutes
group_times(DT = DT, datetime = 'datetime', threshold = '5 minutes')

# Spatial grouping with threshold of 25 meters
group_pts(DT = DT, threshold = 25, id = 'individual_local_identifier',
          coords = c('utm-easting', 'utm-northing'), timegroup = 'timegroup')
</code></pre>
<p>Behind the scenes, we are building a distance matrix and using the connected components to determine group membership.</p>
<pre><code>library(igraph)
distMatrix &lt;- as.matrix(dist(cbind(X, Y), method = &quot;euclidean&quot;))
graphAdj &lt;- graph_from_adjacency_matrix(distMatrix &lt;= threshold,
                                        mode = 'undirected', diag = FALSE)
groups &lt;- clusters(graphAdj)$membership
</code></pre>
<p>A distance matrix measures the spatial distance between all of the individuals. The spatial distance <code>threshold</code> defined by the user is used to turn the distance matrix into a boolean matrix.</p>
<div><img src="https://www.r-bloggers.com/img/blog-images/2018-12-04-spatsoc/dist-matrix.png"></div>
<p>This matrix is then passed to <code>igraph</code> to build a graph, and the connected components are extracted. The connected components represent the distinct, separate subgraphs that make up a graph. In this case, there are three connected components representing our spatiotemporal groups.</p>
<div><img src="https://www.r-bloggers.com/img/blog-images/2018-12-04-spatsoc/connected-components.png"></div>
<h3>Reflecting on the process</h3>
<p><code>spatsoc</code> was submitted to rOpenSci on July 16 2018 and accepted on October 26 2018. Based on feedback during the review process, the package documentation and usability were greatly improved. The rOpenSci onboarding process represented a different standard than CRAN, where test coverage, useful documentation and <a rel="nofollow" href="https://github.com/MangoTheCat/goodpractice" target="_blank"><code>goodpractice</code></a> were just as important as getting <code>0 errors | 0 warnings | 0 messages</code>. We really appreciated the common goal of improving the package shared by package authors, rOpenSci reviewers, and editor.</p>
<p>The package building process gave us a better understanding of how the functions and packages we use in our analyses are built. Using powerful tools like unit testing and CRAN checks we could ensure our code was well written. Alec was really surprised how much he enjoyed writing unit tests, thinking of all the different ways things could fail and setting up the necessary framework to prevent it, returning useful warnings and errors when it did.</p>
<p>In the future: more packages! For personal use, for fun, and for sharing new methods and tools with the R community. The flexibility and power of the package as a fundamental unit of sharing functions (and data, projects, etc.). Next time, we will write unit tests and documentation as we go (hopefully!). We are looking forward to engaging in more collaboration opportunities in the R community through the rOpenSci onboarding process as authors and reviewers or less formally through Pull Requests or Issue Boards.</p>
<h3>Resources</h3>
<p>Check out the <a rel="nofollow" href="https://github.com/ropensci/spatsoc" target="_blank">repository</a>, contribute to <a rel="nofollow" href="https://github.com/ropensci/spatsoc/issues" target="_blank">open issues</a> or <a rel="nofollow" href="https://github.com/ropensci/spatsoc/issues/new" target="_blank">open a new issue</a>! We have written three vignettes and created a <a rel="nofollow" href="https://spatsoc.gitlab.io/" target="_blank">website</a> using <a rel="nofollow" href="http://pkgdown.r-lib.org/" target="_blank">pkgdown</a>. If the documentation and these resources still leave you with questions about using the package, please contact us or create an issue.</p>
<p>We are always open to chat about our ideas, the research we are currently doing with spatsoc, and the research we plan to do in the future, so please let us know if you have any questions.</p>
<h3>References</h3>
<p><a name="ref1">1</a>: Webber, Q. M., &amp; Vander Wal, E. (2018). <a rel="nofollow" href="https://besjournals.onlinelibrary.wiley.com/doi/full/10.1111/1365-2656.12773" target="_blank">An evolutionary framework outlining the integration of individual social and spatial ecology.</a> <em>Journal of Animal Ecology</em>.</p>
<p><a name="ref2">2</a>: Spiegel, O., Sih, A., Leu, S. T., &amp; Bull, C. M. (2018). <a rel="nofollow" href="https://www.sciencedirect.com/science/article/pii/S0003347217303639" target="_blank">Where should we meet? Mapping social network interactions of sleepy lizards shows sex-dependent social network structure.</a> <em>Animal Behaviour</em>.</p>
<p><a name="ref3">3</a>: Kays, R., Crofoot, M. C., Jetz, W., &amp; Wikelski, M. (2015). <a rel="nofollow" href="http://science.sciencemag.org/content/348/6240/aaa2478" target="_blank">Terrestrial animal tracking as an eye on life and planet.</a> <em>Science</em>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ropensci.org/blog/2018/12/04/spatsoc/"> rOpenSci - open tools for open science</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/_Kpl8QQ6dB4" height="1"></a></div>