--- title: "Introduction to multigraphr" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Introduction to multigraphr} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(out.width = "100%", cache = FALSE ) ``` This vignette serves as an introduction to the package `multigraphr`. Parts of the theoretical background is provided but for more details, consult the following literature which the package is based on: > Shafie, T. (2015). A multigraph approach to social network analysis. *Journal of Social Structure*, 16. [Link](https://www.doi.org/10.21307/joss-2019-011) > Shafie, T. (2016). Analyzing local and global properties of multigraphs. *The Journal of Mathematical Sociology*, 40(4), 239-264. [Link](https://doi.org/10.1080/0022250X.2016.1219732) > Shafie, T., Schoch, D. (2021) Multiplexity analysis of networks using multigraph representations. *Statistical Methods & Applications* 30, 1425–1444. [Link](https://doi.org/10.1007/s10260-021-00596-0) > Shafie, T. (2023). Goodness of fit tests for random multigraph models, *Journal of Applied Statistics*, 50:15, 3062-3087. [Link](https://doi.org/10.1080/02664763.2022.2099816) Make sure the library is loaded ```{r eval=TRUE} library('multigraphr') ``` ## Multigraphs and applicability Multigraphs are network representations in which multiple edges and edge loops (self edges) are permitted. These data structures can be either directly observed or aggregated by classifying or cross-classifying node attributes into meta nodes. For the latter case, within group edges correspond to self-edges. See example below where the original graph with 15 nodes and 12 edges (left) is aggregated based on node categories into a small multigraph with 4 nodes (right). Edge aggregation can also be used to obtain multigraphs. Assume that we study a graph with three different types of relations over three periods of time: If we aggregate over time periods, we obtain for each edge category a multigraph for the total time period of three days: For more details on these kinds of aggregations, see Shafie (2015;2016). ## Multigraph representation of network data Multigraphs are represented by their edge multiplicity sequence $$\mathbf{M} = (M_{ij} : (i,j) \in \cal{R} )$$ where $\cal{R}$ is the canonical site space for undirected edges $$R = \lbrace (i,j) : 1 \leq i \leq j \leq n \rbrace$$ i.e. $$(1,1) < (1,2) <···< (1,n) < (2,2) < (2,3) <···< (n,n)$$ where $n$ is number of nodes. The number of vertex pair sites is given by $\displaystyle r = \binom{n+1}{2}$. Edge multiplicities can also be represented as entries in a matrix $$\mathbf{M}= \begin{bmatrix} M_{11} & M_{12} & \dots & M_{1n} \\ 0 & M_{22} & \dots & M_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0& \ldots & M_{nn} \end{bmatrix} \qquad \mathbf{M} + \mathbf{M'}= \begin{bmatrix} 2M_{11} & M_{12} & \dots & M_{1n} \\ M_{12} & 2M_{22} & \dots & M_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ M_{1n} & M_{2n} & \ldots & 2M_{nn} \end{bmatrix}$$ where the right hand matrix is the equivalence of an adjacency matrix of a multigraph (Shafie, 2016). ## Random multigraph models Two probability models for generating undirected random multigraphs are implemented in the package together with several statistics under these two models. Moreover, functions for goodness of fit tests are available for the presented models. Note that some of the functions are only practical for small scale multigraphs (with number of nodes less than 10 and number of edges less than 20). ### Random stub matching model for multigraphs The first model is obtained by random stub matching (RSM) given observed degree sequence of a multigraphs, so that edges are assigned to sites given fixed degree sequence $\mathbf{d}=(d_1, \ldots, d_n)$. The edge assignment probability sequence $\mathbf{Q}$ is defined as a function of these degrees and the edge assignment probabilities are given by \begin{equation} Q_{ij}= \left\{ \begin{array}{ll} \displaystyle \binom{d_i}{2}\bigg/ \binom{2m}{2} & \mbox{for $i=j$}\\ \displaystyle d_id_j\bigg/ \binom{2m}{2} & \mbox{for $i0$ and $\sum_{i=1}^np_i = 1$. Thus, \mathbf{d} is the outcome of a random degree sequence that is multinomial distributed with parameters $2m$ and $\mathbf{p}=\mathbf{d}$. Then it follows that the multiplicity sequence $\mathbf{M}$ has an IEA distribution with edge probability sequence $\mathbf{Q}(\mathbf{p})$. For the RSM approximation, $\mathbf{p}=\mathbf{d}/2m$. The relations between the models are summarized in the below figure: #### Example The function `iea_model` has both versions of the IEA model implemented and can be specified to approximate the RSM model. Consider using the IEA model to approximate the RSM model so that edge assignment probabilities are functions of observed degree sequence. Note that the sample space for multigraphs is much bigger than for the RSM model so the multiplicity sequences are not printed (they can be found using the function `get_edgemultip_seq` for very small multigraphs and their probabilities can be found using the multinomial distribution). The following shows the number of multigraphs under either of the two IEA models: ```{r ieas_ex1, include=TRUE, results='markup', message=FALSE} ieas_1 <- iea_model(adj = A , type = 'graph', model = 'IEAS', K = 0, apx = TRUE) isa_1 <- iea_model(adj = A , type = 'graph', model = 'ISA', K = 0, apx = TRUE) isa_1$nr.multigraphs ieas_1$nr.multigraphs ``` The logical parameter `apx` determines whether the IEA model is being used as an approximation to the RSM model and the parameter `model` specifies which IEA model is used for the approximation. The IEA models can also be used independent of the RSM model. For example, the IEAS model can be used where edge assignment probabilities are estimated using the observed edge multiplicities (maximum likelihood estimates): ```{r ieas_ex3, include=TRUE, results='markup', message=FALSE} ieas_2 <- iea_model(adj = A , type = 'graph', model = 'IEAS', K = 0, apx = FALSE) ``` The ISA model can also be used independent of the RSM model. Then, a sequence with the stub assignment probabilities (for example based on prior belief) should be given as argument: ```{r isa_ex2, include=TRUE, results='markup', message=FALSE} isa_2 <- iea_model(adj = A , type = 'graph', model = 'ISA', K = 0, apx = FALSE, p.seq = c(1/3, 1/3, 1/3)) ``` ### Statistics to analyze structural properties Several statistics for analyzing the structural properties of multigraphs under the different models are implemented in the package. These include number of loops denoted $M_1$ and number of non-loops denoted $M_2$ (indicator of e.g. homophily and heterophily). Other statistics which are only implemented for the IEA model are those part of a so called complexity sequence with the distribution of edge multiplicities. This sequence is given by $\mathbf{R}=(R_0, R_1, R_2, \ldots, R_m)$ where \begin{equation} R_k = \sum \sum_{i **(3) Testing a composite IEAS hypothesis against a RSM model with degree sequence (14,2,2,2)**: ```{r gof3, include=TRUE, results='markup', message=FALSE, eval=FALSE} gof3 <- gof_sim(m = 10, model = 'RSM', deg.mod = c(14,2,2,2), hyp = 'IEAS', deg.hyp = 0) ``` **(4) Testing a composite ISA hypothesis against a ISA model with degree sequence (14,2,2,2)**: ```{r gof4, include=TRUE, results='markup', message=FALSE, eval=FALSE} gof4 <- gof_sim(m = 10, model = 'ISA', deg.mod = c(14,2,2,2), hyp = 'ISA', deg.hyp = 0) ``` The non-null (`gof3`) and null (`gof4`) distributions of the test statistics can then be visualized as shown above to check their fit to the asymptotic $\chi^2$-distribution. ## Performing the goodness of fit test on your data Use function `gof_test` to test whether the observed data follows IEA approximations of the RSM model. The null hypotheses can be simple or composite, although the latter is not recommended for small multigraphs as it is difficult to detect a false composite hypothesis under an RSM model and under IEA models (this can be checked and verified using `gof_sim` to simulate these cases). Non-rejection of the null implies that the approximations fit the data, thus implying that above statistics under the IEA models can be used to further analyze the observed network. Consider the following multigraph from the well known Florentine family network with marital. This multigraphs is aggregated based on the three actor attributes wealth (W), number of priorates (P) and total number of ties (T) which are all dichotomized to reflect high or low economic, political and social influence (details on the aggregation can be found in Shafie, 2015):
The multiplicity sequence represented as an upper triangular matrix for this mutigrpah is given by ```{r flor1, include=TRUE, results='markup', message=FALSE} flor_m <- t(matrix(c (0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 5, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1), nrow= 8, ncol=8)) ``` The equivalence of adjacency matrix for the multigraph is given by ```{r flor2, include=TRUE, results='markup', message=FALSE} flor_adj <- flor_m+t(flor_m) flor_adj ``` with the diagonal representing the loops double counted (Shafie, 2016). The function `get_degree_seq` can now be used to find the degree sequence for this multigraph: ```{r flor3, include=TRUE, results='markup', message=FALSE} flor_d <- get_degree_seq(adj = flor_adj, type = 'multigraph') flor_d ``` Now we test whether the observed network fits the IEAS or the ISA model. The $p$-values for testing whether there is a significant difference between observed and expected edge multiplicity values according to the two approximate IEA models are given in the output tables below. Note that the asymptotic $\chi^2$-distribution has $r-1 = \binom{n+1}{2} - 1 =35$ degrees of freedom. ```{r flor4, include=TRUE, results='markup', message=FALSE} flor_ieas_test <- gof_test(flor_adj, 'multigraph', 'IEAS', flor_d, 35) flor_ieas_test ``` ```{r flor5, include=TRUE, results='markup', message=FALSE} flor_isa_test <- gof_test(flor_adj, 'multigraph', 'ISA', flor_d, 35) flor_isa_test ``` The results show that we have strong evidence for the null such that we fail to reject it. Thus, there is not a significant difference between the observed and the expected edge multiplicity sequence according on the two IEA models. Statistics derived under these models presented above can thus be used to analyze the structure of these multigraphs. ## Other functions in the package and their usage The function `nsumk` is useful for finding all possible degree sequences for a network with $n$ nodes and $k/2$ number of edges, or for finding all possible edge multiplicity sequence $n$ that sum up to $k$ number of edges. #### Example All edge multiplicity sequences/multigraph with 2 nodes and 4 edges ```{r func1, include=TRUE, results='markup', message=FALSE} r <- (2*3)/2 # vertex pair sites (or length of edge multiplicity sequences) mg <- nsumk(r,4) # number of rows give number of possible multigraphs mg ``` The function `get_edge_assignment_probs` can be used to calculates the edge assignment probabilities $\mathbf{Q}$ given specified degree sequence under the two ways in which the RSM model can be approximated by the IEA model. #### Example Under the IEAS model with 10 possible vertex pair sites (4 nodes), the edge assignment probabilities are given by ```{r func2, include=TRUE, results='markup', message=FALSE} Q <- get_edge_assignment_probs(m = 8, deg.seq = c(4,4,4,4), model = 'IEAS') Q ``` Given a degree sequence, the function `get_edge_multip_seq` can be used to find all unique edge multiplicity sequences as rows in a data frame. Each row in the data frame represents a unique multigraph given the degree sequence. This is useful if you need to find the full outcome space of multgraphs under the IEA model (but only practical and readable for small multigraphs) #### Example To find all multigraphs with degree sequence $(4,2,2)$ ```{r func3, include=TRUE, results='markup', message=FALSE} mg <- get_edge_multip_seq(deg.seq = c(4,2,2)) mg ```