In this article, we go through some of the basic visualization
functionality in the nett
package.
Let us sample a network from a DCSBM:
n = 1500
Ktru = 4
lambda = 15 # expected average degree
oir = 0.1
pri = 1:Ktru
set.seed(1234)
theta <- EnvStats::rpareto(n, 2/3, 3)
B = pp_conn(n, oir, lambda, pri=pri, theta)$B
z = sample(Ktru, n, replace=T, prob=pri)
# sample the adjacency matrix
A = sample_dcsbm(z, B, theta)
We can plot the network using community labels z to color the nodes:
original = par("mar")
gr = igraph::graph_from_adjacency_matrix(A, "undirected") # convert to igraph object
par(mar = c(0,0,0,0))
out = nett::plot_net(gr, community = z)
We can also plot the degree distribution:
Now consider a latent variable model with K communities as follows: The adjacency matrix A = (Aij) is generated as a symmetric matrix, with independent Bernoulli entries above the diagonal with where ek is the kth basis vector of ℝd, wi ∼ N(0, Id), {zi} ⊂ [K]n are multinomial labels (similar to the DCSBM labels) and d = K. The proportionality constant in~ is chosen such that the overall network has expected average degree λ
We can generate from this model using the
nett::sample_dclvm()
function as follows:
d = Ktru
labels = sample(Ktru, n, replace = T, prob = pri)
labels = sort(labels)
mu = diag(Ktru)
x = 2*mu[labels, ] + 0.75*matrix(rnorm(n*d), n)
A = sample_dclvm(x, lambda, theta)
Visualizing the network and its degree distribution goes as before:
original = par("mar")
gr = igraph::graph_from_adjacency_matrix(A, "undirected") # convert to igraph object
par(mar = c(0,0,0,0))
out = nett::plot_net(gr, community = labels)
nett::plot_deg_dist(gr)
#> Warning in nett::plot_deg_dist(gr): There are 0-degree nodes. Omitting them on
#> log scale.
Let us compare with Political Blogs network accessible via
polblogs
.
original = par("mar")
par(mar = c(0,0,0,0))
out = nett::plot_net(polblogs, community = igraph::V(polblogs)$community)
#> This graph was created by an old(er) igraph version.
#> ℹ Call `igraph::upgrade_graph()` on it to use with the current igraph version.
#> For now we convert it on the fly...
nett::plot_deg_dist(polblogs)
#> Warning in nett::plot_deg_dist(polblogs): There are 0-degree nodes. Omitting
#> them on log scale.