library(nett)
library(igraph)
In this article, we go through some of the basic visualization
functionality in the nett
package.
Let us sample a network from a DCSBM:
= 1500
n = 4
Ktru = 15 # expected average degree
lambda = 0.1
oir = 1:Ktru
pri
set.seed(1234)
<- EnvStats::rpareto(n, 2/3, 3)
theta = pp_conn(n, oir, lambda, pri=pri, theta)$B
B = sample(Ktru, n, replace=T, prob=pri)
z
# sample the adjacency matrix
= sample_dcsbm(z, B, theta) A
We can plot the network using community labels \(z\) to color the nodes:
= par("mar")
original
= igraph::graph_from_adjacency_matrix(A, "undirected") # convert to igraph object
gr par(mar = c(0,0,0,0))
= nett::plot_net(gr, community = z) out
par(mar = original)
We can also plot the degree distribution:
::plot_deg_dist(gr) nett
summary(igraph::degree(out$gr))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 1.00 10.00 13.00 14.81 17.00 131.00
Now consider a latent variable model with \(K\) communities as follows: The adjacency matrix \(A = (A_{ij})\) is generated as a symmetric matrix, with independent Bernoulli entries above the diagonal with \[\begin{align}\label{eq:dclvm:def} \mathbb E [\,A_{ij} \mid x, \theta\,] \; \propto \; \theta_i \theta_j e^{- \|x_i - x_j\|^2} \quad \text{and} \quad x_i = 2 e_{z_i} + \frac34 w_i \end{align}\] where \(e_k\) is the \(k\)th basis vector of \(\mathbb R^d\), \(w_i \sim N(0, I_d)\), \(\{z_i\} \subset [K]^n\) are multinomial labels (similar to the DCSBM labels) and \(d = K\). The proportionality constant in~\(\eqref{eq:dclvm:def}\) is chosen such that the overall network has expected average degree \(\lambda\)
We can generate from this model using the
nett::sample_dclvm()
function as follows:
= Ktru
d = sample(Ktru, n, replace = T, prob = pri)
labels = sort(labels)
labels = diag(Ktru)
mu = 2*mu[labels, ] + 0.75*matrix(rnorm(n*d), n)
x
= sample_dclvm(x, lambda, theta) A
Visualizing the network and its degree distribution goes as before:
= par("mar")
original
= igraph::graph_from_adjacency_matrix(A, "undirected") # convert to igraph object
gr par(mar = c(0,0,0,0))
= nett::plot_net(gr, community = labels) out
par(mar = original)
::plot_deg_dist(gr)
nett#> Warning in nett::plot_deg_dist(gr): There are 0-degree nodes. Omitting them on
#> log scale.
summary(igraph::degree(out$gr))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 1.00 6.00 11.00 13.61 18.00 198.00
Let us compare with Political Blogs network accessible via
polblogs
.
= par("mar")
original
par(mar = c(0,0,0,0))
= nett::plot_net(polblogs, community = igraph::V(polblogs)$community) out
par(mar = original)
::plot_deg_dist(polblogs)
nett#> Warning in nett::plot_deg_dist(polblogs): There are 0-degree nodes. Omitting
#> them on log scale.
summary(igraph::degree(polblogs))
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> 0.00 1.00 8.00 25.62 33.00 468.00