--- title: "Visualization" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Visualization} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup, message = FALSE} library(nett) library(igraph) ``` In this article, we go through some of the basic visualization functionality in the `nett` package. ## Visualizing a DCSBM Let us sample a network from a DCSBM: ```{r } 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: ```{r} 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) par(mar = original) ``` We can also plot the degree distribution: ```{r} nett::plot_deg_dist(gr) summary(igraph::degree(out$gr)) ``` ## A latent variable model 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: ```{r} 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: ```{r} 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) par(mar = original) ``` ```{r} nett::plot_deg_dist(gr) summary(igraph::degree(out$gr)) ``` ## Visualizing *Political Blogs* network Let us compare with *Political Blogs* network accessible via `polblogs`. ```{r} original = par("mar") par(mar = c(0,0,0,0)) out = nett::plot_net(polblogs, community = igraph::V(polblogs)$community) par(mar = original) ``` ```{r} nett::plot_deg_dist(polblogs) summary(igraph::degree(polblogs)) ```