Install netTS, load some libraries

#if netTS is not yet installed you can use the following code to install it from github:
#devtools::install_github("tbonne/netTS")

library(netTS)
library(lubridate)
library(ggplot2)
library(igraph)
library(reshape2)

Introduction

This vignette for the netTS package is meant to provide information on how to control for variation in sampling effort when extracting network measures over time. This vignette will first introduce two pre-built methods, and then introduce how a user specified function can be developed.

We use grooming data of vervet monkeys as an example:

head(groomEvents)
##     ID PartnerID                date
## 1 Laur      Malc 2015-07-01 12:32:19
## 2 Malc      Laur 2015-07-01 12:33:01
## 3 Ubun      Wall 2015-07-01 16:08:26
## 4 Wall      Ubun 2015-07-01 16:09:52
## 5 Dott      Jasm 2015-07-02 09:59:27
## 6 Jasm      Dott 2015-07-02 10:01:58

We will be using mean node strength for these examples:

#1. create a measurement function
mean_strength <- function (net) {
  md <- mean(strength(net))
    return(md)
}

Controlling for sampling effort: ad libitum sampling

This first method makes the assumption that during a sampling day effort between first and last samples taken is constant. Given this assumption it calculates the time spent sampling each day, using first and last sample times, and sums across all days within a window. By then dividing each edge by this effort this method returns the number of interactions / hours sampled.

#1. Extract measurements without correcting for sampling effort
net.time.noEffort <-graphTS(groomEvents, windowsize = days(30), windowshift = days(10), measureFun = mean_strength, directed=TRUE)
## [1] "48 networks extracted"
#2. Extract measurements correcting for sampling effort
net.time.yesEffort<-graphTS(groomEvents, windowsize = days(30), windowshift = days(10), measureFun = mean_strength, directed=TRUE, effortFun=effort.time)
## [1] "48 networks extracted"
#3. Plot the results
graphTS.plot(net.time.noEffort)

graphTS.plot(net.time.yesEffort)

Controlling for sampling effort: scan sampling

This second method makes the assumption that sampling is performed in bouts, e.g., scans. Given a dataframe with the number of scans per time period (e.g., day), this this method sums the total number of scans within a window and divides each edge by the number of scans. The input for this method is a dataframe with the date in the first column and the number of scans in the second, and an effort function: effort.scan.

#1. Extract measurements without correcting for sampling effort
net.time.noEffort <-graphTS(groomEvents, windowsize = days(30), windowshift = days(10), measureFun = mean_strength, directed=TRUE)
## [1] "48 networks extracted"
#2. Extract measurements with correcting for sampling effort
net.time.yesEffort<-graphTS(groomEvents, windowsize = days(30), windowshift = days(10), measureFun = mean_strength, directed=TRUE,effortFun=effort.scan,effortData = df.scans)
## [1] "48 networks extracted"
#3. Plot the results
graphTS.plot(net.time.noEffort)

graphTS.plot(net.time.yesEffort)

Controlling for sampling effort: custom functions

It is also possible to create a custom effort function, e.g.:

#1. create a function that takes a subset of my events dataframe and outputs a network corrected for the number of days sampled
my.effort.days <- function(df.window, directed=FALSE){
  
  #create a network from the data within this time window
  g <- create.a.network(df.window, directed)
  
  #calculate the number of unique days in this window
  numb.days <- length(unique(as.Date(df.window$date)))
  
  #correct the edge weights for total number of days
  E(g)$weight <- E(g)$weight/numb.days
  
  #add a graph attribute (optional, but sometimes useful)
  g <- set_graph_attr(g, "effort", numb.days)

  #return the network
  return(g)
  
}

Extract time series

#1. Extract measurements without correcting for sampling effort
net.time.noEffort <-graphTS(groomEvents, windowsize = days(60), windowshift = days(10), measureFun = mean_strength, directed=TRUE)
## [1] "45 networks extracted"
#2. Extract measurements with correcting for sampling effort: using my.effort.days function
net.time.yesEffort<-graphTS(groomEvents, windowsize = days(60), windowshift = days(10), measureFun = mean_strength, directed=TRUE,effortFun=my.effort.days)
## [1] "45 networks extracted"
#3. Plot the results
graphTS.plot(net.time.noEffort)

graphTS.plot(net.time.yesEffort)