Introduction

When looking at a social network in time, it is often important to ask how stable or similar is the network? This tutorial runs through the use of lagged network measures in netTS. We start by introducing cosine similarity (Newman (2010)), then introduce how to add custom functions.

The vignette is organised as follows:

  1. Network level similarity
  2. Node level similarity
  3. Custom functions for lagged measures

Libraries

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

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

1. Network level similarity

Here we use the netTS package to calculate network similarity over time. We look at the relative change from the first network over time at the network level.

#1. extract cosine measures at the network level: compared to first network
graph.cosine.first <- graphTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE,firstNet = TRUE ,measureFun= cosine_between_graphs)
## [1] "48 networks extracted"
#2. extract cosine measures at the network level: compared to the previous network
graph.cosine.prev <- graphTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE,firstNet = FALSE ,measureFun= cosine_between_graphs)
## [1] "48 networks extracted"
#plot the results
graphTS.plot(graph.cosine.first)

graphTS.plot(graph.cosine.prev)

2. Node level similarity

In what follows, we use the netTS package to calculate network similarity over time. We look at the relative change from the first network over time at the node level.

#1. extract cosine measures at the node level: compared to first network
node.cosine.first <- nodeTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE, firstNet=TRUE, measureFun= cosine_between_nodes)
## [1] "48 networks extracted"
#2. extract cosine measures at the node level: compared to the previous network
node.cosine.prev <- nodeTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE, firstNet=FALSE, measureFun= cosine_between_nodes)
## [1] "48 networks extracted"
#3. plot the results
nodeTS.plot(node.cosine.first)
## Warning: Removed 399 rows containing missing values (geom_path).

nodeTS.plot(node.cosine.prev)
## Warning: Removed 394 rows containing missing values (geom_path).

3. User specified functions for lagged comparisons

It is also possible to create a user specified lagged network function. As an example we use a function of the difference in mean strength between two networks.

#the function should have two networks as the inputs
my.lagged.measure <- function(net_t1, net_t2){

  #Measure between two networks
  diff.strength <- mean(strength(net_t2))-mean(strength(net_t1))
  
  #return value
  return(diff.strength)
  
}

Use the custom lagged function with netTS

#1. extract strength difference between network: compared to first network
graph.str.diff.first <- graphTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE,firstNet = TRUE ,measureFun= my.lagged.measure)
## [1] "48 networks extracted"
#2. extract strength difference between network: compared to previous network
graph.str.diff.prev <- graphTS(data = groomEvents, windowsize = days(30), windowshift = days(10), lagged = TRUE,firstNet = FALSE ,measureFun= my.lagged.measure)
## [1] "48 networks extracted"
#3. plot the strength differences from the first network
graphTS.plot(graph.str.diff.first)

graphTS.plot(graph.str.diff.prev)
## Warning: Removed 2 rows containing missing values (geom_path).
## Warning: Removed 2 rows containing missing values (geom_point).