Skip to content

Structure Analysis: Advanced

borretts edited this page Sep 21, 2017 · 1 revision

This tutorial illustrates how to use the enaR package to perform a selected structural analyses on an ecosystem model. As in previous tutorials, the first step is to prepare the workspace as follows

rm(list = ls())
library(enaR)
library(network)

Next we will load and select a model to analyze

# load data
data(enaModels)        # load library of Ecosystem Networks
names(enaModels)       # view model names
NET <- enaModels[[9]]  # select the Oyster Reef model (Dame & Patten 1981) and save it as NET
> NET%v%'vertex.names'
[1] "Filter Feeders"     "Microbiota"         "Meiofauna"         
[4] "Deposit Feeders"    "Predators"          "Deposited Detritus"

This gives us a model to analyze. If you have your own model to analyze, you can use it instead.

As a first step in our analysis, lets find the size of the network, the number of edges, its conductance or network density, and a metric called link density.

# --- NETWORK DESCRIPTIVE STATISTICS ---
# example analyses
n = network.size(NET)       # number of nodes
L = network.edgecount(NET)  # number of edges (links)
C = L/n^2                      # connectance
LD = L/n                       # link density (average links per node)

> ns = c("n"=n, "L"=L, "C"=C, "LD"=LD)            # create a summary vector of the network statistics
> show(ns)
         n          L          C         LD 
 6.0000000 12.0000000  0.3333333  2.0000000 

This tells us that the Oyster Reef model has 6 nodes and an edge density of 0.33. On average, there are two edges per node in the network. We can also extract the Adjacency matrix from the model as follows

> A <- as.matrix(NET)            # get adjacency matrix
> show(A)
                   Filter Feeders Microbiota Meiofauna Deposit Feeders
Filter Feeders                  0          0         0               0
Microbiota                      0          0         1               1
Meiofauna                       0          0         0               1
Deposit Feeders                 0          0         0               0
Predators                       0          0         0               0
Deposited Detritus              0          1         1               1
                   Predators Deposited Detritus
Filter Feeders             1                  1
Microbiota                 0                  0
Meiofauna                  0                  1
Deposit Feeders            1                  1
Predators                  0                  1
Deposited Detritus         0                  0

The elements of the adjacency matrix are 1 if there is a direct connection from node i to j. Otherwise, the element is zero.

Distance

We can use tools from the network package to learn more about the graph. For example we can find the geodesic distance between each of the nodes (ignoring edge weights).

> # -- DISTANCE ---
> geodist(NET)       # returns counts of geodesics and length of geodesics
$counts
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    1    1    1
[2,]    0    1    1    1    1    2
[3,]    0    1    1    1    1    1
[4,]    0    1    1    1    1    1
[5,]    0    1    1    1    1    1
[6,]    0    1    1    1    1    1

$gdist
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    2    2    2    1    1
[2,]  Inf    0    1    1    2    2
[3,]  Inf    2    0    1    2    1
[4,]  Inf    2    2    0    1    1
[5,]  Inf    2    2    2    0    1
[6,]  Inf    1    1    1    2    0

This tells us that the maximum geodesic distance from nodes 2, 3, 4, 5, and 6 to the others is a walk of length 2, but that node 1 is unreachable from the other nodes. While there is a single geodesic between most of the nodes, there are two walks of length 2 from node 2 (Microbiota) to node 6 (Deposited Detritus).

Degree

We can also find the unweighted node degree (positional importance). As this is a directed network, we can find the input, output, and total degree

 > degree(NET, cmode = "indegree")
[1] 0 1 2 3 2 4  
> degree(NET, cmode = "outdegree")
[1] 2 2 2 2 1 3
> degree(NET, cmode = "freeman")  # total
[1] 2 3 4 5 3 7

** Using enaStructure() **

The enaR package includes a function that extracts the adjacency matrix and calculates a number of network statistics.

> s <- enaStructure(NET)
> attributes(s)
$names    
[1] "A"  "ns"
> show(s$A)
                   Filter Feeders Microbiota Meiofauna Deposit Feeders
Filter Feeders                  0          0         0               0
Microbiota                      0          0         1               1
Meiofauna                       0          0         0               1
Deposit Feeders                 0          0         0               0
Predators                       0          0         0               0
Deposited Detritus              0          1         1               1
                   Predators Deposited Detritus
Filter Feeders             1                  1
Microbiota                 0                  0
Meiofauna                  0                  1
Deposit Feeders            1                  1
Predators                  0                  1
Deposited Detritus         0                  0
> show(s$ns)
     n  L         C LD      ppr    lam1A mlam1A      rho         R        d
[1,] 6 12 0.3333333  2 2.147899 2.147899      1 2.147899 0.4655712 0.147899
     no.scc no.scc.big      pscc
[1,]      2          1 0.8333333