<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/"></div></div><div><a data-url="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/" data-counturl="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/" data-count="horizontal" data-text="Collapsing a bipartite co-occurrence network" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://f.briatte.org/r/collapsing-a-bipartite-co-occurrence-network"> R / Notes</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This note is a follow-up to the <a target="_blank" href="http://f.briatte.org/r/turning-keywords-into-a-co-occurrence-network">previous one</a>. It shows how to use student-submitted keywords to find clusters of shared interests between the students.</p>
<h2>Dear students</h2>
<p>If you enjoyed my <a target="_blank" href="http://f.briatte.org/r/turning-keywords-into-a-co-occurrence-network">previous note</a>, this one might also entertain you. And since your real first names are used in the data, you should be able to tell me later if this note makes sense with regards to the true clusters (i.e. student groups) that you formed in my class.</p>
<p><strong>tl;dr</strong> – This note explains how to plot this graph in less than 70 lines of code:</p>
<p><img alt="bordered" src="https://i1.wp.com/f.briatte.org/r/images/collapsing-a-bipartite-co-occurrence-network-clusters.png?w=456" data-recalc-dims="1"></p>
<h2>The data: Your keywords</h2>
<p>Let’s start by loading <a target="_blank" href="https://gist.github.com/briatte/fb1ca2d10253886ac33ea749aa95f82c#file-00_keywords-txt">the same dataset</a> that we used to plot a co-occurrence network out of your research interests:</p>
<p></p>
<p>We will start by extracting your first names, which, very conveniently, are unique (there are no duplicate first names in the data):</p>
<p></p>
<p>The result of that operation is simply a vector of your 17 first names in alphabetical order, because we sorted the data on the <code>name</code> when we imported it.</p>
<p>Let’s now obtain a vector of all the different keywords that you submitted, following the same separation rule as used in the <a target="_blank" href="http://f.briatte.org/r/turning-keywords-into-a-co-occurrence-network">previous note</a>: keywords separated by a comma <em>or</em> by the “and” conjunction, as in “Sexuality and Gender”, will be treated as discrete (separate) entities. The code for that operation is a bit more obscure:</p>
<p></p>
<p>The result of that operation is a vector of 34 alphabetically-sorted keywords, starting with the following ones:</p>
<blockquote>
<p>Collective Action, Community Empowerment, Community/Neighborhood, Conflict, …</p>
</blockquote>
<p>We are now going to build a matrix with as many rows as there are students ($n = 17$), and as many columns as there are distinct keywords in the complete data ($k = 34$). The matrix, which will be a non-square, asymmetrical matrix, will serve as the basis for the bipartite (or two-mode) network that we are going to build.</p>
<h2>The concept: Bipartite/two-mode networks</h2>
<p>A two-mode network is a network in which there are two distinct kinds of entities (<em>modes</em>). These networks are fairly common in the social sciences: just think of a network formed by individuals who are members of groups, such as trade unions, political parties or Facebook groups.</p>
<p>The two different “modes” of a two-mode network are referred to as the “primary” and “secondary” modes. In the case at hand, the primary mode will be you, the students, and the secondary mode will be the keywords that you selected as your research interests. The ordering of the modes is arbitrary: it is just common practice to assign human beings (actors) as the primary mode.</p>
<h3>The incidence matrix</h3>
<p>The matrix that underlies a two-mode network is called an <a target="_blank" href="https://en.wikipedia.org/wiki/Incidence_matrix">incidence matrix</a>. If there were only 3 students in the class, and if these students had listed only 5 different keywords, the matrix might have looked like this:</p>
<p>$$<br>
\begin{array}{lc}<br>
\ A = &amp; \left(\begin{array}{}<br>
1 &amp; 1 &amp; 0 &amp; 0 &amp; 0 \\<br>
0 &amp; 1 &amp; 1 &amp; 0 &amp; 1 \\<br>
0 &amp; 0 &amp; 0 &amp; 1 &amp; 1 \\<br>
\end{array}\right)<br>
\end{array}<br>
$$</p>
<p>This example matrix indicates that Student 1, shown on the first row, has listed Keywords 1 and 2 as his/her research interests. Student 2, on the second row, listed Keywords 2, 3 and 5, and Student 3, on the last row, listed Keywords 4 and 5.</p>
<p>This matrix has several properties relevant to network construction, but the only one you need to remember for now is that it is a binary matrix: it contains $0$ when a student did not select a given keyword, and $1$ otherwise.</p>
<h3>Building the matrix</h3>
<p>Creating a binary incidence matrix from our data only takes a single, and admittedly quite esoteric, line of R code:</p>
<p></p>
<p>The crucial part of the code is where it is reads <code>k %in% x</code>, which tests the $k \in x$ logical statement where $k$ are all distinct keywords in the data, and $x$ is each student’s selection of keywords.</p>
<p>The result of the entire operation is a binary incidence matrix like the one shown in the previous section, except with $n = 17$ rows and $k = 34$ keywords. And just to make sure that we understand the data structure, let’s label the rows and columns of that matrix with the student names and the keywords:</p>
<p></p>
<h2>Visualizing the two-mode network</h2>
<p>Let’s now throw that matrix to the same <a target="_blank" href="http://f.briatte.org/r/ggnetwork-network-geometries-for-ggplot2">network visualization function</a> that I used in the previous note. The code for the visualization is, again, quite esoteric, because I want to show students and keywords in different graphical styles:</p>
<p></p>
<p>And here’s our two-mode network, shown in all its glory, with students in bold blue and keywords in light grey:</p>
<p><img alt="bordered" src="https://i1.wp.com/f.briatte.org/r/images/collapsing-a-bipartite-co-occurrence-network-bipartite.png?w=456" data-recalc-dims="1"></p>
<p>In this graph, students are connected to other students by the keywords that they both listed as research interests. Note that there are a few parts of the graph where it looks like students are directly connected to each other, but this is <em>not</em> the case: in a two-mode network like ours, nodes of the same mode <em>never</em> connect directly to each other.</p>
<p>The network conveys useful information about some keywords, like Gender, Migration and Environment, which act as bridges between several students. It also shows a group of densely connected students on the right, and several students, like Gabriella, Alexander and Felicitas, in peripheral positions on the left, as their choice of keywords more rarely matched with that of other students.</p>
<h2>Going back to a one-mode network</h2>
<p>Let’s now use the information of this two-mode network to connect students to each other. This time, we want to obtain a square, symmetrical matrix that would look like this if there were only 3 students in the class:</p>
<p>$$<br>
\begin{array}{lc}<br>
\ B = &amp; \left(\begin{array}{}<br>
2 &amp; 1 &amp; 0 \\<br>
1 &amp; 3 &amp; 1 \\<br>
0 &amp; 1 &amp; 2 \\<br>
\end{array}\right)<br>
\end{array}<br>
$$</p>
<p>This matrix actually contains the same information as the first one that I showed, only in different form: it has the 3 students on both rows and columns, and indicates, for instance, that Student 1 and Student 2 share one common keyword. You can read that information on the first row, second column, or on the second row, first column.</p>
<p>In this matrix, the diagonal indicates the total number of keywords listed by each student (or, if you prefer, the number of keywords that they share with themselves). Since that information is meaningless for our purposes of connecting students to each other, we will later discard it.</p>
<p>We are, however, interested in the other values of the matrix, and even though all these other values are equal to either $0$ or $1$ in my example, that might not be the case in the data, as some students might share more than one keyword with each other.</p>
<p>In other words, what we want here is to obtain a <em>weighted, one-mode <a target="_blank" href="https://en.wikipedia.org/wiki/Adjacency_matrix">adjacency matrix</a></em> of students, with the weights indicating the number of keywords that they share with each other. Note that we could perhaps find <a target="_blank" href="http://f.briatte.org/r/weighting-authorship-networks">better ways</a> to weigh that matrix, but let’s keep it simple and stick with the simplest weighting structure.</p>
<p>How do we do that, starting with the incidence matrix $A$ that we created in a previous section? <a target="_blank" href="https://solomonmessing.wordpress.com/2012/09/30/working-with-bipartiteaffiliation-network-data-in-r/">Here’s the trick</a>, in the words of <a target="_blank" href="https://twitter.com/SolomonMg/">Solomon Messing</a>:</p>
<blockquote>
<p>To convert a two-mode incidence matrix to a one-mode adjacency matrix, one can simply multiply an incidence matrix by its transpose.</p>
</blockquote>
<p>Or, in mathematical notation, to get the one-mode adjacency matrix $B$ of students, all we need to do is to perform $AA’$ on the two-mode incidence matrix of students and keywords. The only condition for that operation to work properly is that students need to be located on the rows of the incidence matrix, which is the case.</p>
<p>So let’s do that, and then convert the result to an <a target="_blank" href="http://igraph.org/"><code>igraph</code></a> network object:</p>
<p></p>
<p>… And we are back with a one-mode network object, only this time, in contrast to the network shown in the <a target="_blank" href="http://f.briatte.org/r/turning-keywords-into-a-co-occurrence-network">previous one</a>, the nodes of the networks are students, not keywords. We could plot the network straight away to show that, but let’s add a final twist to our analysis.</p>
<h2>Adding clusters, a.k.a community detection</h2>
<p>Since what we have in our network are students connected by 0, 1 or more keywords, let’s try to group the students together based on that information.</p>
<p>In statistical jargon, finding groups of things based on some properties of these things is called <a target="_blank" href="https://en.wikipedia.org/wiki/Cluster_analysis">cluster analysis</a>. In network theory, the equivalent is called <a target="_blank" href="https://en.wikipedia.org/wiki/Community_structure">community</a> detection, and one commonly used method to find communities in networks like ours is called the “<a target="_blank" href="https://en.wikipedia.org/wiki/Louvain_Modularity">Louvain method</a>,” named after the university <a target="_blank" href="https://sites.uclouvain.be/networks/">in which</a> it was designed.</p>
<p>It would take too much effort to explain here <a target="_blank" href="http://arxiv.org/abs/arXiv:0803.0476">how the Louvain method works</a>; just take note that <a target="_blank" href="http://www.rdocumentation.org/packages/igraph/versions/1.0.1/topics/cluster_louvain">it works</a> with networks like ours—that is, one-mode, undirected networks based on a weighted adjacency matrix.</p>
<p>Detecting the Louvain community of each student, i.e. his or her cluster, based on the keywords that he or she shares with other students, takes one line of R code:</p>
<p></p>
<p>Let’s now see if the clusters make sense!</p>
<h2>Visualizing the one-mode network</h2>
<p>Before we proceed to plotting the network, let’s also compute the weighted degree <a target="_blank" href="https://en.wikipedia.org/wiki/Centrality">centrality</a> of the nodes, which will be useful to show students with less shared keywords in smaller size:</p>
<p></p>
<p>Note that the degree distribution that we have <a target="_blank" href="http://www.rdocumentation.org/packages/tnet/versions/3.0.14/topics/degree_w">computed</a> here also takes into account the weighted structure of the adjacency matrix that underlies our network.</p>
<p>Let’s now finally get to the network plot, with each keyword-clustered group of students shown in a different color:</p>
<p></p>
<p><img alt="bordered" src="https://i1.wp.com/f.briatte.org/r/images/collapsing-a-bipartite-co-occurrence-network-clusters.png?w=456" data-recalc-dims="1"></p>
<p>In this graph, the intensity of the edges (the ties between the students) reflects the number of keywords that they share with each other. The graph uses a different <a target="_blank" href="https://en.wikipedia.org/wiki/Force-directed_graph_drawing">force-directed layout</a> algorithm than the one used in the <a target="_blank" href="http://f.briatte.org/r/turning-keywords-into-a-co-occurrence-network">previous note</a>, but this is only a matter of taste.</p>
<h2>Final comments</h2>
<p>Looking at this final graph, what can we say about this year’s batch of GLM students?</p>
<p>If we trust the method that we used to cluster the students together based on their shared research interests, then Akhil, Alexander and Felicitas should be working together. Similarly, the four students from the “blue” cluster (Gabriella, Gabriel, Alice and Charlotte) should form two student pairs between them.</p>
<p>The results are less obvious for other students, but still, I would expect Mohamed to have paired with either Lucien, Margaux, Corentin or Isabelle, and Marina to have paired with either Elena, Miranda, Cosima or Lucile.</p>
<p>We will see in class if that is what happened in reality, but I can answer that question straight away for readers from outside our class: not at all! This year’s GLM students seem to have privileged other forms of ties between them.</p>
<hr>
<p>The code shown in this not is provided in full in <a target="_blank" href="https://gist.github.com/briatte/fb1ca2d10253886ac33ea749aa95f82c">this Gist</a>. If you want to replicate the plot above, you will need to <a target="_blank" href="https://gist.github.com/briatte/fb1ca2d10253886ac33ea749aa95f82c#file-00_keywords-txt">download the data</a> and (install and) <a target="_blank" href="https://gist.github.com/briatte/fb1ca2d10253886ac33ea749aa95f82c#file-01_packages-r">load the required packages</a> first.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/"></div></div><div><a data-url="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/" data-counturl="https://www.r-bloggers.com/collapsing-a-bipartite-co-occurrence-network/" data-count="vertical" data-text="Collapsing a bipartite co-occurrence network" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://f.briatte.org/r/collapsing-a-bipartite-co-occurrence-network"> R / Notes</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/TSCqfcjfrxI"></a></div>