knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = F, message = F )
library(enaR) library(network) library(sna)
This tutorial illustrates how to use the enaR
package to perform a selected structural analyses on an ecosystem model.
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'
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)
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)
The elements of the adjacency matrix are 1 if there is a direct connection from node i to j. Otherwise, the element is zero.
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
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).
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") degree(NET, cmode = "outdegree") degree(NET, cmode = "freeman") # total
enaStructure()
The enaR
package includes a function that extracts the adjacency matrix and calculates a number of network statistics.
s <- enaStructure(NET) attributes(s) show(s$A) show(s$ns)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.