library(netseg) library(igraph) requireNamespace("scales") knitr::opts_chunk$set( collapse = TRUE, comment = "#>", out.width = "100%", fig.width=10, fig.height=6 ) set.seed(666)
The following vignette demonstrates using the functions from package netseg [@r-netseg]. Two example datasets are described in the next section. Mixing matrices are described in section 2 and the measures are described in section 3. Please consult @bojanowski-corten-2014 for further details.
data(Classroom)
In the examples below we will use data Classroom
, a directed network in a classroom of r vcount(Classroom)
kids [@dolata2014]. Ties correspond to nominations from a survey question "With whom do you like to play with?". Here is a picture:
plot( Classroom, vertex.color = c("Skyblue", "Pink")[match(V(Classroom)$gender, c("Boy", "Girl"))], vertex.label = NA, vertex.size = 10, edge.arrow.size = .7 ) legend( "topright", pch = 21, legend = c("Boy", "Girl"), pt.bg = c("Skyblue", "Pink"), pt.cex = 2, bty = "n" )
For us it will be a graph $G =
Some measures are applicable only to an undirected network. For that purpose let's create an undirected network of reciprocated nominations in the Classroom
network and call it undir
:
undir <- as.undirected(Classroom, mode="mutual") plot( undir, vertex.color = c("Skyblue", "Pink")[match(V(undir)$gender, c("Boy", "Girl"))], vertex.label = NA, vertex.size = 10, edge.arrow.size = .7 ) legend( "topright", pch = 21, legend = c("Boy", "Girl"), pt.bg = c("Skyblue", "Pink"), pt.cex = 2, bty = "n" )
Mixing matrix is traditionally a two-dimensional cross-classification of edges depending on group membership of the adjacent nodes. A three-dimensional version of a mixing matrix cross-classifies all the dyads according to the following criteria:
Formally, mixing matrix is a matrix $M$ in which entry $m_{ghy}$ is a number of pairs of nodes such that
TRUE
if there is a tie, $y$ is FALSE
if there is no tieWe can compute the mixing matrix for the classroom network and attribute gender
with
the function mixingm()
. By default the traditional two-dimensional version is returned:
mixingm(Classroom, "gender")
Among other things we see that:
Supplying argument full=TRUE
the function will return an three-dimensional array cross-classifying the dyads:
m <- mixingm(Classroom, "gender", full=TRUE) m
We can analyze the mixing matrix as a typical frequency crosstabulation. For example:
round( prop.table(m, c(1,2)) * 100, 1)
round( prop.table(m[,,2], 1 ) * 100, 1)
In other words, boys are 95% of nominations of other boys, but only 11% of nominations of girls.
Function mixingm()
works also for undirected networks, values below the diagonal are always 0:
mixingm(undir, "gender") mixingm(undir, "gender", full=TRUE)
Most of the segregation indexes described below summarize the mixing matrix.
Function mixingdf()
returns the same data in the form of a data frame. For directed Classroom
network:
mixingdf(Classroom, "gender") mixingdf(Classroom, "gender", full=TRUE)
For undir
:
mixingdf(undir, "gender") mixingdf(undir, "gender", full=TRUE)
assort(Classroom, "gender") assort(undir, "gender")
Coleman's index compares the distribution of group memberships of alters with the distribution of group sizes. It captures the extent the nominations are "biased" due to the preference for own group.
coleman(Classroom, "gender")
Values are close to 1 (high segregation). The value for boys is greater than for girls, so girls nominated boys a bit more often than boys nominated girls.
ei(Classroom, "gender") ei(undir, "gender")
Is applicable to undirected networks with two groups.
Function freeman
:
freeman(undir, "gender")
gamix(Classroom, "gender") gamix(undir, "gender")
orwg(Classroom, "gender") orwg(undir, "gender")
smi(Classroom, "gender")
Values for vertices
(v <- ssi(undir, "gender"))
Plotted with grayscale (the more segregated the darker the color):
kol <- gray(scales::rescale(v, 1:0)) plot( undir, vertex.shape = c("circle", "square")[match(V(undir)$gender, c("Boy", "Girl"))], vertex.color = kol, vertex.label = V(undir), vertex.label.color = ifelse(apply(col2rgb(kol), 2, mean) > 125, "black", "white"), vertex.size = 15, vertex.label.family = "sans", edge.arrow.size = .7 )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.