Simulate and Sample from Ecological Interaction Networks

Randomly generate a wide range of interaction networks with specified size, average degree, modularity, and topological structure. Sample nodes and links from within simulated networks randomly, by degree, by module, or by abundance. Simulations and sampling routines are implemented in 'FORTRAN', providing efficient generation times even for large networks. Basic visualization methods also included. Algorithms implemented here are described in de Aguiar et al. (2017) .


Travis-CI BuildStatus AppVeyor BuildStatus CoverageStatus CRAN_Status_Badge lifecycle downloads DOI

EcoNetGen lets you randomly generate a wide range of interaction networks with specified size, average degree, modularity, and topological structure. You can also sample nodes and links from within simulated networks randomly, by degree, by module, or by abundance. Simulations and sampling routines are implemented in ‘FORTRAN’, providing efficient generation times even for large networks. Basic visualization methods also included. Algorithms implemented here are described in de Aguiar et al. (2017) arXiv:1708.01242.

EcoNetGen is now on CRAN and can be installed in the usual way:

install.packages("EcoNetGen")

See NEWS for a list of the most recent changes to the development version and current CRAN release. You can install the current development version of EcoNetGen from GitHub with:

# install.packages("devtools")
devtools::install_github("cboettig/EcoNetGen")

This way requires you have a recent FORTRAN compiler avialble on your machine.

Randomly generate networks

This is a basic example which generates a network. See ?netgen for documentation describing the parameter arguments. Setting verbose = FALSE (default) surpresses the output summary message.

library(EcoNetGen)
set.seed(123456) # for a reproducible simulation
 
network <- netgen(net_size = 150,
                  ave_module_size = 20, 
                  min_module_size = 10,
                  min_submod_size = 5,
                  net_type = "bi-partite nested",
                  ave_degree = 10,
                  verbose = TRUE
                  ) 
#> 
#> module count = 8 
#> average degree = 6.08 
#> average module size = 18.75 
#> number of components = 1 
#> size of largest component = 150

We can plot the resulting igraph as an adjacency matrix:

adj_plot(network)

Network igraph objects can also be plotted using the standard igraph plotting routines, for example:

library(igraph)
plot(network, vertex.size= 0, vertex.label=NA, 
     edge.color = rgb(.22,0,1,.02), vertex.shape="none", 
     edge.curved =TRUE, layout = layout_with_kk)

Sample from a network

set.seed(123456) # for a reproducible random sampling
sampled <- netsampler(network, 
                     key_nodes_sampler = "degree", 
                     neighbors_sampler = "random",
                     n_key_nodes = 50,
                     n_neighbors = 0.5 # 50%
                    )

We can plot the adjacency network, coloring red the sampled nodes. Note that adj_plot objects are just ggplot graphs (geom_raster) under the hood, and can be modified with the usual ggplot arguments, such as adding a title and changing the color theme here.

library(ggplot2) # needed to modify plot
 
adj_plot(sampled) + 
  ggtitle("Adjacency matrix of sampled vs full network") + 
  scale_fill_manual(values = c("#ED4E33", "#3B7EA1"))

Don’t forget to check out the ggraph package, which isn’t required for EcoNetGen but provides a lot of additional great ways to plot your network. Here we plot the simulated network color-coding the sampled nodes and edges (indicated by the label “sampled” on vertices and edges):

library(ggraph)
ggraph(sampled, layout = 'kk') +
        geom_edge_link(aes(color = label), alpha=0.4) +
        geom_node_point(aes(color = label)) +
        theme_graph() + 
        scale_color_manual(values = c("#ED4E33", "#3B7EA1")) + 
        scale_edge_color_manual(values = c("#ED4E33", "#3B7EA1"))

Or extract and plot just the sampled network:

subnet <- subgraph.edges(sampled, 
                         E(sampled)[label=="sampled"])
 
ggraph(subnet, layout = 'graphopt') +
        geom_edge_link(alpha=0.4) +
        geom_node_point() +
        theme_graph() 

A few example statistics

And we can compute common statistics from igraph as well. Here we confirm that clustering by “edge betweeness” gives us the expected number of modules:

community <- cluster_edge_betweenness(as.undirected(network))
length(groups(community))
#> [1] 9

We can check the size of each module as well:

module_sizes <- sizes(community)
module_sizes
#> Community sizes
#>  1  2  3  4  5  6  7  8  9 
#> 18 19 31 19 22  7  3 20 11

Average degree:

mean(degree(as.undirected(network)))
#> [1] 6.08

We can also label and plot the cluster membership:

V(sampled)$module <- as.character(membership(community))
ggraph(sampled, layout = 'kk') +
  geom_edge_link(alpha=0.1) +
  geom_node_point(aes(colour = module)) + 
  theme_graph()

News

NetGen 0.2.2

  • Avoid potential crashes from FORTRAN code by intercepting any invalid parameter combinations and returning a helpful error message. Please note, this changes the default parameter settings of min_module_size and ave_degree, which involved a potentially combination for scale-free and thus also some mixed-type networks.

NetGen 0.2.1

  • fix inappropriate default in netgen_v1 routine, and add assertion to check argument lengths.

NetGen 0.2.0

  • Add netsampler() routine

  • Modify user interface for netgen() to provide more self-explanatory argument names. Note: the old behaviour can be recovered for previous scripts you don't want to update manually by doing netgen <- EcoNetGen:::netgen_v1 after loading EcoNetGen.

  • Ensure that FORTRAN correctly obeys set.seed() by always using the R random number generator. #6

NetGen 0.1.1 2018-04-10

  • Initial release to CRAN

NetGen 0.1.0 2018-04-04

  • Added a NEWS.md file to track changes to the package.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("EcoNetGen")

0.2.2 by Carl Boettiger, 6 months ago


https://github.com/cboettig/EcoNetGen


Report a bug at https://github.com/cboettig/EcoNetGen/issues


Browse source code at https://github.com/cran/EcoNetGen


Authors: Marcus de Aguiar [aut, cph] , Erica Newman [aut] , Mathias Pires [aut] , NIMBioS [fnd] , Carl Boettiger [aut, cre]


Documentation:   PDF Manual  


GPL-3 license


Imports igraph, ggplot2

Suggests spelling, testthat, covr, ggraph


See at CRAN