<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/voting-clusters-in-the-u-n/"></div></div><div><a data-url="http://www.r-bloggers.com/voting-clusters-in-the-u-n/" data-counturl="http://www.r-bloggers.com/voting-clusters-in-the-u-n/" data-count="horizontal" data-text="Voting clusters in the U.N." 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://www.enelmargen.org/datascience/un-voting-communities"> En El Margen - R-English</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>After some more digging, and a suggestion by <a target="_blank" href="https://twitter.com/theMexIndian">@theMexIndian</a> I decided to see more in the depth the unvotes database that I <a target="_blank" href="http://enelmargen.org/datascience/un-voting-patterns/">wrote about</a> some weeks ago.</p>
<p>This time, amit suggested I do some hierarchical clustering of the votes. So here goes a <strong>very dirty</strong> first attempt…</p>
<h2>Data and setup</h2>
<p>Nothing too impressive here… (for a discussion of the package, see the original post).</p>
<div>
<pre><code data-lang="r"><span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>magrittr<span>)</span>
<span>library</span><span>(</span>unvotes<span>)</span>
<span>library</span><span>(</span>reshape2<span>)</span>

<span># number of roll-calls</span>
votes <span>&lt;-</span> un_votes <span>%>%</span>
  left_join<span>(</span><span>.</span><span>,</span> un_roll_calls<span>)</span> <span>%>%</span>
  left_join<span>(</span><span>.</span><span>,</span> un_roll_call_issues<span>)</span>

<span>length</span><span>(</span><span>unique</span><span>(</span>votes<span>$</span>rcid<span>))</span>
<span># [1] 5275 # number of unique roll call votes</span></code></pre>
</div>
<p>There are more than 5k unique roll calls, so if we where to open up dimensionality by each roll call, this is going to be huge, but i’ll go ahead and do it anyways, to test a hypothesis towards the end…</p>
<h2>‘Widen’ data…</h2>
<div>
<pre><code data-lang="r">wide <span>&lt;-</span> votes <span>%>%</span> 
  select<span>(</span>rcid<span>,</span> country<span>,</span> vote<span>)</span> <span>%>%</span> 
  dcast<span>(,</span> formula <span>=</span> rcid<span>+</span>country <span>~</span> vote<span>)</span> <span>%>%</span> 
  dcast<span>(,</span> formula <span>=</span> country<span>~</span>rcid<span>+</span>yes<span>+</span>no<span>+</span>abstain<span>)</span>

str<span>(</span>wide<span>)</span>
<span># 'data.frame':	200 obs. of 14352 variables:</span></code></pre>
</div>
<p>Now that we have a very high dimension data set (each variable is the vote in a roll call, for example, abstain_120, yes_120, no_120 would be a count of abstain, yes and no votes in roll call 120). This data set is basically ones and ceros. Now to do some cleaning and get the distance matrix…</p>
<div>
<pre><code data-lang="r">wide<span>[</span><span>is.na</span><span>(</span>wide<span>)]</span> <span>&lt;-</span> <span>0</span>
d_wide <span>&lt;-</span> <span>as.matrix</span><span>(</span>wide<span>)</span>
<span>row.names</span><span>(</span>d_wide<span>)</span> <span>&lt;-</span> wide<span>$</span>country <span># to name rows</span>
d_wide <span>&lt;-</span> dist<span>(</span>d_wide<span>)</span> <span># distance matrix</span>
hc_wide <span>&lt;-</span> hclust<span>(</span>d_wide<span>)</span> <span># hierarchical cluster</span></code></pre>
</div>
<p>Let’s graph this hierarchical clustering using the <code>ggdendro</code> package…</p>
<div>
<pre><code data-lang="r"><span>library</span><span>(</span>ggdendro<span>)</span>
<span>library</span><span>(</span>eem<span>)</span> <span># blog colors</span>
ggdendrogram<span>(</span>hc_wide<span>,</span> 
  rotate <span>=</span> <span>TRUE</span><span>)</span> <span>+</span> 
    theme_eem<span>()</span> <span>+</span> 
    theme<span>(</span>axis.text.y <span>=</span> element_text<span>(</span>size<span>=</span><span>6</span><span>))</span> <span>+</span> 
    labs<span>(</span>x <span>=</span> <span>&quot;country&quot;</span><span>,</span> 
         y <span>=</span> <span>&quot;&quot;</span><span>,</span> 
         title <span>=</span> <span>&quot;Hierarchical clusters of votes \n in U.N.&quot;</span><span>)</span></code></pre>
</div>
<p><img alt="Dendogram of UN votes by country" src="http://i2.wp.com/www.enelmargen.org/images/posts/dendo_1.png?w=456" data-recalc-dims="1"></p>
<p>I’m going to export these clusters and upload them on <a target="_blank" href="https://github.com/Eflores89/proyectos/tree/master/data">my github</a> for anyone to download.</p>
<div>
<pre><code data-lang="r">hc_c <span>&lt;-</span> cutree<span>(</span>hc_wide<span>,</span> k <span>=</span> <span>8</span><span>)</span>
hc_c <span>&lt;-</span> <span>as.data.frame</span><span>(</span>hc_c<span>,</span> row.names <span>=</span> <span>names</span><span>(</span>hc_c<span>))</span>
hc_c<span>$</span><span>c</span> <span>&lt;-</span> <span>row.names</span><span>(</span>hc_c<span>)</span>
cc <span>&lt;-</span> hc_c <span>%>%</span> arrange<span>(</span><span>-</span>hc_c<span>)</span>

write.csv<span>(</span><span>as.data.frame</span><span>(</span>cc<span>),</span> file <span>=</span> <span>&quot;country_clusters.csv&quot;</span><span>)</span></code></pre>
</div>
<h2>By issues</h2>
<p>Now, because the latest data set was very high dimension, i’m going to condense the analysis to just votes on particular issues. The data base has seven core issues, so i’m going to try to group by issue instead of roll call. This might let us see if there are different voting blocs from the earlier set (maybe countries vote the same, except when important issues come up).</p>
<div>
<pre><code data-lang="r"><span># Widen, by issue...</span>
wide_byissue <span>&lt;-</span> votes <span>%>%</span> 
  select<span>(</span>issue<span>,</span> country<span>,</span> vote<span>)</span> <span>%>%</span> 
  dcast<span>(,</span> formula <span>=</span> country <span>~</span> vote<span>+</span>issue<span>)</span>

wide_byissue<span>[</span><span>is.na</span><span>(</span>wide_byissue<span>)]</span> <span>&lt;-</span> <span>0</span>
d_wide_issue <span>&lt;-</span> <span>as.matrix</span><span>(</span>wide_byissue<span>)</span>
<span>row.names</span><span>(</span>d_wide_issue<span>)</span> <span>&lt;-</span> wide_byissue<span>$</span>country
d_wide_issue <span>&lt;-</span> dist<span>(</span>d_wide_issue<span>)</span>
hc_wide_issue <span>&lt;-</span> hclust<span>(</span>d_wide_issue<span>)</span>

ggdendrogram<span>(</span>hc_wide_issue<span>,</span> 
  rotate <span>=</span> <span>TRUE</span><span>)</span> <span>+</span> 
    theme_eem<span>()</span> <span>+</span> 
    theme<span>(</span>axis.text.y <span>=</span> element_text<span>(</span>size<span>=</span><span>6</span><span>))</span> <span>+</span> 
    labs<span>(</span>x <span>=</span> <span>&quot;country&quot;</span><span>,</span> 
         y <span>=</span> <span>&quot;&quot;</span><span>,</span> 
         title <span>=</span> <span>&quot;Hierarchical clusters of votes \n in U.N. (issues)&quot;</span><span>)</span></code></pre>
</div>
<p><img alt="Dendogram of UN votes by country, when grouping by issue" src="http://i2.wp.com/www.enelmargen.org/images/posts/dendo_2.png?w=456" data-recalc-dims="1"></p>
<p>I’ll export this too…</p>
<div>
<pre><code data-lang="r">hc_c2 <span>&lt;-</span> cutree<span>(</span>hc_wide_issue<span>,</span> k <span>=</span> <span>8</span><span>)</span>
hc_c2 <span>&lt;-</span> <span>as.data.frame</span><span>(</span>hc_c2<span>,</span> row.names <span>=</span> <span>names</span><span>(</span>hc_c2<span>))</span>
hc_c2<span>$</span><span>c</span> <span>&lt;-</span> <span>row.names</span><span>(</span>hc_c2<span>)</span>
cc2 <span>&lt;-</span> hc_c2 <span>%>%</span> arrange<span>(</span><span>-</span>hc_c2<span>)</span>

write.csv<span>(</span><span>as.data.frame</span><span>(</span>cc2<span>),</span> file <span>=</span> <span>&quot;country_clusters_issue.csv&quot;</span><span>)</span></code></pre>
</div>
<p>To disprove the earlier hypothesis, i’m going to find Mexico’s neighborhood, and see if there are many countries that repeat themselves in both sets…</p>
<div>
<pre><code data-lang="r"><span># find cluster where Mexico lives ... </span>
neighborhood_mx <span>&lt;-</span> hc_c <span>%>%</span> filter<span>(</span>hc_c <span>==</span> <span>3</span><span>)</span>
neighborhood_mx_issue <span>&lt;-</span> hc_c2 <span>%>%</span> filter<span>(</span>hc_c2 <span>==</span> <span>1</span><span>)</span>

<span>sum</span><span>(</span>neighborhood_mx_issue<span>$</span><span>c</span> <span>%in%</span> neighborhood_mx<span>$</span><span>c</span><span>)</span><span>/</span><span>length</span><span>(</span>neighborhood_mx_issue<span>$</span><span>c</span><span>)</span>
<span># [1] 0.8</span>

<span># export mexico's neighborhood</span>
write.csv<span>(</span>neighborhood_mx_issue<span>,</span> file <span>=</span> <span>&quot;neighborhood_mx_issue.csv&quot;</span><span>)</span></code></pre>
</div>
<p>So 80% of the country’s are “close” to Mexico whether the vote is by issue or by roll call. This is a rough first attempt (there are probably many slight errors) but there are some interesting things to be found.</p>
<p>In the issues groups, the outliers in a single group are the United States and Israel (the Palestinian conflict probably is the culprit here, as I found earlier, they agree on 77% of the votes).</p>
<p>Then there are countries that seem to be very close culturally, and they show it in the votes…</p>
<div>
<pre><code data-lang="r"><span># advanced foreign policy</span>
hc_c2 <span>%>%</span> filter<span>(</span>hc_c2 <span>==</span> <span>&quot;6&quot;</span><span>)</span>
<span># [1] &quot;Austria&quot; &quot;Denmark&quot; &quot;Finland&quot; &quot;Greece&quot; &quot;Iceland&quot; </span>
<span># [6] &quot;Ireland&quot; &quot;Japan&quot; &quot;New Zealand&quot; &quot;Norway&quot; &quot;Spain&quot; </span>
<span># [11] &quot;Sweden&quot;</span></code></pre>
</div>
<p>Finally, some like-minded countries, like Chile, Colombia, Panama, Paraguay, Peru, etc are in Mexico’s neighborhood (although it’s one of the largest groups).</p>
<p><a target="_blank" href="https://twitter.com/eflores89">Tweet</a> me up if you have any questions with the data!</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="http://www.r-bloggers.com/voting-clusters-in-the-u-n/"></div></div><div><a data-url="http://www.r-bloggers.com/voting-clusters-in-the-u-n/" data-counturl="http://www.r-bloggers.com/voting-clusters-in-the-u-n/" data-count="vertical" data-text="Voting clusters in the U.N." 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://www.enelmargen.org/datascience/un-voting-communities"> En El Margen - R-English</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/nN3vY09PgdA"></a></div>