<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Bernie&#39;s Website</title>
<link>https://bernhardbieri.ch/blog/</link>
<atom:link href="https://bernhardbieri.ch/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>A blog about development economics, statistical computing and the likes.&lt;br&gt;
[Subscribe via RSS](/index.xml)
</description>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Sat, 28 Jan 2023 00:00:00 GMT</lastBuildDate>
<item>
  <title>Mapmaking with R and OpenStreetMaps</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<p>In this first post of the year, we’ll explore how to create artistic maps with R and OpenStreetMaps. In the process of creating the map, we’ll also discover the API capabilities of the <code>{osmdata}</code> package which could also be leveraged for heaps of other scientific applications. The main inspiration for this post comes from <a href="https://www.tanyashapiro.com/">Tanya Shapiro’s</a> visualizations.</p>
<section id="the-artsy-application" class="level2">
<h2 class="anchored" data-anchor-id="the-artsy-application">The artsy application…</h2>
<p>We’ll rely on the <code>{sf}</code>, <code>{osmdata}</code>, and <code>{tidyverse}</code> libraries to create a map of Washington, DC that includes information on university campuses and cafes a.k.a. the most important places for students.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import required libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osmdata)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</div>
<p>We’ll start by getting the border polygon of Washington, DC with the <code>getbb()</code> function from the <code>{osmdata}</code> library as an <code>{sf}</code> object. We then select the main polygon only.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get DC borders</span></span>
<span id="cb2-2">dcosmborders <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format_out =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf_polygon"</span>)</span>
<span id="cb2-3">dcosmborders <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dcosmborders[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only main polygon</span></span></code></pre></div>
</div>
<p>Next, the code uses the <code>opq()</code> and <code>add_osm_feature()</code> functions from the <code>{osmdata}</code> library to get data on the city’s streets and filter it to only include “motorway”, “primary”, “secondary”, and “tertiary” roads. The resulting data is then stored in the variable “streets” and is intersected with the city’s borders to ensure that the data only includes information within the city. The same method is applied to extract data for bodies of water, universities and cafes. Note that water has both polygons and multipolygons that need to be extracted separately.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get big streets</span></span>
<span id="cb3-2">streets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"highway"</span>, </span>
<span id="cb3-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"motorway"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary"</span>, </span>
<span id="cb3-6">                            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secondary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tertiary"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb3-8">streets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> streets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Water polygons</span></span>
<span id="cb3-12">water <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"water"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"river"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb3-16">water_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> water<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb3-18">water_multipoly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> water<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_multipolygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb3-20"></span>
<span id="cb3-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># University polygons</span></span>
<span id="cb3-22">university <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amenity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"university"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb3-26">university <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> university<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb3-28"></span>
<span id="cb3-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cafes points</span></span>
<span id="cb3-30">cafe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amenity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cafe"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb3-34">cafe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cafe<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span></code></pre></div>
</div>
<p>Finally, we leverage <code>{ggplot2}</code> functions to create the map itself including various customization for the map’s appearance, such as colors, text, and themes. The color palette was manually selected using the <a href="https://coolors.co/">Coolors</a> generator.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting the title code in HTML for ggtext::geom_richtext</span></span>
<span id="cb4-2">title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;span style='font-family:Arial;font-size:20pt;'&gt;**Washington, DC**&lt;/span&gt;&lt;br&gt;&lt;br&gt;</span></span>
<span id="cb4-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;span style='font-size:10pt;'&gt;Map of &lt;span style='font-size:12pt; color:#D1BE9C'&gt;University Campuses&lt;/span&gt; and &lt;span style='font-size:12pt; color:#EBB9DF'&gt;Cafes&lt;/span&gt; in&lt;br&gt; Washington, DC according to OSM data.&lt;/span&gt;"</span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot</span></span>
<span id="cb4-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dcosmborders,</span>
<span id="cb4-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#834ba0"</span>,</span>
<span id="cb4-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#834ba0"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb4-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> streets,</span>
<span id="cb4-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ce78b3"</span>,</span>
<span id="cb4-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb4-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb4-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> water_poly,</span>
<span id="cb4-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span>,</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span></span>
<span id="cb4-21">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb4-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> water_multipoly,</span>
<span id="cb4-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span>,</span>
<span id="cb4-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span></span>
<span id="cb4-27">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb4-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> university,</span>
<span id="cb4-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> F,</span>
<span id="cb4-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D1BE9C"</span>,</span>
<span id="cb4-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D1BE9C"</span></span>
<span id="cb4-33">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb4-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> cafe,</span>
<span id="cb4-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EBB9DF"</span>,</span>
<span id="cb4-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EBB9DF"</span>,</span>
<span id="cb4-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span></span>
<span id="cb4-40">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.79163</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.99597</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.15979</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">76.89937</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-43">  ggtext<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_richtext</span>(</span>
<span id="cb4-44">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.11</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.855</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> title),</span>
<span id="cb4-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb4-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb4-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb4-48">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Map Data Source: OpenStreetMap Contributors"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-51">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb4-52">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>),</span>
<span id="cb4-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ad5fad"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>),</span>
<span id="cb4-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#573b88"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb4-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pt"</span>)</span>
<span id="cb4-56">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="and-the-less-artsy-applications" class="level2">
<h2 class="anchored" data-anchor-id="and-the-less-artsy-applications">…and the less artsy applications:</h2>
<p>Open Street Map is a treasure trove for GIS data and its access has been greatly facilitated by the <a href="https://github.com/ropensci/osmdata"><code>{osmdata}</code></a> package leveraging the <a href="https://wiki.openstreetmap.org/wiki/Overpass_API">Overpass API</a>. Exploring the geometries you can query with the API is easy with the purpose built function of the <code>{osmdata}</code> package. Simply use <code>availiable_features()</code> and <code>availiable_tags()</code> to see all available features. In addition to geometries you can also often scrape location specific information such as the website of a business or artists having designed a particular fountain. Examples of what one can do with that data includes <a href="https://ghanadatastuff.com/post/plotting_walking_distances_osrm/">computing walking distances to amenities</a> and <a href="https://github.com/vjsantojaca/art-map-octo">creating more minimalistic art-maps</a>.</p>
</section>
<section id="epilogue" class="level2">
<h2 class="anchored" data-anchor-id="epilogue">Epilogue:</h2>
<p>And finally, here is the full code on howw you can make beautiful artistic maps with the OSM data API interface and <code>{ggplot2}</code>’s mapping capabilities in about 100 lines of code.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(sf)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(osmdata)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get DC borders</span></span>
<span id="cb5-6">dcosmborders <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format_out =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sf_polygon"</span>)</span>
<span id="cb5-7">dcosmborders <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dcosmborders[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,] <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only main polygon</span></span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get big streets</span></span>
<span id="cb5-10">streets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">key =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"highway"</span>, </span>
<span id="cb5-13">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"motorway"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary"</span>, </span>
<span id="cb5-14">                            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"secondary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tertiary"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb5-16">streets <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> streets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb5-18"></span>
<span id="cb5-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Water polygons</span></span>
<span id="cb5-20">water <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"water"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"river"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb5-24">water_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> water<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb5-26">water_multipoly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> water<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_multipolygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb5-28"></span>
<span id="cb5-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># University polygons</span></span>
<span id="cb5-30">university <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amenity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"university"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb5-34">university <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> university<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_polygons <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb5-36"></span>
<span id="cb5-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cafes</span></span>
<span id="cb5-38">cafe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getbb</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"washington dc"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opq</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_osm_feature</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"amenity"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cafe"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">osmdata_sf</span>()</span>
<span id="cb5-42">cafe <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cafe<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>osm_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">st_intersection</span>(dcosmborders)</span>
<span id="cb5-44"></span>
<span id="cb5-45">title <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;span style='font-family:galada;font-size:24pt;'&gt;**Washington, DC**&lt;/span&gt;&lt;br&gt;&lt;br&gt;</span></span>
<span id="cb5-46"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;span style='font-size:11pt;'&gt;Map of &lt;span style='font-size:13pt; color:#D1BE9C'&gt;University Campuses&lt;/span&gt; and &lt;span style='font-size:13pt; color:#EBB9DF'&gt;Cafes&lt;/span&gt; in&lt;br&gt; Washington, DC according to OSM data.&lt;/span&gt;"</span></span>
<span id="cb5-47"></span>
<span id="cb5-48"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dcosmborders,</span>
<span id="cb5-50">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#834ba0"</span>,</span>
<span id="cb5-51">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#834ba0"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb5-53">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> streets,</span>
<span id="cb5-54">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-55">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ce78b3"</span>,</span>
<span id="cb5-56">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb5-57">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb5-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> water_poly,</span>
<span id="cb5-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-61">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span>,</span>
<span id="cb5-62">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span></span>
<span id="cb5-63">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb5-65">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> water_multipoly,</span>
<span id="cb5-66">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb5-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span>,</span>
<span id="cb5-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#00E5E8"</span></span>
<span id="cb5-69">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-70">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb5-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> university,</span>
<span id="cb5-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> F,</span>
<span id="cb5-73">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D1BE9C"</span>,</span>
<span id="cb5-74">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#D1BE9C"</span></span>
<span id="cb5-75">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-76">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_sf</span>(</span>
<span id="cb5-77">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> cafe,</span>
<span id="cb5-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EBB9DF"</span>,</span>
<span id="cb5-79">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#EBB9DF"</span>,</span>
<span id="cb5-80">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb5-81">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span></span>
<span id="cb5-82">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-83">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.79163</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.99597</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-84">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.11979</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">76.90937</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-85">  ggtext<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_richtext</span>(</span>
<span id="cb5-86">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.09</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">38.855</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> title),</span>
<span id="cb5-87">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb5-88">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb5-89">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb5-90">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-91">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Map Data Source: OpenStreetMap Contributors"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-92">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-93">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb5-94">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>),</span>
<span id="cb5-95">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ad5fad"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>),</span>
<span id="cb5-96">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#573b88"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb5-97">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(</span>
<span id="cb5-98">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb5-99">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb5-100">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb5-101">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb5-102">    )</span>
<span id="cb5-103">  )</span></code></pre></div>
</div>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2023" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2023. <span>“Mapmaking with R and
OpenStreetMaps.”</span> January 28, 2023. <a href="https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/">https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/</a>.
</div></div></section></div> ]]></description>
  <category>Maps</category>
  <category>GIS</category>
  <category>R</category>
  <guid>https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/</guid>
  <pubDate>Sat, 28 Jan 2023 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2023-01-28-mapmaking-with-r-and-openstreetmaps/featured.png" medium="image" type="image/png" height="136" width="144"/>
</item>
<item>
  <title>Simulating Simpson’s Paradox</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<blockquote class="blockquote">
<p>This is the first post in a series called Bernie’s statistics aide-mémoire where I strive to understand various econometric concepts with simulations and other techniques. This first entry is dedicated to Simpson’s paradox, a well known phenomenon occuring when considering data at multiple levels of observation.</p>
</blockquote>
<section id="simpsons-paradox" class="level1">
<h1>Simpson’s Paradox</h1>
<section id="simpsons-case-study" class="level2">
<h2 class="anchored" data-anchor-id="simpsons-case-study">Simpson’s Case Study</h2>
<p>Simpson’s paradox has been defined in multiple ways over the past few decades and the Stanford Encyclopedia provides an <a href="https://plato.stanford.edu/entries/paradox-simpson/#WhatMakeSimpParaPara">excellent theoretical overview of the concept</a>. The aim of this less extensive post is to provide a short introduction of Simpson’s paradox and illustrate it by running Monte Carlo simulations to visually explore its impact on causal inference.</p>
<p>In short, Simpson’s paradox manifests itself when one considers data at different levels of observation and observes the reversal or the disappearance of an association between two variables <span class="citation" data-cites="sprengerSimpsonParadox2021">(Sprenger and Weinberger 2021)</span>. Imagine that you are running a field experiment in Statsland and run a randomized control trial to assess the causal effect of a medical treatment on a given health condition. For simplicity, assume that health status is binary e.g. <img src="https://latex.codecogs.com/png.latex?Y%5C_i%20%5Cin%20%5C%7B%5Ctext%7BHealthy%7D;%20%5Ctext%7BSick%7D%5C%7D">. Assume that you observe a large enough sample so that the observed proportions correspond to the true proportions of the population. Finally, imagine that there are two regions in Statsland: North and South. Given this setup, the following situation may arise.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 13%">
<col style="width: 18%">
<col style="width: 18%">
<col style="width: 17%">
<col style="width: 18%">
<col style="width: 13%">
</colgroup>
<thead>
<tr class="header">
<th>Region Arm</th>
<th>North Control</th>
<th>North Treated</th>
<th>South Control</th>
<th>South Treated</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Healthy</td>
<td>4</td>
<td>8</td>
<td>2</td>
<td>12</td>
<td>26</td>
</tr>
<tr class="even">
<td>Sick</td>
<td>3</td>
<td>5</td>
<td>3</td>
<td>15</td>
<td>26</td>
</tr>
<tr class="odd">
<td>Total</td>
<td>7</td>
<td>13</td>
<td>5</td>
<td>27</td>
<td>52</td>
</tr>
</tbody>
</table>
<blockquote class="blockquote">
<p>The numbers in the table above are inspired by <span class="citation" data-cites="sprengerSimpsonParadox2021">(Sprenger and Weinberger 2021)</span> who use the example of <span class="citation" data-cites="simpsonInterpretationInteractionContingency1951">(Simpson 1951)</span> to illustrate the intuition behind Simpson’s Paradox.</p>
</blockquote>
<p>One can identify two seemingly contradicting insights in the numbers above. When considering the population as a whole, the proportion of cured individuals is equal in the treatment and the control arm e.g.</p>
<p><img src="https://latex.codecogs.com/png.latex?P(Healthy%7CTreated)%20=%20%5Cfrac%7B8+12%7D%7B40%7D%20=%20P(Healthy%7CControl)%20=%20%5Cfrac%7B2+4%7D%7B20%7D"></p>
<p>However, one sees that when conditioning on the region in addition to treatment status, we notice that <img src="https://latex.codecogs.com/png.latex?P(Healthy%7CTreated,%20North)%20=%20%5Cfrac%7B8%7D%7B13%7D%20%3E%20P(Healthy%7CControl,%20North)%20=%20%5Cfrac%7B4%7D%7B7%7D"> and</p>
<p><img src="https://latex.codecogs.com/png.latex?P(Healthy%7CTreated,%20South)%20=%20%5Cfrac%7B12%7D%7B27%7D%20%3E%20P(Healthy%7CControl,%20South)%20=%20%5Cfrac%7B2%7D%7B5%7D"></p>
<p>In other words, the treatment seems to be ineffective when considering the population as a whole. On the contrary, when considering the conditional probabilities <em>within</em> each level of observation, the treatment seems to have a positive effect on health status<sup>1</sup>.</p>
</section>
<section id="definition" class="level2">
<h2 class="anchored" data-anchor-id="definition">Definition</h2>
<section id="theoretical-intuition" class="level3">
<h3 class="anchored" data-anchor-id="theoretical-intuition">Theoretical Intuition</h3>
<p>Where does this seemingly counterintuitive pattern emerge from? As <span class="citation" data-cites="sprengerSimpsonParadox2021">(Sprenger and Weinberger 2021)</span> point out, one can explain the situation through two observations in the data above. First, people in the northern region are more likely to recover from treatment overall. Second, individuals in the northern region are less likely to be in the treatment group. These two facts explain the disappearance of the correlation at the population level. Formally, we can show it through the following probability decomposition:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AP(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cmathrm%7BT%7D)%20&amp;=P(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cmathrm%7BT%7D,%20%5Cmathrm%7BN%7D)%20P(%5Cmathrm%7BN%7D%20%5Cmid%20%5Cmathrm%7BT%7D)+P(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cmathrm%7BT%7D,%20%5Cneg%20%5Cmathrm%7BN%7D)%20P(%5Cneg%20%5Cmathrm%7BN%7D%20%5Cmid%20%5Cmathrm%7BT%7D)%20%5C%5C%0AP(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cneg%20%5Cmathrm%7BT%7D)%20&amp;=P(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cneg%20%5Cmathrm%7BT%7D,%20%5Cmathrm%7BN%7D)%20P(%5Cmathrm%7BN%7D%20%5Cmid%20%5Cneg%20%5Cmathrm%7BT%7D)+P(%5Cmathrm%7B~H%7D%20%5Cmid%20%5Cneg%20%5Cmathrm%7BT%7D,%20%5Cneg%20%5Cmathrm%7BN%7D)%20P(%5Cneg%20%5Cmathrm%7BN%7D%20%5Cmid%20%5Cneg%20%5Cmathrm%7BT%7D)%0A%5Cend%7Baligned%7D%0A"></p>
<p>The key terms in this decomposition are <img src="https://latex.codecogs.com/png.latex?P(%5Cmathrm%7BN%7D%20%5Cmid%20%5Cmathrm%7BT%7D)"> and <img src="https://latex.codecogs.com/png.latex?P(%5Cmathrm%7B%5Cneg%20N%7D%20%5Cmid%20%5Cmathrm%7BT%7D)"> that weight the probability within the region to form the population level probability<sup>2</sup> in a way that leads to the positive association within regions to vanish in the aggregate population.</p>
</section>
<section id="different-forms-of-the-paradox" class="level3">
<h3 class="anchored" data-anchor-id="different-forms-of-the-paradox">Different Forms of the Paradox</h3>
<p>Simpson’s paradox has been defined in the following three main ways <span class="citation" data-cites="sprengerSimpsonParadox2021">(Sprenger and Weinberger 2021)</span>:</p>
<ul>
<li><strong>Association Reversal</strong>
<ul>
<li>In this first version, the association wihtin each subpopulation/region is either positive, negative, or null while the association at the population level has a different sign.</li>
<li>See <span class="citation" data-cites="samuelsSimpsonParadoxRelated1993">(Samuels 1993)</span>.</li>
</ul></li>
<li><strong>Yule’s Association Paradox</strong>
<ul>
<li>In this second version, the association in the whole population is different from zero e.g. there is a positive or negative effect of the treatment when considering Statsland as a whole. But the effect dissappears when considering individual regions.</li>
<li>See <span class="citation" data-cites="mittalHomogeneitySubpopulationsSimpson1991">(Mittal 1991)</span>.</li>
</ul></li>
<li><strong>Amalgamation Paradox</strong>
<ul>
<li>In this third and more general version, the association between the two variables of interest in the population as a whole is stronger than the maximum association between the two variables <em>within</em> each region.</li>
<li>See <span class="citation" data-cites="goodAmalgamationGeometryTwobyTwo1987">(Good and Mittal 1987)</span>.</li>
</ul></li>
</ul>
<p>For a more formal characterisation of the different variants of Simpson’s Paradox see the excellent entry by <span class="citation" data-cites="sprengerSimpsonParadox2021">(Sprenger and Weinberger 2021)</span>.</p>
</section>
</section>
<section id="simulations-with-non-categorical-data" class="level2">
<h2 class="anchored" data-anchor-id="simulations-with-non-categorical-data">Simulations with Non-Categorical Data</h2>
<p>Now that we gained an intuitive understanding of the paradox, let us run some simulations of an Association Reversal. Unlike in the first section of this post where we considered a binary treatment variable, we will illustrate it with a continuous independent variable <img src="https://latex.codecogs.com/png.latex?X">. We start by writing the data generation process where we define three villages in which we record 1000 observations each. We then define three village level effects called <code>beta</code> as well as various moments of the independent variable <img src="https://latex.codecogs.com/png.latex?X"> and the error term <img src="https://latex.codecogs.com/png.latex?U">. We then generate one example run of the synthetic dataset, plot it and examine a table of results of the various specifications. We define the naive full specification as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20=%20x_i%20%5Cbeta_i%20+%20u_i%0A"> and the village level specification for village 1 as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20=%20x_i%20%5Cbeta_i%20+%20u_i,%5C%20%5Ctext%7Bwhere%7D%5C%20i%5C%20%5Ctext%7Blives%20in%20village%201,%202,%20or%203%7D%0A"> which is equivalent to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_%7Bi,%20v%7D%20=%20x_%7Bi,%20v%7D%20%5Cbeta_%7Bi,%20v%7D%20+%20u_%7Bi,%20v%7D%20%5C%20%5Ctext%7B,%20%7D%20%5C%20v%20%5Cin%20%5C%7B1,%202%20,%203%5C%7D%0A"></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function generating data affected by Simpson's paradox with an</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Association Reversal</span></span>
<span id="cb1-3">gensimpsondata <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(N,                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Within village beta.</span></span>
<span id="cb1-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Within village beta.</span></span>
<span id="cb1-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mean of X within village</span></span>
<span id="cb1-6">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigmax     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SD of X within village</span></span>
<span id="cb1-7">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigmau     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SD of error term</span></span>
<span id="cb1-8">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupFE     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fixed effect of Group</span></span>
<span id="cb1-9">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village"</span>) {</span>
<span id="cb1-10">  groupNo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(mu))</span>
<span id="cb1-11">  groups <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(name_groups, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, groupNo)</span>
<span id="cb1-12">  datasetlist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb1-13">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(groupNo))) {</span>
<span id="cb1-14">    datasetlist[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb1-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GroupNo =</span> groupNo[[i]],</span>
<span id="cb1-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(groups[[i]], N),</span>
<span id="cb1-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GroupFE =</span> groupNo[[i]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> groupFE,</span>
<span id="cb1-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, mu[[i]], sigmax[[i]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> groupNo[[i]],</span>
<span id="cb1-19">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X depends on village level effects</span></span>
<span id="cb1-20">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">U       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sigmau[[i]])</span>
<span id="cb1-21">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-22">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> beta[[i]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> GroupFE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> U)</span>
<span id="cb1-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Y depends on X and Village level effects.</span></span>
<span id="cb1-24">  }</span>
<span id="cb1-25">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(datasetlist)</span>
<span id="cb1-26">}</span>
<span id="cb1-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate one iteration of the data called "example".</span></span>
<span id="cb1-28">example <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gensimpsondata</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span></code></pre></div>
</div>
<p>In the first graph below, we consider all three villages and seemingly identify a positive association between <img src="https://latex.codecogs.com/png.latex?X"> and <img src="https://latex.codecogs.com/png.latex?Y">.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the example data.</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot with full sample.</span></span>
<span id="cb2-3">plotexamplefull <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-4">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-5">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb2-6">    ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y),</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb2-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb2-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x</span>
<span id="cb2-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-11">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Illustrating Simpson's Paradox"</span>,</span>
<span id="cb2-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple regression of Y on X  for the full sample."</span>,</span>
<span id="cb2-13">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated data."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-14">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb2-15">plotexamplefull</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/index_files/figure-html/examplegraphfull-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>However, when running the regressions within each individual village, we find that the effect seems to be reversed! This illustrates the <strong>Association Reversal</strong>, described in the previous section <span class="citation" data-cites="samuelsSimpsonParadoxRelated1993">(Samuels 1993)</span>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot with full sample grouped by village.</span></span>
<span id="cb3-2">plotexample <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> Group), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb3-5">    ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y),</span>
<span id="cb3-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb3-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x</span>
<span id="cb3-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Group),</span>
<span id="cb3-11">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb3-12">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-13">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Illustrating Simpson's Paradox"</span>,</span>
<span id="cb3-14">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple regression of Y on X  for the three villages individually."</span>,</span>
<span id="cb3-15">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated data."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display plot</span></span>
<span id="cb3-18">plotexample</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/index_files/figure-html/examplegraphvillage-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Let us quickly complement this visual intuition within a table with actual regression coefficients. Since we generated the data above, we already know the true <em>village level association</em> between our variables of interest and know the the <em>village effect</em>. We thus specify the naive regression of <img src="https://latex.codecogs.com/png.latex?Y"> on <img src="https://latex.codecogs.com/png.latex?X"> before looking at the effect within each village and finally specifying a regression with village level intercepts but a single <img src="https://latex.codecogs.com/png.latex?%5Cbeta">.</p>
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display the OLS regressions in a simple table to show the different results</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># when considering the full sample and considering the village-by-village</span></span>
<span id="cb4-3">lmall <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> example, Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb4-4">lmvillage1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb4-6">lmvillage2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb4-8">lmvillage3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The true regression with village level intercepts</span></span>
<span id="cb4-11">lmfixedeffect <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> example, Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Group)</span>
<span id="cb4-12"></span>
<span id="cb4-13">stargazer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stargazer</span>(</span>
<span id="cb4-14">  lmall,</span>
<span id="cb4-15">  lmvillage1,</span>
<span id="cb4-16">  lmvillage2,</span>
<span id="cb4-17">  lmvillage3,</span>
<span id="cb4-18">  lmfixedeffect,</span>
<span id="cb4-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>,</span>
<span id="cb4-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aer"</span>,</span>
<span id="cb4-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple Regressions with Full Sample and Village level data"</span>,</span>
<span id="cb4-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">column.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample Naive"</span>,</span>
<span id="cb4-23">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>,</span>
<span id="cb4-24">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>,</span>
<span id="cb4-25">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>,</span>
<span id="cb4-26">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample FE"</span>)</span>
<span id="cb4-27">)</span></code></pre></div>
<table style="text-align:center">
<caption>
<strong>Simple Regressions with Full Sample and Village level data</strong>
</caption>
<tbody><tr>
<td colspan="6" style="border-bottom: 1px solid black">
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td colspan="5">
Y
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
Full Sample Naive
</td>
<td>
Village 1
</td>
<td>
Village 2
</td>
<td>
Village 3
</td>
<td>
Full Sample FE
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
(1)
</td>
<td>
(2)
</td>
<td>
(3)
</td>
<td>
(4)
</td>
<td>
(5)
</td>
</tr>
<tr>
<td colspan="6" style="border-bottom: 1px solid black">
</td>
</tr>
<tr>
<td style="text-align:left">
X
</td>
<td>
1.107<sup>***</sup>
</td>
<td>
-1.002<sup>***</sup>
</td>
<td>
-0.789<sup>***</sup>
</td>
<td>
-1.093<sup>***</sup>
</td>
<td>
-0.961<sup>***</sup>
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
(0.026)
</td>
<td>
(0.093)
</td>
<td>
(0.095)
</td>
<td>
(0.095)
</td>
<td>
(0.054)
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td style="text-align:left">
GroupVillage 2
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
14.487<sup>***</sup>
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
(0.422)
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td style="text-align:left">
GroupVillage 3
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
28.932<sup>***</sup>
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
(0.703)
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td style="text-align:left">
Constant
</td>
<td>
4.761<sup>***</sup>
</td>
<td>
15.316<sup>***</sup>
</td>
<td>
27.492<sup>***</sup>
</td>
<td>
46.366<sup>***</sup>
</td>
<td>
15.066<sup>***</sup>
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
(0.343)
</td>
<td>
(0.591)
</td>
<td>
(1.156)
</td>
<td>
(1.713)
</td>
<td>
(0.380)
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td style="text-align:left">
Observations
</td>
<td>
3,000
</td>
<td>
1,000
</td>
<td>
1,000
</td>
<td>
1,000
</td>
<td>
3,000
</td>
</tr>
<tr>
<td style="text-align:left">
R<sup>2</sup>
</td>
<td>
0.373
</td>
<td>
0.105
</td>
<td>
0.064
</td>
<td>
0.117
</td>
<td>
0.599
</td>
</tr>
<tr>
<td style="text-align:left">
Adjusted R<sup>2</sup>
</td>
<td>
0.372
</td>
<td>
0.104
</td>
<td>
0.063
</td>
<td>
0.116
</td>
<td>
0.599
</td>
</tr>
<tr>
<td style="text-align:left">
Residual Std. Error
</td>
<td>
7.574 (df = 2998)
</td>
<td>
6.084 (df = 998)
</td>
<td>
6.084 (df = 998)
</td>
<td>
5.986 (df = 998)
</td>
<td>
6.055 (df = 2996)
</td>
</tr>
<tr>
<td style="text-align:left">
F Statistic
</td>
<td>
1,780.833<sup>***</sup> (df = 1; 2998)
</td>
<td>
116.827<sup>***</sup> (df = 1; 998)
</td>
<td>
68.635<sup>***</sup> (df = 1; 998)
</td>
<td>
132.690<sup>***</sup> (df = 1; 998)
</td>
<td>
1,493.680<sup>***</sup> (df = 3; 2996)
</td>
</tr>
<tr>
<td colspan="6" style="border-bottom: 1px solid black">
</td>
</tr>
<tr>
<td style="text-align:left">
<em>Notes:</em>
</td>
<td colspan="5" style="text-align:left">
<sup>***</sup>Significant at the 1 percent level.
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td colspan="5" style="text-align:left">
<sup>**</sup>Significant at the 5 percent level.
</td>
</tr>
<tr>
<td style="text-align:left">
</td>
<td colspan="5" style="text-align:left">
<sup>*</sup>Significant at the 10 percent level.
</td>
</tr>
</tbody></table>
<p>We see in this regression table that the point estimates of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> reflect our graphical analysis when we look at the full sample and the within-village estimations. The fifth and last model adds village level fixed effects to show that even if this estimation is consistent with the within village estimations in this case as all within village <img src="https://latex.codecogs.com/png.latex?%5Cbeta">’s are equal to <img src="https://latex.codecogs.com/png.latex?-1">, this need not be the case if within village effects are heterogenous (different) across villages. This can be easily seen if one runs the DGP function defined above with values such as <code>beta = c(-1, 0.5, -1)</code>. Simpson’s paradox can therefore not be solved by simply adding fixed effects.</p>
<p>Let us now convince ourselves that we did not get these results by chance as this was only one draw of our data generating function. To do that, we plot a distribution of the coefficient of interest <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> in the three regressions specified at the beginning of this section which we show below.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">mcsimpson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) {</span>
<span id="cb5-2">  datalist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-3">  coefvectlst <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb5-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(reps)) {</span>
<span id="cb5-5">    datalist[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gensimpsondata</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb5-6">    coefvectlst[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>()</span>
<span id="cb5-7">    lmall <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> datalist[[i]], Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb5-8">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmall[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb5-9">    lmvillage1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-10">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb5-12">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage1[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb5-13">    lmvillage2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-14">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb5-16">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage2[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb5-17">    lmvillage3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-18">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-19">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb5-20">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage3[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb5-21">    lmfixedeffect <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> datalist[[i]], Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Group)</span>
<span id="cb5-22">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmfixedeffect[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb5-23">  }</span>
<span id="cb5-24">  coefvectdf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, coefvectlst))</span>
<span id="cb5-25">  coefvectdf</span>
<span id="cb5-26">}</span>
<span id="cb5-27">coefsMC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mcsimpson</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-28">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-29">  tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(index, coefficient, dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-30">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample: Naive"</span>),</span>
<span id="cb5-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>),</span>
<span id="cb5-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>),</span>
<span id="cb5-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>),</span>
<span id="cb5-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample: FE"</span>)</span>
<span id="cb5-36">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-37">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sample =</span> index,</span>
<span id="cb5-38">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> coefficient)</span>
<span id="cb5-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Density plot of different coefficients</span></span>
<span id="cb5-40">freqmcsimpson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(coefsMC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-41">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb5-42">    Beta,</span>
<span id="cb5-43">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> Sample,</span>
<span id="cb5-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Sample,</span>
<span id="cb5-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Sample</span>
<span id="cb5-46">  ),</span>
<span id="cb5-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-48">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot of Coefficient Estimates Under</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Different Specifications"</span>,</span>
<span id="cb5-49">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Coefficients estimated by OLS"</span>,</span>
<span id="cb5-50">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monte Carlo Simulation: 200 iterations of synthetic DGP."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-51">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb5-52">freqmcsimpson</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/index_files/figure-html/MCcoefficients-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Simulations such as the one we just performed are a neat way of illustrating Simpson’s paradox. We have, however, not yet fully determined a method that would formally tell us at which level associations need to be considered in an observational study with actual empirical data. One way of seeing this is through what is known as Pearl’s Simpson Machine which will be the topic of a future post <span class="citation" data-cites="armisteadResurrectingThirdVariable2014 pearlBayesianAnalysisExpert1993">(Armistead 2014; Pearl 1993)</span>!</p>
<blockquote class="blockquote">
<p>PS: Please reach out if you spot any errors or have any comments and suggestions via the <a href="https://bernhardbieri.ch/contact/">contact form</a>.</p>
</blockquote>
</section>
<section id="code-appendix" class="level2">
<h2 class="anchored" data-anchor-id="code-appendix">Code Appendix</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Libraries</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MASS)</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stargazer)</span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dagitty)</span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggdag)</span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load non-standard fonts with {extrafont}</span></span>
<span id="cb6-9">extrafont<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">loadfonts</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"win"</span>)</span>
<span id="cb6-10">extrafont<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fonts</span>()</span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Function generating data affected by Simpson's paradox with an</span></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Association Reversal</span></span>
<span id="cb6-13">gensimpsondata <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(N,                         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Within village beta.</span></span>
<span id="cb6-14">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Within village beta.</span></span>
<span id="cb6-15">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mean of X within village</span></span>
<span id="cb6-16">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigmax     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SD of X within village</span></span>
<span id="cb6-17">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigmau     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SD of error term</span></span>
<span id="cb6-18">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupFE     =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>,          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fixed effect of Group</span></span>
<span id="cb6-19">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name_groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village"</span>) {</span>
<span id="cb6-20">  groupNo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(mu))</span>
<span id="cb6-21">  groups <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(name_groups, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" "</span>, groupNo)</span>
<span id="cb6-22">  datasetlist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb6-23">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(groupNo))) {</span>
<span id="cb6-24">    datasetlist[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb6-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GroupNo =</span> groupNo[[i]],</span>
<span id="cb6-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(groups[[i]], N),</span>
<span id="cb6-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GroupFE =</span> groupNo[[i]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> groupFE,</span>
<span id="cb6-28">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">X       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, mu[[i]], sigmax[[i]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> groupNo[[i]],</span>
<span id="cb6-29">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X depends on village level effects</span></span>
<span id="cb6-30">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">U       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sigmau[[i]])</span>
<span id="cb6-31">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-32">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y =</span> beta[[i]] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> GroupFE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> U)</span>
<span id="cb6-33">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Y depends on X and Village level effects.</span></span>
<span id="cb6-34">  }</span>
<span id="cb6-35">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(datasetlist)</span>
<span id="cb6-36">}</span>
<span id="cb6-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate one iteration of the data called "example".</span></span>
<span id="cb6-38">example <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gensimpsondata</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb6-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot the example data.</span></span>
<span id="cb6-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot with full sample.</span></span>
<span id="cb6-41">plotexamplefull <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-42">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-43">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb6-44">    ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y),</span>
<span id="cb6-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb6-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb6-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x</span>
<span id="cb6-48">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-49">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Illustrating Simpson's Paradox"</span>,</span>
<span id="cb6-50">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple regression of Y on X  for the full sample."</span>,</span>
<span id="cb6-51">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated data."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-52">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb6-53">plotexamplefull</span>
<span id="cb6-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot with full sample grouped by village.</span></span>
<span id="cb6-55">plotexample <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-56">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> Group), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-57">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb6-58">    ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y),</span>
<span id="cb6-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb6-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb6-61">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x</span>
<span id="cb6-62">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-63">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Group),</span>
<span id="cb6-64">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb6-65">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-66">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Illustrating Simpson's Paradox"</span>,</span>
<span id="cb6-67">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple regression of Y on X  for the three villages individually."</span>,</span>
<span id="cb6-68">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated data."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-69">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb6-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display plot</span></span>
<span id="cb6-71">plotexample</span>
<span id="cb6-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate featured gif with the example data. Does not appear in blogpost.</span></span>
<span id="cb6-73">plot1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-74">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-75">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(</span>
<span id="cb6-76">    ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y),</span>
<span id="cb6-77">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb6-78">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb6-79">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x</span>
<span id="cb6-80">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-81">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-82">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb6-83">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#222831"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb6-84">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#222831"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb6-85">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span id="cb6-86">  )</span>
<span id="cb6-87">plot2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(example) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-88">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(X, Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> Group), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-89">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> X, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Group),</span>
<span id="cb6-90">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>,</span>
<span id="cb6-91">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-92">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-93">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb6-94">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#222831"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb6-95">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#222831"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb6-96">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span id="cb6-97">  )</span>
<span id="cb6-98">ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(plot1,</span>
<span id="cb6-99">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/GitHub/Website/content/blog/2022-09-30-simpsonsparadox/plot1.png"</span>,</span>
<span id="cb6-100">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">192</span>)</span>
<span id="cb6-101">ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(plot2,</span>
<span id="cb6-102">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/GitHub/Website/content/blog/2022-09-30-simpsonsparadox/plot2.png"</span>,</span>
<span id="cb6-103">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">192</span>)</span>
<span id="cb6-104"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Display the OLS regressions in a simple table to show the different results</span></span>
<span id="cb6-105"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># when considering the full sample and considering the village-by-village</span></span>
<span id="cb6-106">lmall <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> example, Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-107">lmvillage1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-108">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-109">lmvillage2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-110">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-111">lmvillage3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(example, Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-112">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-113"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The true regression with village level intercepts</span></span>
<span id="cb6-114">lmfixedeffect <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> example, Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Group)</span>
<span id="cb6-115"></span>
<span id="cb6-116">stargazer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stargazer</span>(</span>
<span id="cb6-117">  lmall,</span>
<span id="cb6-118">  lmvillage1,</span>
<span id="cb6-119">  lmvillage2,</span>
<span id="cb6-120">  lmvillage3,</span>
<span id="cb6-121">  lmfixedeffect,</span>
<span id="cb6-122">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>,</span>
<span id="cb6-123">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"aer"</span>,</span>
<span id="cb6-124">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simple Regressions with Full Sample and Village level data"</span>,</span>
<span id="cb6-125">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">column.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample Naive"</span>,</span>
<span id="cb6-126">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>,</span>
<span id="cb6-127">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>,</span>
<span id="cb6-128">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>,</span>
<span id="cb6-129">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample FE"</span>)</span>
<span id="cb6-130">)</span>
<span id="cb6-131">mcsimpson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) {</span>
<span id="cb6-132">  datalist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb6-133">  coefvectlst <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>()</span>
<span id="cb6-134">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(reps)) {</span>
<span id="cb6-135">    datalist[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gensimpsondata</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb6-136">    coefvectlst[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>()</span>
<span id="cb6-137">    lmall <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> datalist[[i]], Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-138">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmall[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb6-139">    lmvillage1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-140">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-141">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-142">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage1[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb6-143">    lmvillage2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-144">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-145">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-146">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage2[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb6-147">    lmvillage3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-148">      dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(datalist[[i]], Group <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-149">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">formula =</span> Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X)</span>
<span id="cb6-150">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmvillage3[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb6-151">    lmfixedeffect <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> datalist[[i]], Y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Group)</span>
<span id="cb6-152">    coefvectlst[[i]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lmfixedeffect[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coefficients"</span>]][[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>]]</span>
<span id="cb6-153">  }</span>
<span id="cb6-154">  coefvectdf <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, coefvectlst))</span>
<span id="cb6-155">  coefvectdf</span>
<span id="cb6-156">}</span>
<span id="cb6-157">coefsMC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mcsimpson</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-158">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-159">  tidyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(index, coefficient, dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-160">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb6-161">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample: Naive"</span>),</span>
<span id="cb6-162">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 1"</span>),</span>
<span id="cb6-163">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 2"</span>),</span>
<span id="cb6-164">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Village 3"</span>),</span>
<span id="cb6-165">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">index =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replace</span>(index, index <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"V5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full Sample: FE"</span>)</span>
<span id="cb6-166">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-167">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sample =</span> index,</span>
<span id="cb6-168">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Beta =</span> coefficient)</span>
<span id="cb6-169"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Density plot of different coefficients</span></span>
<span id="cb6-170">freqmcsimpson <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(coefsMC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-171">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb6-172">    Beta,</span>
<span id="cb6-173">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> Sample,</span>
<span id="cb6-174">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Sample,</span>
<span id="cb6-175">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Sample</span>
<span id="cb6-176">  ),</span>
<span id="cb6-177">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-178">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density Plot of Coefficient Estimates Under</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">Different Specifications"</span>,</span>
<span id="cb6-179">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Coefficients estimated by OLS"</span>,</span>
<span id="cb6-180">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monte Carlo Simulation: 200 iterations of synthetic DGP."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-181">  HohgantR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">themebernie</span>()</span>
<span id="cb6-182">freqmcsimpson</span>
<span id="cb6-183"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################</span></span>
<span id="cb6-184"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Citing all loaded packages ##</span></span>
<span id="cb6-185"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################</span></span>
<span id="cb6-186">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_bib</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">.packages</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blogdown"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(here<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">here</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/blog/2022-09-30-simpsonsparadox/packages.bib"</span>))</span></code></pre></div>
</div>
</section>
<section id="references" class="level2">




</section>
</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-armisteadResurrectingThirdVariable2014" class="csl-entry">
Armistead, Timothy W. 2014. <span>“Resurrecting the <span>Third Variable</span>: <span>A Critique</span> of <span>Pearl</span>’s <span>Causal Analysis</span> of <span>Simpson</span>’s <span>Paradox</span>.”</span> <em>The American Statistician</em> 68 (1): 1–7. <a href="https://doi.org/10.1080/00031305.2013.807750">https://doi.org/10.1080/00031305.2013.807750</a>.
</div>
<div id="ref-goodAmalgamationGeometryTwobyTwo1987" class="csl-entry">
Good, I. J., and Y. Mittal. 1987. <span>“The <span>Amalgamation</span> and <span>Geometry</span> of <span class="nocase">Two-by-Two Contingency Tables</span>.”</span> <em>The Annals of Statistics</em> 15 (2): 694–711.
</div>
<div id="ref-mittalHomogeneitySubpopulationsSimpson1991" class="csl-entry">
Mittal, Yashaswini. 1991. <span>“Homogeneity of <span>Subpopulations</span> and <span>Simpson</span>’s <span>Paradox</span>.”</span> <em>Journal of the American Statistical Association</em> 86 (413): 167–72. <a href="https://doi.org/10.1080/01621459.1991.10475016">https://doi.org/10.1080/01621459.1991.10475016</a>.
</div>
<div id="ref-pearlBayesianAnalysisExpert1993" class="csl-entry">
Pearl, Judea. 1993. <span>“[<span>Bayesian Analysis</span> in <span>Expert Systems</span>]: <span>Comment</span>: <span>Graphical Models</span>, <span>Causality</span> and <span>Intervention</span>.”</span> <em>Statistical Science</em> 8 (3): 266–69.
</div>
<div id="ref-samuelsSimpsonParadoxRelated1993" class="csl-entry">
Samuels, Myra L. 1993. <span>“Simpson’s <span>Paradox</span> and <span>Related Phenomena</span>.”</span> <em>Journal of the American Statistical Association</em> 88 (421): 81–88. <a href="https://doi.org/10.1080/01621459.1993.10594297">https://doi.org/10.1080/01621459.1993.10594297</a>.
</div>
<div id="ref-simpsonInterpretationInteractionContingency1951" class="csl-entry">
Simpson, E. H. 1951. <span>“The <span>Interpretation</span> of <span>Interaction</span> in <span>Contingency Tables</span>.”</span> <em>Journal of the Royal Statistical Society: Series B (Methodological)</em> 13 (2): 238–41. <a href="https://doi.org/10.1111/j.2517-6161.1951.tb00088.x">https://doi.org/10.1111/j.2517-6161.1951.tb00088.x</a>.
</div>
<div id="ref-sprengerSimpsonParadox2021" class="csl-entry">
Sprenger, Jan, and Naftali Weinberger. 2021. <span>“Simpson’s <span>Paradox</span>.”</span> In <em>The <span>Stanford Encyclopedia</span> of <span>Philosophy</span></em>, edited by Edward N. Zalta, Summer 2021. <span>Metaphysics Research Lab, Stanford University</span>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>In this example, we refer to different levels of observations in a geographic sense. Partitioning the population could also be done across any observable characteristic of individuals such as gender as shown in the examples used by <span class="citation" data-cites="simpsonInterpretationInteractionContingency1951">Simpson (1951)</span> and <span class="citation" data-cites="sprengerSimpsonParadox2021">Sprenger and Weinberger (2021)</span>↩︎</p></li>
<li id="fn2"><p>See <a href="https://plato.stanford.edu/entries/paradox-simpson/">Stanford’s Plato entry</a> for a formal definition.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2022" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2022. <span>“Simulating Simpson’s Paradox.”</span>
September 26, 2022. <a href="https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/">https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/</a>.
</div></div></section></div> ]]></description>
  <category>Econometrics</category>
  <category>Aide-Mémoire</category>
  <category>Simulation</category>
  <category>R</category>
  <guid>https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/</guid>
  <pubDate>Mon, 26 Sep 2022 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2022-09-30-simpsonsparadox/featured.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Literate Programming in Stata</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<blockquote class="blockquote">
<p>TLDR: Literate programming with Stata just became easier thanks to the recent release of Quarto and the continuingly increasing integration of Stata and Python. One can now render a <code>.qmd</code> Quarto file or a Jupyter notebook containing Stata chunks and create publishing grade outputs such as presentations, dashboards and reports at the click of a button! The prerequisites of this workflow are Stata 17, Quarto, Python, as well as a few standard Python libraries. Click here to jump to the detailed instructions and skip the intro.</p>
</blockquote>
<section id="what-literate-programming-is-and-why-you-should-care" class="level2">
<h2 class="anchored" data-anchor-id="what-literate-programming-is-and-why-you-should-care">What Literate Programming Is and Why You Should Care</h2>
<p><a href="https://en.wikipedia.org/wiki/Literate_programming">Literate programming</a> is a concept pioneered by <a href="https://en.wikipedia.org/wiki/Donald_Knuth">Donald Knuth</a>, a Turing Award recipient known for creating <a href="https://en.wikipedia.org/wiki/TeX">TeX</a>. The main idea behind the early form of literate programming was to upend the traditional programming practices of the time by systematically including human readable text accompanying and explaining the logic and the purpose of a program. As he describes in <a href="https://www-cs-faculty.stanford.edu/~knuth/lp.html">“Literate Programming”</a>, Knuth considers the programmer as an “essayist” who should strive to communicate the purpose of a program in order to create better code. While initially centered in the domain of computer science, it more recently resurged in the interdisciplinary world of “data science”.</p>
</section>
<section id="the-resurgence-of-literate-programming-in-the-2010s" class="level2">
<h2 class="anchored" data-anchor-id="the-resurgence-of-literate-programming-in-the-2010s">The Resurgence of Literate Programming in the 2010’s</h2>
<p>With the advent of data science in the past 20 years with its interdisciplinary nature, the need for human readable text accompanying code grew. This is due to the fact that the <em>interpretation</em> of the results of a computation lies at the center of every data science project. It is thus extremely important to include either comments in a code or leverage modern literate programming tools to communicate the purpose of one’s code.</p>
<p>Modern examples of this practice include <a href="https://jupyter.org/">Jupyter Notebooks</a> for Python and the combination of the <a href="https://yihui.org/knitr/"><code>{knitr}</code></a> package and the RMarkdown format for R. These tools allow data scientists to interweave computational output with the theoretical explanations necessary to interpret it. Thereby, they accelerate the production of scientific products such as papers, reports, dashboards, etc. However, Stata, one of the most widely used piece of statistical software still lags behind in this regard and does not provide its users with a native option to leverage literal programming workflows. Existing solutions include <a href="https://journals.sagepub.com/doi/pdf/10.1177/1536867X1601600409"><code>{markdoc}</code></a> a Stata package that is akin to <code>{knitr}</code> in R and supports exports to PDF, HTML, Word, Markdown and other common formats. Recently, however, the <a href="https://www.youtube.com/watch?v=yvi5uXQMvu4&amp;ab_channel=RStudio">Quarto revolution</a> occurred within the R/Python community and paradoxically also changed the game for literate Stata workflows thanks to the latter’s integration with Python.</p>
</section>
<section id="the-stata-python-quarto-workflow-for-literate-programming" class="level2">
<h2 class="anchored" data-anchor-id="the-stata-python-quarto-workflow-for-literate-programming">The Stata-Python-Quarto Workflow for Literate Programming</h2>
<p><a href="https://quarto.org">Quarto’s</a> release earlier this summer added the missing link to the implementation of the Stata literate programming workflow described below. Quarto is essentially a language agnostic tool that enables data scientists to generate PDFs, Word Documents, dashboards and other outputs from code at the click of a button in R, Python, ObservableJS, etc. While Quarto does not work with Stata out of the box, the tighter integration of Python and Stata with the release of Stata 17 and the PyStata package enables a literate programming workflow!</p>
<p>In what follows, we will look at two options to use Quarto to generate similar static HTML reports based on <a href="https://www.stata.com/new-in-stata/pystata/">this example script</a> introducing Stata17’s new companion PyStata package. We will first look at how to handle classic Jupyter notebooks before moving on to rendering a native <code>.qmd</code> files. Both workflows are almost identical, hence if you’re used to Jupyter notebooks, simply stick to the first one. The second one may be more familiar for those migrating from R as the <code>.qmd</code> file structure is almost identical to the <code>.Rmarkdown</code> file structure.</p>
<p>Want to see the final product before we start? Check out the <a href="../../pages/FirstTestPyStata.html">Jupyter notebook</a> example and the <a href="../../pages/StataPython.html">native Quarto</a> example.</p>
<section id="a-few-prerequisites" class="level3">
<h3 class="anchored" data-anchor-id="a-few-prerequisites">A Few Prerequisites</h3>
<p>Make sure that the following prerequisites are met before reading on. They are the same for rendering both Jupyter notebooks and <code>.qmd</code> files.</p>
<ul>
<li><a href="https://www.python.org/">Python</a></li>
<li><a href="https://code.visualstudio.com/">VSCode</a> (I found that it works best for this workflow due to the Quarto extension)</li>
<li><a href="https://www.stata.com/">Stata 17</a></li>
<li><a href="https://quarto.org/">Quarto</a></li>
<li><a href="https://quarto.org/docs/tools/vscode.html">The Quarto extension for VSCode</a></li>
<li>A few Python libraries and their dependencies:
<ul>
<li><a href="https://pypi.org/project/jupyter/">Jupyter</a> to run Jupyter Notebooks</li>
<li><a href="https://pypi.org/project/stata-setup/">Stata_setup</a> to initialize the PyStata package which is located in the install folder of Stata 17</li>
</ul></li>
</ul>
<p>Additionally to run the example code, install the following packages and their dependencies:</p>
<ul>
<li><a href="https://matplotlib.org/">matplotlib</a></li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new project env:</span></span>
<span id="cb1-2">conda create <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">--</span>name StataPythonQuarto</span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Activate it:</span></span>
<span id="cb1-4">conda activate StataPythonQuarto</span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To run the Jupyter Notebook:</span></span>
<span id="cb1-6">pip install <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>U jupyter</span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stata bridge itself:</span></span>
<span id="cb1-8">pip install <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>U stata_setup</span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For matplotlib graph:</span></span>
<span id="cb1-10">pip install <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>U matplotlib</span></code></pre></div>
</div>
</section>
<section id="setting-up-your-project" class="level3">
<h3 class="anchored" data-anchor-id="setting-up-your-project">Setting up your Project</h3>
<p>Head to VSCode, select your new environment, and open up a new Jupyter notebook or a <code>.qmd</code> file in your project folder. The steps below are exactly the same in both cases, simply insert the code in Jupyter chunks in the first case and in <code>.qmd</code> chunks in the latter case.</p>
<p>The first thing you have to do before starting your data analysis workflow is to set the YAML header containing the metadata of your data science project. If you’re opting for a Jupyter notebook, you’ll need to insert it in a <code>raw</code> chunk at the very beginning of your notebook (see the <a href="../../pages/FirstTestPyStata.html">example notebook</a> for more details). In the case you’re running with a <code>.qmd</code> setup, simply copy put it at the top of your file.</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb2-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Running Stata within a Jupyter Notebook and Rendering to HTML with Quarto.</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">author</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> Bernhard Bieri</span></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"09-02-2022"</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cosmo"</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> html</span></span>
<span id="cb2-7"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div>
<p>The next step is to import the PyStata package located in the install folder of your Stata distribution with the <code>stata_setup.config()</code> function. Simply run the following code chunk after inserting the YAML. Note that you need to specify the Stata version you have installed on your system. <img src="https://latex.codecogs.com/png.latex?%5Cin%20%5C%7B%5Ctext%7Bbe%7D%5C%20;%5Ctext%7Bse%7D%5C%20;%5Ctext%7Bmp%7D%5C%7D">.</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setup Stata from within Python</span></span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> stata_setup</span>
<span id="cb3-3">stata_setup.config(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/Program Files/Stata17"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"be"</span>)</span></code></pre></div>
<p>After this you are good to go!</p>
</section>
<section id="running-the-analysis" class="level3">
<h3 class="anchored" data-anchor-id="running-the-analysis">Running the Analysis</h3>
<p>You can now import your data with your standard Python commands, clean them within Python before running your analysis and produce tables or graphical outputs with your favorite Stata commands.</p>
<blockquote class="blockquote">
<p>See the following <a href="../../pages/FirstTestPyStata.html">Jupyter notebook example</a> and <a href="../../pages/StataPython.html">.qmd example</a> for a complete workflow &amp; output into HTML!</p>
</blockquote>
</section>
<section id="previewing-and-rendering-your-project" class="level3">
<h3 class="anchored" data-anchor-id="previewing-and-rendering-your-project">Previewing and Rendering your Project</h3>
<p>Rendering your report is a one click/one line thing! If you have VSCode and the Quarto extension installed, simply hit render in the menu bar as indicated on the screenshots below. This will execute all the code chunks in the background and weave together tables, graphs, and text. It will then open up a preview in a dedicated tab and save your final output in your root folder.</p>
<p>Bonus Tip: You can hit the <code>CTRL/CMD + Shift + K</code> key-binding to render your report even faster. It works for both Jupyter notebooks and <code>.qmd</code> files.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/render.jpg" class="img-fluid figure-img"></p>
<figcaption>rendercommands</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/livepreview.jpg" class="img-fluid figure-img"></p>
<figcaption>livepreview</figcaption>
</figure>
</div>
</section>
</section>
<section id="further-readings" class="level2">
<h2 class="anchored" data-anchor-id="further-readings">Further Readings</h2>
<p>Here are some useful websites to go further with your workflow.</p>
<ul>
<li><a href="https://quarto.org/docs/guide/">The awesome Quarto Documentation</a></li>
<li><a href="https://appsilon.com/quarto-python-and-vscode">A great Appsilon Blog on Quarto</a></li>
<li><a href="https://www.stata.com/new-in-stata/">What’s new in Stata 17</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2022" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2022. <span>“Literate Programming in Stata.”</span>
August 25, 2022. <a href="https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/">https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/</a>.
</div></div></section></div> ]]></description>
  <category>Stata</category>
  <category>Python</category>
  <category>Tips</category>
  <guid>https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/</guid>
  <pubDate>Thu, 25 Aug 2022 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2022-08-25-litteralprogramminginstata/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Introducing Branches to your {blogdown} Workflow</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2022-08-10-branchesblogdown/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<p>This is a quick tips &amp; tricks post I decided to write after switching my personal website to the <a href="https://github.com/hugo-apero/">Hugo Apero</a> theme by using this <a href="https://www.silviacanelon.com/blog/2021-hello-hugo-apero/">excellent guide</a> by <a href="https://www.silviacanelon.com">Silvia Canelon</a>. In it, she describes how she uses branches to switch from one theme to the other without breaking the initial site. The original idea came from the following Twitter thread by the following Tweet by <a href="https://www.garrickadenbuie.com/">Garrick Aden-Buie</a> and leverages what is known as “Branch Deploys” on Netlify.</p>
<div class="cell">
<div class="cell-output-display">
{{% tweet "1384960915276275715" %}}
</div>
</div>
<p>Previously, I used to write my entire blog post, served it locally to make sure it rendered correctly, and published it on the <code>main</code> branch of my repository. This in turn triggered a build instruction on Netlify which built the site with the new post. While this workflow is fine for simple blogs with a single author, I ended up switching to using branches to compartmentalize individual blog posts, render them on a <em>branch deploy</em> on the web to see the final product and to avoid breaking the main site while experimenting things on branches.</p>
<p>Hence, I switched to the following workflow to write new content:</p>
<ol type="1">
<li>Head to the site’s settings on Netlify and activate branch deploys for <em>all</em> branches.</li>
<li>Create a new branch titled after the blog post I want to write</li>
<li>Create a new blog post using <code>blogdown::new_post()</code></li>
<li>Write the actual post</li>
<li>Commit and push the changes <strong>on the new branch</strong></li>
</ol>
<p>And that’s already it!</p>
<blockquote class="blockquote">
<p>NB: I am unsure why but when selecting specific branches of my repository the branch deploys did not work when I pushed changes to GitHub.</p>
</blockquote>



<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2022" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2022. <span>“Introducing Branches to Your {Blogdown}
Workflow.”</span> August 10, 2022. <a href="https://bernhardbieri.ch/blog/2022-08-10-branchesblogdown/">https://bernhardbieri.ch/blog/2022-08-10-branchesblogdown/</a>.
</div></div></section></div> ]]></description>
  <category>blogdown</category>
  <category>R</category>
  <category>GitHub</category>
  <guid>https://bernhardbieri.ch/blog/2022-08-10-branchesblogdown/</guid>
  <pubDate>Wed, 10 Aug 2022 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2022-08-10-branchesblogdown/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Coursedown: a blogdown template for courses</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<section id="the-philosophy-behind-this-template" class="level2">
<h2 class="anchored" data-anchor-id="the-philosophy-behind-this-template">The philosophy behind this template ?</h2>
<p>Many lecturers, professors and R-aficionados have great scripts living a peaceful but very lonely life on their machines. While they sometimes get shared on the likes of Moodle or via Dropbox, they rarely get the chance of living on a dedicated website since there is often a non-negligible upfront cost in setting it up. This is exactly where <a href="https://coursedown.netlify.app/"><code>{coursedown}</code></a> comes in! Designed to minimize the time banging one’s head over CSS styling options or HTML building blocks, it allows its users to create a content-centric website for their course in less than an hour.</p>
<p>Before we get started, let me give a shoutout to both Alison Hill’s great <a href="https://share-blogdown.netlify.app/">Share Blogdown site</a> and <a href="https://evalsp22.classes.andrewheiss.com/">Andrew Heiss’ awesome course websites</a> which inspired me to create this template! Go check them out!</p>
</section>
<section id="lets-get-started" class="level2">
<h2 class="anchored" data-anchor-id="lets-get-started">Let’s get started!</h2>
<p>We’ll assume that you already have the necessary accounts and software installed for a standard <code>{blogdown}</code> site. If not, you can get started by reading <a href="https://bernhardbieri.ch/blog/quickstart-guide-to-blogdown/">this tutorial</a> or <a href="https://www.apreshill.com/blog/2020-12-new-year-new-blogdown/">this one</a>. It will also assume that you are somewhat familiar with the blogdown <code>{blogdown}</code> publishing workflow. If you need a quick refresher, read <a href="https://www.apreshill.com/blog/2020-12-new-year-new-blogdown/">this great post</a>.</p>
<p>There are essentially three main steps to setting up your site: fork, customize, and share!</p>
<section id="fork" class="level3">
<h3 class="anchored" data-anchor-id="fork">Fork</h3>
<p>The first step in setting up your site is to “fork” it from the <a href="https://github.com/BBieri/coursedown"><code>{coursedown}</code>repository</a>. Note here that the <code>{coursedown}</code> repository has a special structure since it actually is a template repository! Hence the only thing you need to do is to click on the green “Use this template” button and select a name for your repository.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/1_UseTemplate.png" class="img-fluid figure-img"></p>
<figcaption>usetemplate</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/2_CreateRepo.png" class="img-fluid figure-img"></p>
<figcaption>createrepository</figcaption>
</figure>
</div>
<p>Once the repository is created, clone it to your local machine with your Git client. Then, set up the description, the title and the README of your repository to reflect the information of your course. When your repository is set up on GitHub, head to <a href="https://www.netlify.com/">Netlify</a> and deploy your site by linking it to their services. Note that a free plan should be more than enough to cover your needs.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/3_Netlify1.png" class="img-fluid figure-img"></p>
<figcaption>netlifyaddsite</figcaption>
</figure>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/4_Netlify2.png" class="img-fluid figure-img"></p>
<figcaption>netlifyaddsite2</figcaption>
</figure>
</div>
<p>You should not have to change anything in the following site settings. Once once you’re done, click deploy!</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/5_Netlify3.png" class="img-fluid figure-img"></p>
<figcaption>netlifyaddsite3</figcaption>
</figure>
</div>
<p>After waiting a minute or two to let Netlify build your site, you’ll now be able to access your <code>{coursedown}</code> site online with an automatically generated link shown on the Netlify settings page of your site. Congrats! Your site is now online.</p>
</section>
<section id="customize" class="level3">
<h3 class="anchored" data-anchor-id="customize">Customize</h3>
<p>It’s time to make the site yours by filling it with content! Start by editing the different files in the <code>config/_default/</code> directory. These files contain mostly the metadata of your site. All files contain comments explaining the function of the different YAML selectors. If something remains unclear, feel free to <a href="https://github.com/BBieri/coursedown/issues">open an issue</a> on the <code>{coursedown}</code> repo.</p>
<p>The next step is to look at the <code>content</code> directory. As its name suggests, you’ll be able to edit the content of your site here such as the building blocks of your landing page, the posts section you’ll use to organize weekly classes and further metadata about the authors of the site. Let’s start by editing the latter. Head to the <code>content/authors/admin/_index.md</code> file and edit it to make it your own.</p>
<p>Once this is done, check out the <code>content/courses</code> folder. You guessed it, this is where this course lives! These courses are mainly meant as additional resources for students where you can link useful resources for them to learn R or mini scripts on how to perform basic data manipulation tasks for example. Feel free to remove the ones included with the template and add your own!</p>
<p>The <code>content/home</code> folder contains all the building blocks of the main landing page. By default, the <code>{coursedown}</code> template uses the <code>hero.md</code> block with a parralax effect and an additional custom <code>outline.md</code> block. Edit the former to add the course title displayed on the landing page, the course code and describe it in a few words. You can also add an <code>R</code> style course logo if you’re feeling fancy! Simply add the image to the <code>content/home</code> folder and link it in the <code>hero.md</code> file. Note that you should not need to edit the <code>outline.md</code> file as it’s contents were already edited in the <code>config/_default/params.yaml</code> file. Finally, if you want to add a widget present in the original wowchemy template, simply add it in this folder and edit it <sup>1</sup>.</p>
<p>It’s time to create your weekly classes! Head over to the <code>content/post</code> folder and edit/add posts for each one of your course sessions. Each folder contains a featured image and the <code>index.Rmarkdown</code> file that you can edit. Feel free to sumarise the contents you plan on covering during the week in this file and do not forget to create links to your resources (labs and slides) in the <code>yaml</code> header. You could also add hyperlinks to the readings you plan on covering this week to make it even easier for students to access the resources.</p>
<p>The final step in the <code>content</code> folder is to edit the privacy and the terms and conditions page if you’d like to add one. Check out <a href="https://gdpr.eu/privacy-notice/">this EU resource</a> to learn more about writing a GDPR compliant privacy notice.</p>
<p>You’re almost done! It’s now time to add the mantlepiece of your site, the slides and the scripts you prepared for your course and have linked in the <code>content/post</code> directory. These can be easily added to the <code>content/static</code> folder in their corresponding folder. It’s as easy as drag and drop! Take a moment to look at how I integrated the <code>{iheiddown}</code> presentations and some labs borrowed from Alison Hill’s Sharing on short notice template for illustration purposes and make them your own (these will be replaced with coursedown’s own labs shortly). Finally, note that the static folder also holds all other static resources such as pdf’s and images.</p>
</section>
<section id="share" class="level3">
<h3 class="anchored" data-anchor-id="share">Share</h3>
<p>Before sharing your site with students, you might want to change it’s URL. You can easily do this by heading to <a href="https://www.netlify.com/">Netlify</a> and buy a domain. See <a href="https://docs.netlify.com/domains-https/custom-domains/">their documentation</a> on how best to do that.</p>
<p>A neat way to promote your course online is to share a link to your website on Twitter. To make your Twitter post stand out, you could add an OpenGraph image to your GitHub repository. To read up on how to do this exactly head over to <a href="https://github.blog/2021-06-22-framework-building-open-graph-images/">GitHub’s documentation</a>.</p>
</section>
</section>
<section id="wrapping-up-and-going-further" class="level2">
<h2 class="anchored" data-anchor-id="wrapping-up-and-going-further">Wrapping up and going further</h2>
<p>Congratulations! You’ve come to the end of this quick tutorial and should now have a brand new course website set up. Feel free to <a href="mailto:bernhard.bieri@graduateinstitute.ch">email me</a> if you have any feedback or issues with the site. You can also <a href="https://github.com/BBieri/coursedown">open an issue</a> on the GitHub repository. Head over to the <a href="https://coursedown.netlify.app/"><code>{coursedown}</code></a> to learn more!</p>


</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>You can find them in the <code>themes</code> directory if needed.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2022" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2022. <span>“Coursedown: A Blogdown Template for
Courses.”</span> February 2, 2022. <a href="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/">https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/</a>.
</div></div></section></div> ]]></description>
  <category>Guide</category>
  <category>blogdown</category>
  <category>R</category>
  <guid>https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/</guid>
  <pubDate>Wed, 02 Feb 2022 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2022-02-02-coursedown-a-blogdown-template-for-courses/featured.png" medium="image" type="image/png" height="73" width="144"/>
</item>
<item>
  <title>ERGMs and Co-Offending: an Applied Social Networks Project</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>This blog post constitutes the second part of the course evaluation of a social networks class I took while pursuing my masters’ in International Economics at IHEID. We have explored the process of describing a network in a previous post that you can read <a href="https://bernhardbieri.ch/blog/marvellous-social-networks-an-applied-project/">here</a>. Instead, we will focus on understanding <em>why</em> certain patterns of ties are more likely to appear than others. Since we have cross-sectional data, we’ll use an Exponential family of Random Graph Model (ERGM).</p>
<section id="introducing-the-data" class="level3">
<h3 class="anchored" data-anchor-id="introducing-the-data">Introducing the data</h3>
<p>The dataset that we will use to illustrate ERGMs in this post is a cross-sectional dataset on co-offending patterns of an inner city street-gang that was observed between 2005 and 2009. The data can be accessed <a href="http://www.casos.cs.cmu.edu/tools/datasets/external/index.php">here</a> under the London Gang section <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span>. Let’s take a look at the data!</p>
<div class="cell" data-layout-align="center" data-skimr_include_summary="false">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################### Import data ########################################</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import adjacency matrix</span></span>
<span id="cb1-3">gangs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONDON_GANG.csv"</span>)[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import list of attributes</span></span>
<span id="cb1-6">gangs_attr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONDON_GANG_ATTR.csv"</span>)[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check out the data</span></span>
<span id="cb1-9">skimr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">skim</span>(gangs_attr)</span></code></pre></div>
<div class="cell-output-display">
<p><strong>Variable type: numeric</strong></p>
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 18%">
<col style="width: 12%">
<col style="width: 18%">
<col style="width: 7%">
<col style="width: 6%">
<col style="width: 3%">
<col style="width: 5%">
<col style="width: 5%">
<col style="width: 7%">
<col style="width: 6%">
<col style="width: 7%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">skim_variable</th>
<th style="text-align: right;">n_missing</th>
<th style="text-align: right;">complete_rate</th>
<th style="text-align: right;">mean</th>
<th style="text-align: right;">sd</th>
<th style="text-align: right;">p0</th>
<th style="text-align: right;">p25</th>
<th style="text-align: right;">p50</th>
<th style="text-align: right;">p75</th>
<th style="text-align: right;">p100</th>
<th style="text-align: left;">hist</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Age</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">19.83</td>
<td style="text-align: right;">2.69</td>
<td style="text-align: right;">16</td>
<td style="text-align: right;">18</td>
<td style="text-align: right;">19</td>
<td style="text-align: right;">21.00</td>
<td style="text-align: right;">27</td>
<td style="text-align: left;">▇▇▃▂▂</td>
</tr>
<tr class="even">
<td style="text-align: left;">Birthplace</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">2.44</td>
<td style="text-align: right;">0.96</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">3.00</td>
<td style="text-align: right;">4</td>
<td style="text-align: left;">▃▃▁▇▂</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Residence</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.37</td>
<td style="text-align: right;">0.49</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">▇▁▁▁▅</td>
</tr>
<tr class="even">
<td style="text-align: left;">Arrests</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">9.91</td>
<td style="text-align: right;">6.44</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">14.75</td>
<td style="text-align: right;">23</td>
<td style="text-align: left;">▅▇▃▃▃</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Convictions</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">4.20</td>
<td style="text-align: right;">3.66</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">7.00</td>
<td style="text-align: right;">13</td>
<td style="text-align: left;">▇▆▂▃▂</td>
</tr>
<tr class="even">
<td style="text-align: left;">Prison</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.44</td>
<td style="text-align: right;">0.50</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1.00</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">▇▁▁▁▆</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Music</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0.17</td>
<td style="text-align: right;">0.38</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.00</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">▇▁▁▁▂</td>
</tr>
<tr class="even">
<td style="text-align: left;">Ranking</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">3.46</td>
<td style="text-align: right;">0.82</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">4.00</td>
<td style="text-align: right;">5</td>
<td style="text-align: left;">▁▂▅▇▁</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The data is comprised of two <code>.csv</code> documents. The first one contains an adjacency matrix describing the co-offending patterns of two individuals in the network. The second one provides us with more information about the individual nodes in the form of different attributes, such as the age or the ethnicity (measured by birthplace) of the individuals in the network for example.</p>
</section>
<section id="the-theoretical-intuition-behind-co-offending" class="level3">
<h3 class="anchored" data-anchor-id="the-theoretical-intuition-behind-co-offending">The theoretical intuition behind co-offending</h3>
<p>A sound theoretical understanding of the theory underpinning the sociological study of criminality is required before considering network models. Without going into too much detail, one can summarize the key relevant elements in the criminal sociology literature as follows:</p>
<ul>
<li>Co-offending patterns vary among types of crime. Co-offending is more a characteristic of juvenile than adult criminality and is often short-lived. Co-offenders tend to have many accomplices as a consequence. <span class="citation" data-cites="reiss1988co">(Reiss Jr 1988)</span></li>
<li>Gangs form as a result of a convergence of collective interests and then form a social identity and a group structure through the repeated interaction between individuals <span class="citation" data-cites="papachristos2013corner wasserman1994social">(Papachristos, Hureau, and Braga 2013; Wasserman, Faust, et al. 1994)</span>.</li>
<li>Grund and Densley therefore argue that it is important to conceptualize the gang as a network to shed light into the patterns driving the organisational structure of the network, most notably the role of homophily in co-offending patterns <span class="citation" data-cites="grund2012ethnic grund2015ethnic">(Grund and Densley 2012, 2015)</span>.</li>
</ul>
<p>Note that while this list is not exhaustive by any means, it gives a sufficient overview of the theoretical framework for us to analyse the network in more detail. A more curious reader might want to read the papers by Grund and Densley on which the dataset is based and which provides an excellent overview of the literature <span class="citation" data-cites="grund2012ethnic grund2015ethnic">(Grund and Densley 2012, 2015)</span>.</p>
</section>
<section id="a-primer-on-ergms" class="level3">
<h3 class="anchored" data-anchor-id="a-primer-on-ergms">A primer on ERGMs</h3>
<p>Before diving into the data analysis itself, let us remind ourselves of the key elements of the theory of ERGMs and why they are helpful in understanding the pattern of ties in this cross-sectional network. The guts of the model can be intuitively explained by looking at the components of the following function:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP(X%20=%20x%20%5Cmid%20%5Ctheta)%20=%0A%5Cfrac%7B%5Cleft.%5Cexp%20%5Cleft(%5Csum_%7Bk%7D%0A%5Chat%7B%5Ctheta%7D_%7Bk%7D%5Cright)%5Cleft(z_%7Bk%7D(x)%5Cright)%5Cright)%7D%0A%7Bk%7D%0A"></p>
<p>Where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?P(X=x%20%5Cmid%20%5Ctheta)"> is the probability of observing network <img src="https://latex.codecogs.com/png.latex?x"> conditional on the weighting parameter <img src="https://latex.codecogs.com/png.latex?%5Ctheta"></li>
<li><img src="https://latex.codecogs.com/png.latex?%5Cleft.%5Cexp%20%5Cleft(%5Csum_%7Bk%7D%20%5Chat%7B%5Ctheta%7D_%7Bk%7D%5Cright)%5Cleft(z_%7Bk%7D(x)%5Cright)%5Cright)"> can be broken down into:
<ul>
<li><img src="https://latex.codecogs.com/png.latex?z_%7Bk%7D(x)"> a vector of network characteristics such as node attributes or network properties (think homophily, transitivity, etc.)</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bk%7D%20%5Chat%7B%5Ctheta%7D_%7Bk%7D"> which is a vector of coefficients corresponding to the network characteristics we included in the <img src="https://latex.codecogs.com/png.latex?z_%7Bk%7D(x)"> vector.</li>
</ul></li>
<li><img src="https://latex.codecogs.com/png.latex?%5Ckappa"> is a normalizing constant.</li>
</ul>
<p>However note that the normalizing constant <img src="https://latex.codecogs.com/png.latex?%5Ckappa"> can become very large depending on the included characteristics and the size of the network as is apparent in the following equation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ckappa=%5Csum_%7Bx%5E%7B%5Cprime%7D%20%5Cin%20X%7D%20%5Cexp%20%5Cleft(%5Csum_%7Bk%7D%20%5Ctheta_%7Bk%7D%20z_%7Bk%7D%5Cleft(x%5E%7B%5Cprime%7D%5Cright)%5Cright)%0A"> That’s a lot of networks to estimate, even if the number of vertices is relatively small! We’ll therefore use a Markov Chain Monte Carlo estimation technique which (if it behaves well) generates a stationary distribution of graphs with the right characteristics <img src="https://latex.codecogs.com/png.latex?z_%7Bk%7D%5Cleft(x%5E%7B%5Cprime%7D%5Cright)">. This process should generate a distribution with low autocorrelation (between generated networks) and provides a representative distribution of the sample space.</p>
<p>Finally, let us make a rapid comment about <a href="https://link.springer.com/article/10.1007/s11336-007-9016-1">Network Linear Regression models</a> and why they are not ideal to answer the question at hand. While they can provide insight about the impact of homophily in tie formation, they take <em>random</em> networks of the same size as a baseline. The main point Grund and Densley make is that the impact of adding transitivity (a measure of network structure rather than attributes) to the baseline may lead to a reduction of the pure effect of homophily in co-offending patterns. In a nutshell, omitting network structure from the baseline graphs, leads to a positive bias of homophily on co-offending due to the fact that if an individual offends with two people, it is more likely that these two co-offenders offend together as well. Since MRQAP designs cannot incorporate transitivity measures, the authors turned to ERGMs.</p>
</section>
<section id="getting-a-first-look-at-the-graph" class="level3">
<h3 class="anchored" data-anchor-id="getting-a-first-look-at-the-graph">Getting a first look at the graph</h3>
<p>After having gone trough this theoretical introduction, we can confidently move forward with our analysis by importing our networks in the various formats (<code>{igraph}</code>, <code>{tidygraph}</code>, and <code>{network}</code>), visualizing it and describing the main features of the network. Note that a particular focus will be given on exploring homophily due to the theoretical considerations laid out above. Note that for the analytical purposes, we have generated a vector of random names and named the different vertices, these names are not in the original dataset! Finally, note that the authors have removed 6 nodes from the original network which we were not able to identify. This will lead to different results in the analytical part where we run the ERGMs and try to replicate their findings.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="forceNetwork html-widget html-fill-item" id="htmlwidget-0af0286950321451f50b" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-0af0286950321451f50b">{"x":{"links":{"source":[19,18,20,24,1,6,8,10,27,9,11,2,22,15,7,0,21,1,0,24,34,13,33,3,21,32,27,11,30,2,4,5,12,2,3,7,6,1,11,17,42,6,21,20,7,13,11,0,9,6,1,8,4,2,0,7,0,1,2,3,4,6,22,10,46,24,23,21,20,22,10,0,11,3,6,1,8,4,9,0,4,5,1,0,2,3,22,24,23,25,3,5,4,33,11,34,31,35,21,32,41,0,19,18,0,2,13,1,9,24,1,8,22,21,7,20,9,13,1,20,8,7,0,6,2,10,11,0,12,11,13,6,9,10,7,1,2,8,3,2,8,5,4,1,9,22,21,6,0,2,1,2,21,11,13,32,19,18,1,6,16,5,0,3,13,11,4,2,6,11,10,13,21,3,4,9,8,20,7,1,0,5,1,9,0,8,6,7,4,1,6,0,2,7,42,44,0,8,6,41,11,42,3,2,27,30,21,32,33,3,13,11,24,13,14,12,6,1,1,9,20,13,22,8,25,15,0,23,21,24,10,3,0,10,9,4,2,0,5,7,6,1,8,3,19,17,41,18,10,42,0,9,8,18,9,19,3,8,22,21,24,23,13,9,18,2,10,19,4,6,24,30,21,22,20,28,27,13,1,28,15,4,5,0,3,12,5,4,3,13,2,3,6,4,17,10,5,2,3,0,3,9,8,0,4,17,18,5,10,11,6,30,24,2,13],"target":[41,41,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,2,2,35,35,35,35,35,35,35,35,35,35,35,13,13,13,13,13,13,13,13,13,52,52,26,26,26,26,26,26,26,26,9,9,9,9,9,9,9,5,5,5,5,5,47,47,47,47,47,24,24,24,24,24,24,24,24,24,24,24,24,24,44,6,6,6,6,6,6,51,51,51,51,48,48,48,36,36,36,36,36,36,36,42,42,39,39,3,3,37,37,30,30,30,30,30,30,30,30,21,21,21,21,21,21,21,21,21,21,21,20,20,20,20,20,20,20,20,20,20,20,12,12,12,12,12,12,12,23,23,7,7,7,7,33,33,33,33,33,49,49,17,17,17,17,17,17,17,17,17,17,22,22,22,22,22,22,22,22,22,22,22,22,22,22,10,10,10,10,10,10,8,8,8,8,8,8,45,45,45,45,45,43,43,43,43,34,34,34,34,34,34,34,34,34,34,15,15,15,15,15,27,27,27,27,27,27,27,27,27,27,27,27,27,27,1,11,11,11,11,11,11,11,11,11,11,11,50,50,50,50,50,50,50,38,38,40,40,40,40,40,25,25,25,25,53,53,53,53,53,53,46,46,31,31,31,31,31,31,29,29,29,29,29,16,16,16,16,14,14,14,14,14,14,18,18,18,18,18,18,4,4,4,19,19,19,19,19,19,19,19,19,32,32,32,32,32,32],"colour":["#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666","#666"]},"nodes":{"name":["Rajeana","Mekel","Alexa","Farhaan","Tony","Brendan","Catrina","Kayla","Loreel","Ashley","Lia","Michelle","Justin","Anjanetta","Tawfeeqa","Maria","Susan","Lacey","Thinh","Tran","Jonah","Jennifer","Lauren","Kaitlyn","Byron","Phillip","Arrlando","Mason","Adonis","Sarah","Jacob","Samantha","Zachary","Kelsey","Marcus","Ammaar","Dominic","Hamood","Muammar","Enrique","Phaedra","Aaron","Dru","Marcello","Carlee","Lutfiyya","Rasheeda","Brittany","Christopher","Kiana","Mu'mina","Chager","Arneka","Rachel"],"group":["West Africa","Caribbean","Caribbean","Caribbean","Caribbean","UK","East Africa","West Africa","West Africa","West Africa","West Africa","UK","UK","UK","UK","UK","Caribbean","Caribbean","Caribbean","West Africa","West Africa","West Africa","East Africa","East Africa","East Africa","East Africa","UK","UK","Caribbean","Caribbean","West Africa","West Africa","UK","UK","UK","UK","UK","Caribbean","UK","West Africa","Caribbean","UK","UK","UK","UK","UK","UK","UK","Caribbean","West Africa","UK","UK","East Africa","UK"],"nodesize":[16,16,12,8,11,17,8,15,9,12,16,5,19,23,12,14,14,23,22,8,4,5,19,0,8,0,3,2,7,6,12,13,15,2,7,7,6,6,4,18,8,7,6,7,23,5,1,12,3,3,4,1,20,11]},"options":{"NodeID":"name","Group":"group","colourScale":"d3.scaleOrdinal(d3.schemeCategory10);","fontSize":18,"fontFamily":"serif","clickTextSize":45,"linkDistance":75,"linkWidth":"function(d) { return Math.sqrt(d.value); }","charge":-30,"opacity":0.8,"zoom":false,"legend":true,"arrows":false,"nodesize":true,"radiusCalculation":" Math.sqrt(d.nodesize)+6","bounded":false,"opacityNoHover":0,"clickAction":null}},"evals":[],"jsHooks":[]}</script>
<p>Homophily in Criminal Networks in 2D (hover over me!)</p>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="scatterplotThree html-widget html-fill-item" id="kWOPqYur1l" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="kWOPqYur1l">{"x":{"NROW":54,"height":null,"width":null,"axis":false,"numticks":[6,6,6],"xticklabs":null,"yticklabs":null,"zticklabs":null,"color":[["#E5243B","#DDA63A","#DDA63A","#DDA63A","#DDA63A","#4C9F38","#C5192D","#E5243B","#E5243B","#E5243B","#E5243B","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#DDA63A","#DDA63A","#DDA63A","#E5243B","#E5243B","#E5243B","#C5192D","#C5192D","#C5192D","#C5192D","#4C9F38","#4C9F38","#DDA63A","#DDA63A","#E5243B","#E5243B","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#DDA63A","#4C9F38","#E5243B","#DDA63A","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#4C9F38","#DDA63A","#E5243B","#4C9F38","#4C9F38","#C5192D","#4C9F38"]],"size":[0.7619047619047619,0.7619047619047619,0.5714285714285714,0.3809523809523809,0.5238095238095238,0.8095238095238095,0.3809523809523809,0.7142857142857143,0.4285714285714285,0.5714285714285714,0.7619047619047619,0.2380952380952381,0.9047619047619048,1.095238095238095,0.5714285714285714,0.6666666666666667,0.6666666666666667,1.095238095238095,1.047619047619048,0.3809523809523809,0.1904761904761905,0.2380952380952381,0.9047619047619048,0,0.3809523809523809,0,0.1428571428571428,0.09523809523809523,0.3333333333333334,0.2857142857142857,0.5714285714285714,0.619047619047619,0.7142857142857143,0.09523809523809523,0.3333333333333334,0.3333333333333334,0.2857142857142857,0.2857142857142857,0.1904761904761905,0.8571428571428571,0.3809523809523809,0.3333333333333334,0.2857142857142857,0.3333333333333334,1.095238095238095,0.2380952380952381,0.04761904761904762,0.5714285714285714,0.1428571428571428,0.1428571428571428,0.1904761904761905,0.04761904761904762,0.9523809523809524,0.5238095238095238],"stroke":"black","flipy":true,"grid":false,"renderer":"auto","signif":8,"bg":"#DAD7CD","cexsymbols":1,"xlim":[-1,1],"ylim":[-1,1],"zlim":[-1,1],"axisscale":[1,1,1],"pch":["@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@","@"],"elementId":"kWOPqYur1l","from":[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,15,15,15,16,17,17,17,17,18,18,18,18,18,18,18,19,19,19,19,19,19,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,22,23,23,23,23,24,24,24,24,24,24,24,24,24,24,25,25,27,27,27,27,28,28,30,30,30,30,31,32,32,32,32,33,33,33,34,34,35,41,41,41,42,42,42,42,44,46]],"to":[[1,2,3,4,5,6,7,8,9,10,11,16,17,19,20,21,22,24,26,27,28,42,44,45,50,2,5,6,7,8,9,10,11,12,13,15,17,20,21,22,24,27,28,29,30,37,3,4,5,6,7,8,9,11,12,13,14,17,20,21,28,32,33,34,35,53,4,5,6,11,12,13,14,16,17,18,19,22,24,27,34,35,40,43,48,5,6,8,9,11,12,13,14,16,17,18,19,22,24,46,48,6,11,12,13,14,16,17,18,19,22,48,7,8,9,10,11,13,15,17,18,20,21,22,24,26,28,32,45,46,47,8,9,10,11,13,20,21,22,26,28,30,9,10,11,12,19,20,21,22,24,27,28,30,38,40,45,10,11,12,19,20,21,22,24,26,27,28,30,38,40,53,11,18,19,20,21,22,24,27,28,47,50,53,13,17,20,21,22,24,26,28,32,33,34,35,36,43,13,14,15,20,14,15,17,20,21,22,26,27,29,32,33,34,35,37,53,15,27,28,29,17,18,19,50,52,19,39,40,41,49,50,53,39,40,41,49,50,53,21,22,24,26,27,28,30,31,22,23,24,25,26,27,28,30,31,33,34,35,36,23,24,25,27,28,30,31,47,51,24,25,27,51,25,27,28,30,31,32,34,35,47,51,27,51,28,29,34,35,29,31,31,32,34,35,36,33,34,35,36,34,35,36,35,36,36,42,43,50,43,45,50,52,45,47]],"lwd":1,"linealpha":1,"center":true,"main":[""],"options":true,"alpha":[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]],"vertices":[[0.14437915,-0.23731583,-0.093879371,-0.24119365,-0.42671963,-0.019221468,-0.27933223,-0.12058693,0.11204657,-0.045983018,-0.049253805,0.22319236,-0.10491589,0.088344096,0.26425536,-0.068525498,-0.085531784,0.36107448,-0.10919651,-0.062164311,-0.074726759,-0.08157503100000001,-0.40913751,-0.226599,-0.0053296892,-0.14084254,-0.26773124,-0.060864759,-0.057224329,-0.29974715,0.061180305,0.082015632,-0.12345227,-0.24233917,-0.230119,0.010847839,-0.1505235,-0.37819363,0.32828176,-0.33683647,-0.32051288,0.19248825,-0.34169447,-0.40749211,0.59417788,-0.37037067,-0.73599729,0.08406925899999999,0.14900496,-0.023174709,0.65812372,0.29474803,-0.061519182,0.34870371,0.46727224,0.33711419,0.26362479,0.38645127,0.24043246,0.081367391,-0.19296314,-0.3111255,-0.320247,-0.34728398,-0.20230778,-0.26786207,-0.30391429,0.044685597,-0.28635967,-0.52466914,0.14993251,-0.70528667,-0.38184184,0.080786656,-0.36839751,-0.61390038,0.23157924,-0.59661749,-0.062070616,-0.64904549,-0.41622375,-0.43709013,-0.39124171,-0.22931654,-0.30976014,-0.52424892,-0.26624248,-0.51107056,-0.8517068800000001,-0.13804932,-0.46555357,-0.29106504,-0.43100228,-0.66181943,-0.42868117,-0.62008517,-0.67194137,-0.076603227,-0.09724721,-0.8014732,-0.26011214,0.21749305,-0.6523881,-0.27510014,0.044573034,-0.67797137,-0.08250484900000001,0.08022550000000001,-1,-0.32815879,-0.18071119,-0.53262221,-1,0.42450826,0.20478366,-0.0071794539,-1,0.85621897,0.83095179,0.51620688,0.41984324,0.47907582,-0.24806173,0.97943619,0.092012678,0.35148773,0.96745255,-0.33084021,0.1781146,0.68212443,-0.31565213,0.39918021,0.9400708800000001,-0.8032906,-0.48996063,0.69497855,-0.49162844,-0.25961554,-0.30085237,1,0.11248588,-0.30453012,0.66566604,-0.34800286,-0.13196157,0.27462585,1,1,0.97418593,0.20264179,0.73677454,0.021338885,0.16515555,-0.73963819,0.48326958,-0.95201081,0.95100672,-0.27387582,0.75912883,0.06713453599999999,0.32081483,0.17429531]],"xticklab":["-1.00","-0.60","-0.20","0.20","0.60","1.00"],"yticklab":["-1.00","-0.60","-0.20","0.20","0.60","1.00"],"zticklab":["1.00","0.60","0.20","-0.20","-0.60","-1.00"],"xtick":[0,0.2,0.4,0.6000000000000001,0.8,1],"ytick":[0,0.2,0.4,0.6000000000000001,0.8,1],"ztick":[0,0.2,0.4,0.6000000000000001,0.8,1],"axislength":[1,1,1]},"evals":[],"jsHooks":[]}</script>
<p>Homophily in Criminal Networks in 3D (turn me!)</p>
</div>
</div>
<p>Just like the authors <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span>, we observe a certain level of homophily along ethnic lines. In other words, individuals in this network, seem to tend to co-offend more frequently with members of the same origin.</p>
</section>
<section id="laying-out-the-hypotheses-and-taking-a-closer-look-at-homophily" class="level3">
<h3 class="anchored" data-anchor-id="laying-out-the-hypotheses-and-taking-a-closer-look-at-homophily">Laying out the hypotheses and taking a closer look at homophily</h3>
<p>Before moving to the inferential analysis with an ERGM, let us investigate the role of homophily in the network beyond the simple visual inspection above <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span>. We will therefore start by looking at the three central hypothesis that we will try to test with the ERGM models <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span>:</p>
<ul>
<li>“Gang members are more likely to offend with each other when they have the same ethnic background.”</li>
<li>“Triad closure accounts for some ethnic homogeneity in co-offending in gangs.”</li>
<li>“Gang members are even more likely to offend with each other when they have the same ethnic background AND share another co-offender from the same ethnic background.”</li>
</ul>
<p>In the following graph, we see that walktrap clustering seems indeed to be happening along ethnic lines again. At this point we could run an MRQAP or network linear regression but as explained at the end of the theoretical part above, the main point of the authors is to show that the <em>structure</em> of the network biases naive estimates of homophily. Thus we’ll move onto the more elaborate ERGM models.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/staticDescriptive_Statistics-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="running-the-exponential-family-random-graph-model-ergm" class="level3">
<h3 class="anchored" data-anchor-id="running-the-exponential-family-random-graph-model-ergm">Running the Exponential family Random Graph Model (ERGM)</h3>
<p>On table 2 (p.362) of the paper, the authors present four ERGM models that can be summarized in term of included RHS network characteristics as follows:</p>
<ul>
<li>Model 1: Edge count and homophily (ethnicity)</li>
<li>Model 2: Edge count, homophily (ethnicity), and triadic closure (GWESP, fixed decay parameter at 0)</li>
<li>Model 3: Edge count, homophily (ethnicity), triadic closure (GWESP, fixed decay parameter at 0), and ethnicity dummies</li>
<li>Model 4: Edge count, homophily (ethnicity), triadic closure (GWESP, fixed decay parameter at 0), ethnicity dummies and a custom interaction term between triadic closure and homophily computed using simulations.</li>
</ul>
<p>The present post focuses essentially on the first three models which correspond to the second, third and fourth model in the table below. We simply added an additional “super naive model” containing only an edge count on the RHS as our first model. We report and interpret goodness of fit statistics on the third model below. Finally note that we will unfortunately not get the exact same results as the authors since their sample only contains 48 individuals instead of the full 54 they include in the dataset.</p>
<p>To get familiar with the coefficient interpretation, let us start by considering the naive model where we only included an edge count on the RHS of the ERGM. As we saw above, we need to transform the edge coefficient slightly before interpreting it since the reported coefficients are conditional log-odds of the existence of a tie. This process is similar to the interpretation of logit models. Following the transformation (outlined in the code below), we can interpret the coefficient as adding one more tie changes the log-odds (conditional on all other parameters) of the presence of a tie by <img src="https://latex.codecogs.com/png.latex?0.22">. Unsurprisingly, we see that the coefficient corresponds to the density of the network (i.e.&nbsp;the proportion of potential ties that are actually present in the graph). In our case, we would interpret this coefficient as the “constant” of our regression model.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">Untransformed</th>
<th style="text-align: right;">Transformed</th>
<th style="text-align: right;">Density</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">edges</td>
<td style="text-align: right;">-1.264933</td>
<td style="text-align: right;">0.2201258</td>
<td style="text-align: right;">0.2201258</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Now that we know how to interpret the coefficients, it is time to look at the goodness of fit (GOF) measures that run simulations and show us how well our model captures the characteristics of the observed network. We start by taking a visual comparison between the observed network and a simulation. We immediately note that the observed network is much denser and does not capture the characteristics of our observed network very well.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/staticGOF_Visual_Naive-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>After getting this visual intuition, we proceed to check the GOF quantitatively. Thankfully, the <code>{ergm}</code> package has a nifty function that automates the simulations and returns some key metrics that we can use to decide whether our model fits the data. In this case, the results confirm the visual inspection we did above. We compare the first moment estimates of the simulated networks (in blue) with the actual observed value (in black) in every graph. Simply put, a good fit is indicated by an overlap of the two lines. We are thus underestimating low and high degrees and overestimating medium ones. Furthermore, low edge-wise shared partners seem to be overestimated while high ones are underestimated. The triad census measure seems to be underestimated for 0 closure and 2 and overestimated for 1 and 3. All in all, we see that our naive model is not a very good fit at all!</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/staticGOF_analytical_Naive-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Let us therefore proceed to estimate the more complex models that the authors suggest <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span> to see whether more complex structural elements are at play here. We will interpret the various models in the following lines before turning to the GOF diagnostics in the next section. Consistent with the authors, we see that homophily plays a significant role in the tie formation process/co-offending in all three specifications. Having a positive coefficient here implies that individuals tend to co-offend with others of the same ethnicity, confirming earlier results of the authors <span class="citation" data-cites="grund2012ethnic">(Grund and Densley 2012)</span>. This confirms the first hypothesis we laid out above. Secondly, we see that triad closure defined as a GWESP measure with zero decay. positively impacts the probability of a tie and is significant throughout the specifications. This implies that if an individual offends with two others, the latter are more likely to offend together. Note that this result does not allow us to confirm the second hypothesis of the authors since the point estimate of the homophily coefficient does not change. This in turn is likely a factor of considering the full sample where they only considered 48 out of the 54 individuals in the paper. The third and final specification contains attribute dummies to account for “ethnic group effects” (note that the baseline category is West Africa). Adding these effects reinforces both the triad closure and the homophily coefficient. Finally, note that we cannot confirm or deny the third hypothesis we laid out above since we were not able to replicate the creation of the new interaction statistic between triad closure and homophily. AIC and BIC both indicate that the last model has the lowest prediction error f the four. We will therefore move forward by considering this model in the GOF checks.</p>
<div class="cell" data-layout-align="center">
<table class="texreg" style="margin: 10px auto;border-collapse: collapse;border-spacing: 0px;caption-side: bottom;color: #000000;border-top: 2px solid #000000;">
<caption>
ERGM results table
</caption>
<thead>
<tr>
<th style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</th>
<th style="padding-left: 5px;padding-right: 5px;">
Naive Model
</th>
<th style="padding-left: 5px;padding-right: 5px;">
Model 1
</th>
<th style="padding-left: 5px;padding-right: 5px;">
Model 2
</th>
<th style="padding-left: 5px;padding-right: 5px;">
Model 3
</th>
</tr>
</thead>
<tbody>
<tr style="border-top: 1px solid #000000;">
<td style="padding-left: 5px;padding-right: 5px;">
Edges
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-1.26<sup>***</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-1.38<sup>***</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-3.95<sup>***</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-3.23<sup>***</sup>
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.06)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.08)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.66)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.73)
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
Nodematch(Ethnicity)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
0.36<sup>***</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
0.34<sup>**</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
0.76<sup>***</sup>
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.14)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.13)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.14)
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
GWESP(decay = 0)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
2.32<sup>***</sup>
</td>
<td style="padding-left: 5px;padding-right: 5px;">
2.30<sup>***</sup>
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.64)
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.70)
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
Carribean
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-0.28<sup>**</sup>
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.13)
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
U.K.
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-0.81<sup>***</sup>
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.11)
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
East Africa
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
0.01
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
&nbsp;
</td>
<td style="padding-left: 5px;padding-right: 5px;">
(0.16)
</td>
</tr>
<tr style="border-top: 1px solid #000000;">
<td style="padding-left: 5px;padding-right: 5px;">
AIC
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1510.47
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1505.66
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1479.23
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1425.89
</td>
</tr>
<tr>
<td style="padding-left: 5px;padding-right: 5px;">
BIC
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1515.73
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1516.19
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1495.03
</td>
<td style="padding-left: 5px;padding-right: 5px;">
1457.49
</td>
</tr>
<tr style="border-bottom: 2px solid #000000;">
<td style="padding-left: 5px;padding-right: 5px;">
Log Likelihood
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-754.23
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-750.83
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-736.62
</td>
<td style="padding-left: 5px;padding-right: 5px;">
-706.94
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td style="font-size: 0.8em;" colspan="5">
<sup>***</sup>p &lt; 0.01; <sup>**</sup>p &lt; 0.05; <sup>*</sup>p &lt; 0.1
</td>
</tr>
</tfoot>
</table>
</div>
<p>The GOF checks for the third model still indicate that we could do better to fit the model to the observed data as we underestimate low and high degrees and overestimate middle ones. The edge-wise shared partner simulations still overestimate low values and underestimate high ones. Triad census values are better for 0 and 1 but worse for 3 and 4. It is therefore necessary to stress that the authors have reported better GOF estimates for their fourth model which we were not able to compute (see <span class="citation" data-cites="grund2015ethnic">Grund and Densley (2015)</span> p.363).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/staticGOF-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>The main goal of this lengthy post was to walk the reader through an ERGM analysis by providing a practical example in the form of a replication attempt of a paper on the impact of ethnic homophily and triad closure on co-offending patterns <span class="citation" data-cites="grund2015ethnic">(Grund and Densley 2015)</span>. We were able to replicate part of the paper and confirm the first hypothesis of the authors as well as infer that triad closure has a significant effect on these tie patterns. Note that we were unfortunately not able to replicate the fourth model the authors estimated nor were we able to determine which 6 nodes were taken out of the sample. This leads our results to be somewhat disappointing. Interesting avenues for further research in the area would include the creation of a panel or time series dataset which would allow us to apply new models such as stochastic actor oriented models or DyNAMs to uncover the dynamic nature of co-offending patterns.</p>
</section>
<section id="appendix" class="level2">
<h2 class="anchored" data-anchor-id="appendix">Appendix</h2>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################### Set chunk options ##################################</span></span>
<span id="cb2-2">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>opts_chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eval =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">echo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fig.align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb2-5">                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># out.width = "95%",</span></span>
<span id="cb2-6">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fig.path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"static"</span></span>
<span id="cb2-7">                      )</span>
<span id="cb2-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################### Load packages ######################################</span></span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages(librarian) # If necessary</span></span>
<span id="cb2-11">pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"randomNames"</span>,</span>
<span id="cb2-12">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"igraph"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skimr"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ergm"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sna"</span>,</span>
<span id="cb2-13">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coda"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"latticeExtra"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"texreg"</span>,</span>
<span id="cb2-14">          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"manynet"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"oaqc"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"concaveman"</span>)</span>
<span id="cb2-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># library(migraph)</span></span>
<span id="cb2-16">librarian<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">shelf</span>(pkgs)</span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set seed for replicability:</span></span>
<span id="cb2-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2193</span>)</span>
<span id="cb2-19"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################### Import data ########################################</span></span>
<span id="cb2-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import adjacency matrix</span></span>
<span id="cb2-21">gangs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONDON_GANG.csv"</span>)[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb2-22"></span>
<span id="cb2-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Import list of attributes</span></span>
<span id="cb2-24">gangs_attr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LONDON_GANG_ATTR.csv"</span>)[,<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check out the data</span></span>
<span id="cb2-27">skimr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">skim</span>(gangs_attr)</span>
<span id="cb2-28"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################### Create graph #######################################</span></span>
<span id="cb2-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create igraph object:</span></span>
<span id="cb2-30">netigraph <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_igraph</span>(gangs)</span>
<span id="cb2-31"></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate random names</span></span>
<span id="cb2-33">gen.names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, ...){</span>
<span id="cb2-34">  names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(randomNames<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">randomNames</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n, ...))</span>
<span id="cb2-35">  need <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(names)</span>
<span id="cb2-36">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (need <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) { </span>
<span id="cb2-37">    names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(randomNames<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">randomNames</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> need, ...), names))</span>
<span id="cb2-38">    need <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(names)</span>
<span id="cb2-39">    }</span>
<span id="cb2-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(names)</span>
<span id="cb2-41">}</span>
<span id="cb2-42">gangmemnames <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gen.names</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(gangs), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">which.names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>)</span>
<span id="cb2-43"></span>
<span id="cb2-44">gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ethnicity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb2-45">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Birthplace <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"West Africa"</span>,</span>
<span id="cb2-46">                 dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Birthplace <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Caribbean"</span>,</span>
<span id="cb2-47">                                dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Birthplace <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UK"</span>,</span>
<span id="cb2-48">                                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"East Africa"</span>)))</span>
<span id="cb2-49"></span>
<span id="cb2-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add attributes to the network</span></span>
<span id="cb2-51"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(netigraph)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gangmemnames</span>
<span id="cb2-52">netigraph <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> netigraph <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-53">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-54">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"birthplace"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Birthplace) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-55">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ethnicity) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-56">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"residence"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Residence) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-57">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"arrests"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Arrests) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-58">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"convictions"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Convictions) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-59">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prison"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Prison) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb2-60">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"music"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Music) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-61">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ranking"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Ranking)</span>
<span id="cb2-62">  </span>
<span id="cb2-63"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Notes related to the definition of the vertex attributes: ##</span></span>
<span id="cb2-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Age of the individual</span></span>
<span id="cb2-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Birthplace of the individual:</span></span>
<span id="cb2-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   - 1 = West Africa</span></span>
<span id="cb2-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   - 2 = Caribbean</span></span>
<span id="cb2-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   - 3 = UK</span></span>
<span id="cb2-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   - 4 = East Africa</span></span>
<span id="cb2-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Residence: Dummy</span></span>
<span id="cb2-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Arrests: Number of arrests</span></span>
<span id="cb2-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Conviction: Number of convictions</span></span>
<span id="cb2-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Prison: Dummy Has been to prison?</span></span>
<span id="cb2-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Music: Likes music</span></span>
<span id="cb2-75"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Ranking: Dummy</span></span>
<span id="cb2-76"></span>
<span id="cb2-77"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Notes related to the definition of the edge attributes: ##</span></span>
<span id="cb2-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - 1 (hang out together)</span></span>
<span id="cb2-79"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - 2 (co-offend together)</span></span>
<span id="cb2-80"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - 3 (co-offend together, serious crime)</span></span>
<span id="cb2-81"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - 4 (co-offend together, serious crime, kin)</span></span>
<span id="cb2-82"></span>
<span id="cb2-83"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Convert to other formats for further processing##</span></span>
<span id="cb2-84"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># {tidygraph} for pretty vizualisations</span></span>
<span id="cb2-85">nettidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tidygraph</span>(netigraph)</span>
<span id="cb2-86"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># {network} for running ERGMs</span></span>
<span id="cb2-87">netnetwork <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_network</span>(netigraph)</span>
<span id="cb2-88"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">########################## Initial Visualizations ##############################</span></span>
<span id="cb2-89"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># With networkD3</span></span>
<span id="cb2-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to networkD3 consistent object</span></span>
<span id="cb2-91">netnetd3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> networkD3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">igraph_to_networkD3</span>(netigraph,</span>
<span id="cb2-92">                                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(netigraph)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ethnicity)</span>
<span id="cb2-93"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add node size attribute as the number of arrests</span></span>
<span id="cb2-94">netnetd3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nodes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(netigraph)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>arrests</span>
<span id="cb2-95"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Render</span></span>
<span id="cb2-96">networkD3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">forceNetwork</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Links =</span> netnetd3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>links,</span>
<span id="cb2-97">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Nodes =</span> netnetd3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>nodes,</span>
<span id="cb2-98">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Source =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'source'</span>,</span>
<span id="cb2-99">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Target =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'target'</span>,</span>
<span id="cb2-100">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">NodeID =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'name'</span>,</span>
<span id="cb2-101">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'group'</span>,</span>
<span id="cb2-102">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Nodesize =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'size'</span>,</span>
<span id="cb2-103">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colourScale =</span></span>
<span id="cb2-104">                          networkD3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">JS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"d3.scaleOrdinal(d3.schemeCategory10);"</span>),</span>
<span id="cb2-105">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linkDistance =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>,</span>
<span id="cb2-106">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontSize =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>,</span>
<span id="cb2-107">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-108">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">opacity =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span>
<span id="cb2-109"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># With threejs</span></span>
<span id="cb2-110"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1 Define a palette:</span></span>
<span id="cb2-111">pal <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> iheiddown<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">iheid_palette</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SDGs"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">V</span>(netigraph)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ethnicity)))</span>
<span id="cb2-112">netthreejs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> netigraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-113">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> gangs_attr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Arrests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-114">  igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.vertex.attribute</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>,</span>
<span id="cb2-115">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> pal[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vertex_attr</span>(netigraph, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"birthplace"</span>)])</span>
<span id="cb2-116"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Render</span></span>
<span id="cb2-117">threejs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graphjs</span>(netthreejs,</span>
<span id="cb2-118">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layout_with_fr</span>(netthreejs,</span>
<span id="cb2-119">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-120">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DAD7CD"</span>)</span>
<span id="cb2-121"></span>
<span id="cb2-122"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Graph Density:</span></span>
<span id="cb2-123">density <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_density</span>(netigraph)</span>
<span id="cb2-124"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.2201258</span></span>
<span id="cb2-125"></span>
<span id="cb2-126"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute the Blau Index</span></span>
<span id="cb2-127"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Definition:</span></span>
<span id="cb2-128"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Probability that two entities taken at random from the dataset of interest</span></span>
<span id="cb2-129"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (with replacement) represent the same type</span></span>
<span id="cb2-130"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  This index reaches its minimum value (0) when there is no variety,</span></span>
<span id="cb2-131"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  that is to say, when all individuals are classified in the same category.</span></span>
<span id="cb2-132"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  Since the index is normalized to the 0 to 1 range, when equal to 1 it means</span></span>
<span id="cb2-133"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  that there is certainty of observing the same attribute value when taking</span></span>
<span id="cb2-134"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  two nodes at random</span></span>
<span id="cb2-135">blauEthnicity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_blau_index</span>(netigraph, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.691358</span></span>
<span id="cb2-136"></span>
<span id="cb2-137"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Are certain groups more diverse than others?</span></span>
<span id="cb2-138">nettidy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nettidy <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-139">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wt_group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">membership</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cluster_walktrap</span>(nettidy)))</span>
<span id="cb2-140">lbb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> graphlayouts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layout_as_backbone</span>(nettidy)</span>
<span id="cb2-141">pal1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> iheiddown<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">iheid_palette</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SDGs"</span>,</span>
<span id="cb2-142">                                 <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">membership</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cluster_walktrap</span>(nettidy)))))</span>
<span id="cb2-143"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Graphically</span></span>
<span id="cb2-144">ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>(nettidy,</span>
<span id="cb2-145">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"manual"</span>,</span>
<span id="cb2-146">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lbb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xy[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb2-147">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lbb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xy[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-148">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_link0</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-149">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> ethnicity,</span>
<span id="cb2-150">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">membership</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cluster_walktrap</span>(nettidy)))),</span>
<span id="cb2-151">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-152">  ggforce<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_mark_hull</span>(</span>
<span id="cb2-153">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x,</span>
<span id="cb2-154">        y,</span>
<span id="cb2-155">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">membership</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cluster_walktrap</span>(nettidy)),</span>
<span id="cb2-156">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> pal1[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">membership</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cluster_walktrap</span>(nettidy))]),</span>
<span id="cb2-157">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">concavity =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-158">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mm"</span>),</span>
<span id="cb2-159">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>,</span>
<span id="cb2-160">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.legend =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-161">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ethnicity"</span>,</span>
<span id="cb2-162">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cluster"</span>,</span>
<span id="cb2-163">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Walktrap clusters and ethnicity"</span>,</span>
<span id="cb2-164">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Individuals with the same ethnic background seem to co-offend together."</span>,</span>
<span id="cb2-165">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: Grund and Densley (2015)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-166">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_graph</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-167">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb2-168">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>),</span>
<span id="cb2-169">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb2-170">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb2-171">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>,</span>
<span id="cb2-172">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb2-173">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DAD7CD"</span>))</span>
<span id="cb2-174"></span>
<span id="cb2-175"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quantitatively: &lt;- Blau Index within a cluster</span></span>
<span id="cb2-176">blaucluster <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_blau_index</span>(nettidy,</span>
<span id="cb2-177">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>,</span>
<span id="cb2-178">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wt_group"</span>)</span>
<span id="cb2-179"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cluster 1 Cluster 2 Cluster 3 Cluster 4 Cluster 5 Cluster 6 </span></span>
<span id="cb2-180"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.4687500 0.6122449 0.6938776 0.3200000 0.0000000 0.4444444</span></span>
<span id="cb2-181"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                                       Small cluster</span></span>
<span id="cb2-182"></span>
<span id="cb2-183"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">####### Model 1: Standard ERGM with only an edgecount -&gt; our baseline ##########</span></span>
<span id="cb2-184">ergm1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ergm</span>(netnetwork <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> edges)</span>
<span id="cb2-185"></span>
<span id="cb2-186"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log-likelihood</span></span>
<span id="cb2-187">ergm1mle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mle.lik</span>
<span id="cb2-188"></span>
<span id="cb2-189"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coefficients</span></span>
<span id="cb2-190">coefs1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(ergm1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients,</span>
<span id="cb2-191">                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(ergm1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(ergm1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients)),</span>
<span id="cb2-192">                     migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">graph_density</span>(netigraph))</span>
<span id="cb2-193"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(coefs1) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Untransformed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Transformed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span>
<span id="cb2-194"></span>
<span id="cb2-195">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(coefs1)</span>
<span id="cb2-196"></span>
<span id="cb2-197"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coefficients have to be reexpressed as the conditional log-odd of a tie</span></span>
<span id="cb2-198"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># occurring. In this case it is equivalent to the density by construction.</span></span>
<span id="cb2-199"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Goodness of fit of the model -&gt; is it a good model?</span></span>
<span id="cb2-200"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's look at it visually:</span></span>
<span id="cb2-201">ergm1.sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate</span>(ergm1)</span>
<span id="cb2-202"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This command enables side-by-side comparison.</span></span>
<span id="cb2-203"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(netnetwork, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observed"</span>)</span>
<span id="cb2-204"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ergm1.sim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated"</span>)</span>
<span id="cb2-205"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-206"></span>
<span id="cb2-207"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mhh one can spot differences between the two graphs, the simulated one being</span></span>
<span id="cb2-208"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># more dense than the observed network. Let's use the `statnet::gof()` function</span></span>
<span id="cb2-209"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to get more advanced diagnostics.</span></span>
<span id="cb2-210">ergm1.gof <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gof</span>(ergm1,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GOF =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> degree <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> triadcensus <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> espartners</span>
<span id="cb2-211">                 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> model)</span>
<span id="cb2-212"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb2-213"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ergm1.gof, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plotlogodds =</span> T)</span>
<span id="cb2-214"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-215"></span>
<span id="cb2-216"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Goodness of fit measures of *non-included* network level, node level or dyad</span></span>
<span id="cb2-217"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># level measures.</span></span>
<span id="cb2-218"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We look at this after running our model and checking whether the MCMC generated</span></span>
<span id="cb2-219"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># networks have indeed the same characteristics than the one we observe.</span></span>
<span id="cb2-220"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#</span></span>
<span id="cb2-221"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One test of whether a local model “fits the data” is therefore how well it</span></span>
<span id="cb2-222"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reproduces the observed global</span></span>
<span id="cb2-223"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># network properties that are not in the model. We do this by choosing a</span></span>
<span id="cb2-224"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># network statistic that is not in the</span></span>
<span id="cb2-225"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># model, and comparing the value of this statistic observed in the original </span></span>
<span id="cb2-226"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># network to the distribution of values</span></span>
<span id="cb2-227"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># we get in simulated networks from our model, using the gof function.</span></span>
<span id="cb2-228"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The gof function is a bit different than the summary, ergm, and simulate</span></span>
<span id="cb2-229"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># functions, in that it currently</span></span>
<span id="cb2-230"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># only takes 3 ergm-terms as arguments: degree, esp (edgewise share partners),</span></span>
<span id="cb2-231"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and distance (geodesic distances).</span></span>
<span id="cb2-232"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each of these terms captures an aggregate network distribution, at either</span></span>
<span id="cb2-233"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the node level (degree), the edge</span></span>
<span id="cb2-234"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># level (esp), or the dyad level (distance).</span></span>
<span id="cb2-235"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span></span>
<span id="cb2-236"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># From page 32 of the EGRM package manual.</span></span>
<span id="cb2-237"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span></span>
<span id="cb2-238"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Conclusion, our simple model is really not a good fit! We are underestimating</span></span>
<span id="cb2-239"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># low and high degrees and over estimating medium ones. Furthermore, low</span></span>
<span id="cb2-240"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># edge-wise shared partners seem to be overestimated while high ones are</span></span>
<span id="cb2-241"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># underestimated. The triad census measure seems to be underestimated for 0</span></span>
<span id="cb2-242"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># closure and 2 and overestimated for 1 and 3. All in all, we see that our</span></span>
<span id="cb2-243"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># baseline model is not a good fit! Let's try to follow the Grund and Dempsey</span></span>
<span id="cb2-244"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to see the result of their model specification.</span></span>
<span id="cb2-245"></span>
<span id="cb2-246"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Models p.362 paper</span></span>
<span id="cb2-247"></span>
<span id="cb2-248"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># IMPORTANT: the network they consider in their 2015 paper is missing 6</span></span>
<span id="cb2-249"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># individuals (no reason was mentioned in the paper). Since we can't</span></span>
<span id="cb2-250"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># infer which individuals were removed from the network, we will not get the</span></span>
<span id="cb2-251"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same results as the authors. Hopefully though, this will not impact</span></span>
<span id="cb2-252"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the interpretation too much. Let's estimate away!</span></span>
<span id="cb2-253"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 1</span></span>
<span id="cb2-254">ergm2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ergm</span>(netnetwork <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> edges <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nodematch</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>))</span>
<span id="cb2-255">coefs2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients</span>
<span id="cb2-256"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 2</span></span>
<span id="cb2-257">ergm3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ergm</span>(netnetwork <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> edges <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nodematch</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-258">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gwesp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb2-259">coefs3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients</span>
<span id="cb2-260"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 3</span></span>
<span id="cb2-261">ergm4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ergm</span>(netnetwork <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> edges <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nodematch</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ethnicity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-262">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gwesp</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-263">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nodefactor</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"birthplace"</span>))</span>
<span id="cb2-264">coefs4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coefficients</span>
<span id="cb2-265"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Getting the results from model 3 into a table:</span></span>
<span id="cb2-266">texreg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">knitreg</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(ergm1, ergm2, ergm3, ergm4),</span>
<span id="cb2-267">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">center =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb2-268">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ERGM results table"</span>,</span>
<span id="cb2-269">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">custom.coef.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Edges"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nodematch(Ethnicity)"</span>,</span>
<span id="cb2-270">                                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GWESP(decay = 0)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carribean"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"U.K."</span>,</span>
<span id="cb2-271">                                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"East Africa"</span>),</span>
<span id="cb2-272">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">custom.model.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Naive Model"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model 1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model 2"</span>,</span>
<span id="cb2-273">                                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Model 3"</span>),</span>
<span id="cb2-274">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stars =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>))</span>
<span id="cb2-275"></span>
<span id="cb2-276"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Goodness of fit: -----</span></span>
<span id="cb2-277"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The authors only provide information of the goodness of fit check for the</span></span>
<span id="cb2-278"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fourth model. Since we don't compute it here, we'll simply do the GOF on the</span></span>
<span id="cb2-279"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># third model.</span></span>
<span id="cb2-280"></span>
<span id="cb2-281"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Model 3: GOF</span></span>
<span id="cb2-282">ergm4.gof <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ergm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gof</span>(ergm4,</span>
<span id="cb2-283">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GOF =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> degree <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> triadcensus <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> espartners <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-284">                         distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> model)</span>
<span id="cb2-285"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb2-286"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(ergm4.gof, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plotlogodds =</span> T) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the argument "plotlogodds=T" here to rescale</span></span>
<span id="cb2-287"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">par</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mfrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb2-288"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################</span></span>
<span id="cb2-289"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Citing all loaded packages ##</span></span>
<span id="cb2-290"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">################################</span></span>
<span id="cb2-291"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># knitr::write_bib(c(.packages(), "blogdown"), "packages.bib")</span></span></code></pre></div>
</div>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<p>Cover photo by <a href="https://unsplash.com/@dulhiier?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Nastya Dulhiier</a> on <a href="https://unsplash.com/s/photos/network?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>



</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-grund2012ethnic" class="csl-entry">
Grund, Thomas U, and James A Densley. 2012. <span>“Ethnic Heterogeneity in the Activity and Structure of a Black Street Gang.”</span> <em>European Journal of Criminology</em> 9 (4): 388–406.
</div>
<div id="ref-grund2015ethnic" class="csl-entry">
———. 2015. <span>“Ethnic Homophily and Triad Closure: Mapping Internal Gang Structure Using Exponential Random Graph Models.”</span> <em>Journal of Contemporary Criminal Justice</em> 31 (3): 354–70.
</div>
<div id="ref-papachristos2013corner" class="csl-entry">
Papachristos, Andrew V, David M Hureau, and Anthony A Braga. 2013. <span>“The Corner and the Crew: The Influence of Geography and Social Networks on Gang Violence.”</span> <em>American Sociological Review</em> 78 (3): 417–47.
</div>
<div id="ref-reiss1988co" class="csl-entry">
Reiss Jr, Albert J. 1988. <span>“Co-Offending and Criminal Careers.”</span> <em>Crime and Justice</em> 10: 117–70.
</div>
<div id="ref-wasserman1994social" class="csl-entry">
Wasserman, Stanley, Katherine Faust, et al. 1994. <span>“Social Network Analysis: Methods and Applications.”</span>
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2021" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2021. <span>“ERGMs and Co-Offending: An Applied Social
Networks Project.”</span> December 15, 2021. <a href="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/">https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/</a>.
</div></div></section></div> ]]></description>
  <category>Networks</category>
  <category>ERGM</category>
  <category>Criminality</category>
  <guid>https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/</guid>
  <pubDate>Wed, 15 Dec 2021 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2021-12-09-ergms-and-co-offending-an-applied-social-networks-project/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Marvellous Social Networks: an applied project</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<p>This blog post was originally an assignment I completed for a Social Networks class during my Master’s in International Economics at the Graduate Institute of Geneva (IHEID). This first post mostly illustrates the descriptive part of the social networks analysis process describing properties of a network of Marvel character friendships. Happy reading!</p>
<blockquote class="blockquote">
<p>If you’d like to follow along, the dataset is available in the excellent <a href="https://snlab-ch.github.io/migraph/index.html"><code>{migraph}</code></a> package.</p>
</blockquote>
<section id="question-1-centrality" class="level2">
<h2 class="anchored" data-anchor-id="question-1-centrality">Question 1: Centrality</h2>
<p>This part will focus on answering the following centrality related question: <em>What characters are the ideal candidates for crossover comics in the Marvel universe, in the global sense?</em></p>
<p>The Marvel universe is comprised of multiple franchises that include but are not limited to <em>the Avengers</em>, <em>the Fantastic Four</em> and <em>the X-Men</em>. The question thus naturally arises which characters are most likely to appear in comics, films or books that span across these franchises. Part of the answer to this question will be revealed in this section by looking at the characters with the highest level of betweenness centrality as these high-betweenness characters (ego) tend to be on the shortest path between two other characters (alters). It is worth mentionning two caveats of this analysis before proceeding further with our analysis. First, when looking at the measure of betweenness centrality, we are considering the global rather than the local structure of the network. Second, the initial <code>ison_marvel_relationships</code> is a signed but unweighted network where the ties either represent a friendly relationship or a rival relationship between characters. On the other hand, the absence of a tie indicates the absence of a relationship between the characters. For the purpose of this first question, we will examine the underlying unsigned network as we are only interested in the <em>existence</em> of a relationship since it indicates some kind of previous interaction in a comic book.</p>
<p>The results are unsurprising, since the top 5 characters in terms of betweenness centrality measure are (in order): Wolverine, Spider-Man, Invisible Woman, Hulk, and Black Panther. These are some of the older Marvel characters and are also some of the characters with the most number of appearances. Out of these top 5 characters, none is a “mainstream” villain<sup>1</sup> and while three are traditional mainstream characters (Black Panther, Spider-Man, and Hulk) the other two come from either the <em>X-Men</em> franchise or the <em>Fantastic Four</em> franchise. This last point leads us to the answer to the question we’re asking in this first part as both Wolverine and the Invisible Woman seem to have enough preexisting back-story in the form of past relationships with other characters to be able to bridge the gap between the different franchises with the most ease.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output cell-output-stderr">
<pre><code>Warning: There was 1 warning in `dplyr::mutate()`.
ℹ In argument: `across(where(is.numeric), round, 3)`.
Caused by warning:
! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
Supply arguments directly to `.fns` through an anonymous function instead.

  # Previously
  across(a:b, mean, na.rm = TRUE)

  # Now
  across(a:b, \(x) mean(x, na.rm = TRUE))</code></pre>
</div>
<div class="cell-output-display">
<table class="table lightable-material caption-top table-sm table-striped small" data-quarto-postprocess="true">
<caption>Top 5 Nodes: Betweenness</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Name</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Gender</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Appearances</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Degree</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Betweenness</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Closeness</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Eigenvector</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Wolverine</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">12751</td>
<td style="text-align: right;">0.878</td>
<td style="text-align: right;">0.067</td>
<td style="text-align: right;">0.817</td>
<td style="text-align: right;">0.328</td>
</tr>
<tr class="even">
<td style="text-align: left;">Spider-Man</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">11963</td>
<td style="text-align: right;">0.837</td>
<td style="text-align: right;">0.057</td>
<td style="text-align: right;">0.803</td>
<td style="text-align: right;">0.308</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Invisible Woman</td>
<td style="text-align: left;">Female</td>
<td style="text-align: right;">5082</td>
<td style="text-align: right;">0.837</td>
<td style="text-align: right;">0.036</td>
<td style="text-align: right;">0.731</td>
<td style="text-align: right;">0.342</td>
</tr>
<tr class="even">
<td style="text-align: left;">Hulk</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">6062</td>
<td style="text-align: right;">0.878</td>
<td style="text-align: right;">0.035</td>
<td style="text-align: right;">0.742</td>
<td style="text-align: right;">0.353</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Black Panther</td>
<td style="text-align: left;">Male</td>
<td style="text-align: right;">2189</td>
<td style="text-align: right;">0.653</td>
<td style="text-align: right;">0.034</td>
<td style="text-align: right;">0.721</td>
<td style="text-align: right;">0.255</td>
</tr>
</tbody>
</table>


</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/index_files/figure-html/graphq1-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" data-fig-pos="!H"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="question-2-community" class="level2">
<h2 class="anchored" data-anchor-id="question-2-community">Question 2: Community</h2>
<p>In this subsection, we will look at the question of structural balance of the network and leverage the <code>{signet}</code> package to look at this issue further. More specifically, we’ll try to answer the following question: <em>How balanced/cognitively consistent is the Marvel Relationship network?</em> This will be interesting in our case since the original dataset is signed, meaning that there is additional binary information on the nature of the tie linking two characters (e.g.&nbsp;whether they are allies or foes). Theorized by the Austrian psychologist Fritz Heider in the mid 20th century <span class="citation" data-cites="heider1958psychology">Heider (1958)</span>, balancedness in a social network is driven by cognitive consistency among an ego and two alters. Cognitive consistency being defined as the absence of contradiction in the nature of the ties that the ego has with its two alters. In our case, Hulk, Iron Man and Thanos are an example of a cognitively consistent and therefore balanced triad. This is due to the fact that Hulk is friends with Iron Man and enemies with Thanos, implying/satisfying (by cognitive consistency) that Iron Man is also an enemy of Thanos. Furthermore, it is interesting to examine this concept of balance in a dynamic setting as attitudes between characters may change over time. For example, Hulk becomes an evil character in one of the universes (we’ll assume that he becomes friends with Thanos for this example). To maintain cognitive consistency in the considered triad, one of two things has to happen. Either Iron Man decides that Hulk is no longer his friend since he is a bad guy who became friends with Thanos (i.e.&nbsp;the friend of my enemy is my enemy) or Thanos and Iron Man become friends and every character becomes a bad guy (i.e.&nbsp;the friend of my friend is my friend from Thanos’ perspective).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="lightable-minimal caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: center;" data-quarto-table-cell-role="th">Type</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Count</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Balanced?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">+++</td>
<td style="text-align: center;">436</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td style="text-align: center;">++-</td>
<td style="text-align: center;">287</td>
<td style="text-align: center;">No</td>
</tr>
<tr class="odd">
<td style="text-align: center;">+--</td>
<td style="text-align: center;">944</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td style="text-align: center;">---</td>
<td style="text-align: center;">398</td>
<td style="text-align: center;">No</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Total</td>
<td style="text-align: center;">2065</td>
<td style="text-align: center;"></td>
</tr>
</tbody>
</table>


</div>
</div>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="lightable-minimal caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: center;" data-quarto-table-cell-role="th">Method</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Results</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">Triangles</td>
<td style="text-align: center;">0.668</td>
</tr>
<tr class="even">
<td style="text-align: center;">Walk</td>
<td style="text-align: center;">0.071</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Frustration</td>
<td style="text-align: center;">0.858</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>The <code>{signnet}</code> allowed us to both count the number of balanced/unbalanced triads and to compute different measures of balancedness. At the first glance, when considering the simple counting table, we seem to witness more balanced triads than unbalanced ones. This seems to be consistent with Heiders’ theory as the engine of cognitive consistency would have shaped the relations between the characters dynamically and may still be in the process of shaping. Let’s push our analysis a little further and look at the three metrics <code>{signnet}</code> computes: Triangles, Walk and Frustration. By looking at the results, we notice that the Frustration algorithm and the Triangles one yield consistent results while the Walk algorithm seems to contradict the former two. While this seems strange at first glance, this leads back to the different ways the metrics are computed and the way they frame the <em>degree of balancedness</em> of a network. Without going into too much detail, Estrada’s <em>walk</em> measure takes into account the proximity of imbalanced clusters while the other two are agnostic of this effect <span class="citation" data-cites="estrada2019rethinking">Estrada (2019)</span>. Conceptually, it’s straightforward to see that close imbalanced clusters are generating more tension than ones that are further away. Thus, the answer to our question is depending on the measure we consider as well as on the underlying theory we use to conceptualize the network with.</p>
<p>In conclusion, while the cognitive consistency in a social network seems like a natural law, it should be interpreted carefully, especially in our case where <em>cognitive inconsistency</em> may also be desirable since it ” [sometimes] evoke[s], like other patterns with unsolved ambiguities, powerful aesthetic forces of a tragic or comic nature” <span class="citation" data-cites="heider1958psychology">Heider (1958)</span>. Having panel measures of these indexes would certainly cast a light onto the dynamics of these social mechanisms and allow to answer the question at hand in more detail.</p>
</section>
<section id="question-3-position" class="level2">
<h2 class="anchored" data-anchor-id="question-3-position">Question 3: Position</h2>
<p>We have hinted at the difference between the global nature of the concept of betweenness centrality and the local nature of the concept of structural holes in the first part of this paper. This last part will therefore try to answer a slightly different question than in the first part of the paper to illustrate this difference. <em>What characters are the ideal candidates for crossover comics in the Marvel universe, in the local sense?</em></p>
<p>This slight difference is consequential though as the definition of a cross-over comic is no longer the same as in part 1. Since we are now focusing on the local nature of patterns of ties, we’ll think about cross-over comics as comics where multiple characters “gang up” to fight each other. Note that we’ll consider both heroes and villains in this case as we can’t disaggregate the data accordingly. Thus, a character in a structural hole marked by a low level of constraint, will be the ideal candidate for a movie/comic bridging the universe of multiple other characters <em>within a franchise</em> (e.g.&nbsp;the Avengers). This indicates that Spider-Man, Worlverine, Black Panther, Hulk and Iron Man seeom to be the ideal candidates for such a role. Importantly, note that this is different from the conclusion that we drew in the discussion on centrality in the first part as betweenness highlights characters that were ideally placed to span across multiple franchises of the entire Marvel Network.</p>
<p>Finally, an interesting albeit tangential clue that we get from examining the node constraint measure is that many villains (4 out of 5 in the top 5) seem to find themselves in a part of the network with strong ties (e.g.&nbsp;a high level of constraint). This inverse correlation reflects the fact that villains are more likely to gang up against a hero than a hero is against a single enemy.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<table class="lightable-material caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Head and Tail Nodes: Constraint</caption>
<thead>
<tr class="header">
<th style="text-align: center;" data-quarto-table-cell-role="th">Name</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Gender</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Appearances</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Constraint</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Betweenness</th>
<th style="text-align: center;" data-quarto-table-cell-role="th">Hero?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">Ant-Man</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">589</td>
<td style="text-align: center;">0.262</td>
<td style="text-align: center;">0.001</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td style="text-align: center;">Emma Frost</td>
<td style="text-align: center;">Female</td>
<td style="text-align: center;">4777</td>
<td style="text-align: center;">0.25</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">Both</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Blade</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">438</td>
<td style="text-align: center;">0.223</td>
<td style="text-align: center;">0</td>
<td style="text-align: center;">Both</td>
</tr>
<tr class="even">
<td style="text-align: center;">Kingpin</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">1323</td>
<td style="text-align: center;">0.211</td>
<td style="text-align: center;">0.002</td>
<td style="text-align: center;">No</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Bullseye</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">898</td>
<td style="text-align: center;">0.196</td>
<td style="text-align: center;">0.001</td>
<td style="text-align: center;">No</td>
</tr>
<tr class="even">
<td style="text-align: center;">...</td>
<td style="text-align: center;">...</td>
<td style="text-align: center;">...</td>
<td style="text-align: center;">...</td>
<td style="text-align: center;">...</td>
<td style="text-align: center;">...</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Iron Man</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">8579</td>
<td style="text-align: center;">0.102</td>
<td style="text-align: center;">0.027</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td style="text-align: center;">Hulk</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">6062</td>
<td style="text-align: center;">0.1</td>
<td style="text-align: center;">0.035</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Black Panther</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">2189</td>
<td style="text-align: center;">0.097</td>
<td style="text-align: center;">0.034</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="even">
<td style="text-align: center;">Wolverine</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">12751</td>
<td style="text-align: center;">0.094</td>
<td style="text-align: center;">0.067</td>
<td style="text-align: center;">Yes</td>
</tr>
<tr class="odd">
<td style="text-align: center;">Spider-Man</td>
<td style="text-align: center;">Male</td>
<td style="text-align: center;">11963</td>
<td style="text-align: center;">0.093</td>
<td style="text-align: center;">0.057</td>
<td style="text-align: center;">Yes</td>
</tr>
</tbody>
</table>


</div>
</div>
</section>
<section id="image-credits" class="level2">
<h2 class="anchored" data-anchor-id="image-credits">Image credits:</h2>
<p>Photo by <a href="https://unsplash.com/@introspectivedsgn?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Erik Mclean</a> on <a href="https://unsplash.com/s/photos/marvel?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
</section>
<section id="appendix" class="level1">
<h1>Appendix:</h1>
<p>This appendix does not contain any additional content. It’s only function is to print the echoes of the code used to handle, measure and visualize the networks in the previous three sections in an effort to ensure transparency and replicability of our code.</p>
<p>Okay I lied, it actually does one more thing, it tries to remotivate the reader who made it that far with a little randomly generated quote (courtesy of the <code>{dang}</code> package).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output cell-output-stdout">
<pre><code>'Success is not final, failure is not fatal. It is the courage to 
continue that counts.'  Winston Churchill </code></pre>
</div>
</div>
<p>And an additional graph to visualize the signed network for question 2:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/index_files/figure-html/appendixgraphQ2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" data-fig-pos="!H"></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set initial knitr options</span></span>
<span id="cb3-2">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>opts_chunk<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eval =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">echo =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb3-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fig.align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb3-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fig.asp =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>,</span>
<span id="cb3-5">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>,</span>
<span id="cb3-6">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.width =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80%"</span>,</span>
<span id="cb3-7">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fig.pos =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"!H"</span>,</span>
<span id="cb3-8">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">out.extra =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)</span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean Environment</span></span>
<span id="cb3-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#********************* Package Management *************************************#</span></span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(migraph)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Development version 29.10.2021</span></span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(igraph)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Latest version</span></span>
<span id="cb3-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggraph)</span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(signnet)</span>
<span id="cb3-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidygraph)</span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All things data science</span></span>
<span id="cb3-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dang)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For fun</span></span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#***************************** Load data  *************************************#</span></span>
<span id="cb3-20">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>ison_marvel_relationships</span>
<span id="cb3-21">teams         <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>ison_marvel_teams</span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set both objects to the same class to be more consistent</span></span>
<span id="cb3-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(relationships) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "tbl_graph" "igraph"</span></span>
<span id="cb3-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(teams)         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "igraph"</span></span>
<span id="cb3-25">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_igraph</span>(relationships)</span>
<span id="cb3-26">teams         <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_igraph</span>(teams)</span>
<span id="cb3-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># relationships # Relationship network, Unimodal</span></span>
<span id="cb3-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># teams         # Teams network, Bipartite: teams and individuals</span></span>
<span id="cb3-29"></span>
<span id="cb3-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial visualisations</span></span>
<span id="cb3-31">manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autographr</span>(relationships)</span>
<span id="cb3-32">manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autographr</span>(teams)</span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial visualisation for relationships dataset shows three (unrealistic)</span></span>
<span id="cb3-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># isolates. Let's drop them.</span></span>
<span id="cb3-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PS: Luke Cage and Iron Fist are friends in the MU</span></span>
<span id="cb3-37">isolates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degree</span>(relationships) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-38">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">delete.vertices</span>(relationships, isolates)</span>
<span id="cb3-39"></span>
<span id="cb3-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that the relationships network is signed. We can separate the network</span></span>
<span id="cb3-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># between friends and enemies.</span></span>
<span id="cb3-42"></span>
<span id="cb3-43">friends <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_unsigned</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"positive"</span>)</span>
<span id="cb3-44">ennemies <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_unsigned</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative"</span>)</span>
<span id="cb3-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#***************************** Question 1  ************************************#</span></span>
<span id="cb3-46"></span>
<span id="cb3-47"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We'll use the relationships network for this one</span></span>
<span id="cb3-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that this is a signed network! +1 indicates a friendship tie and -1</span></span>
<span id="cb3-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># an enemy tie.</span></span>
<span id="cb3-50"></span>
<span id="cb3-51">relDFnode <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_data_frame</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertices"</span>)</span>
<span id="cb3-52">relDFedge <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_data_frame</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"edges"</span>)</span>
<span id="cb3-53"></span>
<span id="cb3-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a dataframe with the different centrality measures of every node</span></span>
<span id="cb3-55">centralityFullDF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-56">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name         =</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get.vertex.attribute</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name,</span>
<span id="cb3-57">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">degree       =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_degree</span>(relationships),</span>
<span id="cb3-58">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">betweenness  =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_betweenness</span>(relationships),</span>
<span id="cb3-59">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">closeness    =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_closeness</span>(relationships),</span>
<span id="cb3-60">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eigenvector  =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_eigenvector</span>(relationships)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-61">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(</span>
<span id="cb3-62">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(betweenness)</span>
<span id="cb3-63">  )</span>
<span id="cb3-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># centralityFullDF</span></span>
<span id="cb3-65"></span>
<span id="cb3-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's focus on relations rather than friends to explore which characters</span></span>
<span id="cb3-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># bridge universes</span></span>
<span id="cb3-68">relUnsigned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">remove.edge.attribute</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sign"</span>)</span>
<span id="cb3-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For the graph let's revert to a tidygraph object and add node level centrality</span></span>
<span id="cb3-70">relUnsigned <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_tidygraph</span>(relUnsigned) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">degree       =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_degree</span>(relUnsigned),</span>
<span id="cb3-72">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">betweenness  =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_betweenness</span>(relUnsigned),</span>
<span id="cb3-73">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">closeness    =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_closeness</span>(relUnsigned),</span>
<span id="cb3-74">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eigenvector  =</span> migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_eigenvector</span>(relUnsigned))</span>
<span id="cb3-75"></span>
<span id="cb3-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DF for top 5 table output</span></span>
<span id="cb3-77">centralityRelationDF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-78">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name         =</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get.vertex.attribute</span>(relUnsigned)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name,</span>
<span id="cb3-79">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">degree       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_degree</span>(relUnsigned)),</span>
<span id="cb3-80">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">betweenness  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_betweenness</span>(relUnsigned)),</span>
<span id="cb3-81">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">closeness    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_closeness</span>(relUnsigned)),</span>
<span id="cb3-82">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eigenvector  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_eigenvector</span>(relUnsigned)))</span>
<span id="cb3-83"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Perform inner join on node characteristics and  centrality measures</span></span>
<span id="cb3-84">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_data_frame</span>(relUnsigned, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertices"</span>),</span>
<span id="cb3-85">                  centralityRelationDF, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>)</span>
<span id="cb3-86"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get top 5</span></span>
<span id="cb3-87">top5betweenness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-88">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(z, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(betweenness.y)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)]</span>
<span id="cb3-89">top5betweenness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(top5betweenness,</span>
<span id="cb3-90">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Name =</span> name,</span>
<span id="cb3-91">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Degree =</span> degree.x,</span>
<span id="cb3-92">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Betweenness =</span> betweenness.x,</span>
<span id="cb3-93">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Closeness =</span> closeness.x,</span>
<span id="cb3-94">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Eigenvector =</span> eigenvector.x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-95">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span>(is.numeric), round, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb3-96"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make outtable</span></span>
<span id="cb3-97">kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(top5betweenness, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top 5 Nodes: Betweenness"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-98">  kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-99">  kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_material</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> T)</span>
<span id="cb3-100"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set layout</span></span>
<span id="cb3-101">l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_layout</span>(relUnsigned, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fr"</span>)</span>
<span id="cb3-102"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot graph</span></span>
<span id="cb3-103">ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>(relUnsigned, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout  =</span> l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-104">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> betweenness,</span>
<span id="cb3-105">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> betweenness,</span>
<span id="cb3-106">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Appearances)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-107">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> betweenness <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">39</span>,</span>
<span id="cb3-108">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label  =</span> name),</span>
<span id="cb3-109">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>,</span>
<span id="cb3-110">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.size    =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-111">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel         =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-112">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_link</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-113">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-114">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Appearences"</span>,</span>
<span id="cb3-115">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size     =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Node Betweenness"</span>,</span>
<span id="cb3-116">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Highlighting Betweenness and Appearence"</span>,</span>
<span id="cb3-117">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"What characters are the ideal candidates for crossover comics in the Marvel universe in the global sense?"</span>,</span>
<span id="cb3-118">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: {migraph} (2021)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-119">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-120">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-121">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-122">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-123">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-124">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-125">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-126">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-127">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-128">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-129">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-130">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-131">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>),</span>
<span id="cb3-132">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb3-133">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-134">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>,</span>
<span id="cb3-135">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>))</span>
<span id="cb3-136"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#************************* Question 2 *****************************************#</span></span>
<span id="cb3-137"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb3-138"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Back again with the relationships data</span></span>
<span id="cb3-139">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>ison_marvel_relationships</span>
<span id="cb3-140"></span>
<span id="cb3-141"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial visualisation for relationships dataset shows three (unrealistic)</span></span>
<span id="cb3-142"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># isolates. Let's drop them.</span></span>
<span id="cb3-143"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PS: Luke Cage and Iron Fist are friends in the MU</span></span>
<span id="cb3-144">isolates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degree</span>(relationships) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-145">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">delete.vertices</span>(relationships, isolates)</span>
<span id="cb3-146"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set linetypes</span></span>
<span id="cb3-147">edge_linetype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sign <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-148">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb3-149">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>)</span>
<span id="cb3-150">edge_colour <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sign <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-151">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>,</span>
<span id="cb3-152">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E20020"</span>)</span>
<span id="cb3-153"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set layout</span></span>
<span id="cb3-154">l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_layout</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fr"</span>)</span>
<span id="cb3-155"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot graph</span></span>
<span id="cb3-156">relgraph <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout  =</span> l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-157">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_link0</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,</span>
<span id="cb3-158">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge_linetype =</span> edge_linetype,</span>
<span id="cb3-159">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge_colour   =</span> edge_colour) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-160">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PowerOrigin,</span>
<span id="cb3-161">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-162">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-163">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hulk"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Iron Man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thanos"</span>),</span>
<span id="cb3-164">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label  =</span> name),</span>
<span id="cb3-165">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>,</span>
<span id="cb3-166">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.size    =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-167">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel         =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-168">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-169">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power Origin"</span>,</span>
<span id="cb3-170">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marvel Character Network: Examining the Signs"</span>,</span>
<span id="cb3-171">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Visualizing network balance"</span>,</span>
<span id="cb3-172">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: {migraph} (2021)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-173">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-174">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-175">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-176">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-177">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-178">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-179">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-180">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-181">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-182">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-183">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-184">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-185">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>),</span>
<span id="cb3-186">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb3-187">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-188">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>,</span>
<span id="cb3-189">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>))</span>
<span id="cb3-190"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We have a network were a +1 tie indicates a friendly raltionship</span></span>
<span id="cb3-191"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># and a -1 tie an animosity between two characters</span></span>
<span id="cb3-192"></span>
<span id="cb3-193"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Count the number of triangles</span></span>
<span id="cb3-194">countTriangles <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-195">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Type =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count_signed_triangles</span>(relationships)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total"</span>),</span>
<span id="cb3-196">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count_signed_triangles</span>(relationships)),</span>
<span id="cb3-197">                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count_signed_triangles</span>(relationships)))),</span>
<span id="cb3-198">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Balanced?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))</span>
<span id="cb3-199"></span>
<span id="cb3-200">t1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(countTriangles,</span>
<span id="cb3-201">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>,</span>
<span id="cb3-202">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-203">  kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_minimal</span>()</span>
<span id="cb3-204">t1</span>
<span id="cb3-205"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># +++ ++- +-- --- </span></span>
<span id="cb3-206"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 436 287 944 398 </span></span>
<span id="cb3-207"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The network seems to be more balanced than unbalanced with this quick</span></span>
<span id="cb3-208"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># comparaison</span></span>
<span id="cb3-209"></span>
<span id="cb3-210"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Degree of balancedness:</span></span>
<span id="cb3-211"></span>
<span id="cb3-212">Balancedness <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-213">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Method  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Triangles"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Walk"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frustration"</span>),</span>
<span id="cb3-214">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Results =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">balance_score</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"triangles"</span>),</span>
<span id="cb3-215">              signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">balance_score</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"walk"</span>),</span>
<span id="cb3-216">              signnet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">balance_score</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"frustration"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-217">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(Results, round, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb3-218"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make outtable</span></span>
<span id="cb3-219">t2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(Balancedness,</span>
<span id="cb3-220">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>,</span>
<span id="cb3-221">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-222">  kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_minimal</span>()</span>
<span id="cb3-223">t2</span>
<span id="cb3-224"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Okay, we get quite a different result with the Walk method based on</span></span>
<span id="cb3-225"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ernesto Estrada measure of balancedness. Let's explain why.</span></span>
<span id="cb3-226"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#***************************** Question 3 *************************************#</span></span>
<span id="cb3-227"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb3-228"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Back again with the relationships data</span></span>
<span id="cb3-229">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>ison_marvel_relationships</span>
<span id="cb3-230"></span>
<span id="cb3-231"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial visualisation for relationships dataset shows three (unrealistic)</span></span>
<span id="cb3-232"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># isolates. Let's drop them.</span></span>
<span id="cb3-233"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PS: Luke Cage and Iron Fist are friends in the MU</span></span>
<span id="cb3-234">isolates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degree</span>(relationships) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-235">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">delete.vertices</span>(relationships, isolates)</span>
<span id="cb3-236"></span>
<span id="cb3-237"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># DF for top 5 table output</span></span>
<span id="cb3-238">constraintRelationDF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-239">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name         =</span> igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get.vertex.attribute</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name,</span>
<span id="cb3-240">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constraint   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_constraint</span>(relationships)),</span>
<span id="cb3-241">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">betweenness  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(migraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">node_betweenness</span>(relationships)))</span>
<span id="cb3-242"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Perform inner join on node characteristics and  centrality measures</span></span>
<span id="cb3-243">z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_data_frame</span>(relationships, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vertices"</span>),</span>
<span id="cb3-244">                  constraintRelationDF, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"name"</span>)</span>
<span id="cb3-245"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get top 5 table </span></span>
<span id="cb3-246">bottom5constraint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-247">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tail</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(z, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(constraint)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-248">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span>(is.numeric), round, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb3-249">top5constraint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb3-250">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(z, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(constraint)), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-251">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span>(is.numeric), round, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb3-252">top5constraint <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(top5constraint, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>))</span>
<span id="cb3-253">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(top5constraint, bottom5constraint)</span>
<span id="cb3-254"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(out) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>()</span>
<span id="cb3-255">out<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Hero?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"..."</span>,</span>
<span id="cb3-256">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yes"</span>)</span>
<span id="cb3-257">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(out,</span>
<span id="cb3-258">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Name =</span> name,</span>
<span id="cb3-259">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Betweenness =</span> betweenness,</span>
<span id="cb3-260">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Constraint =</span> constraint) </span>
<span id="cb3-261"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make outtable</span></span>
<span id="cb3-262">kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(out,</span>
<span id="cb3-263">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"html"</span>,</span>
<span id="cb3-264">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Head and Tail Nodes: Constraint"</span>,</span>
<span id="cb3-265">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-266">  kableExtra<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_material</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full_width =</span> F)</span>
<span id="cb3-267"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># *********************** Motivational Easter Egg *****************************#</span></span>
<span id="cb3-268">dang<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">motivate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-269"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">list =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ls</span>())</span>
<span id="cb3-270"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Back again with the relationships data</span></span>
<span id="cb3-271">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> manynet<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>ison_marvel_relationships</span>
<span id="cb3-272"></span>
<span id="cb3-273"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initial visualisation for relationships dataset shows three (unrealistic)</span></span>
<span id="cb3-274"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># isolates. Let's drop them.</span></span>
<span id="cb3-275"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PS: Luke Cage and Iron Fist are friends in the MU</span></span>
<span id="cb3-276">isolates <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degree</span>(relationships) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-277">relationships <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">delete.vertices</span>(relationships, isolates)</span>
<span id="cb3-278"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set linetypes</span></span>
<span id="cb3-279">edge_linetype <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sign <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-280">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb3-281">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>)</span>
<span id="cb3-282">edge_colour <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(igraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">E</span>(relationships)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sign <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-283">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0072B2"</span>,</span>
<span id="cb3-284">                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E20020"</span>)</span>
<span id="cb3-285"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set layout</span></span>
<span id="cb3-286">l <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_layout</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fr"</span>)</span>
<span id="cb3-287"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot graph</span></span>
<span id="cb3-288">ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggraph</span>(relationships, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">layout  =</span> l) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-289">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_edge_link0</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>,</span>
<span id="cb3-290">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge_linetype =</span> edge_linetype,</span>
<span id="cb3-291">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">edge_colour   =</span> edge_colour) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-292">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> PowerOrigin,</span>
<span id="cb3-293">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb3-294">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-295">  ggraph<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_node_label</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filter =</span> name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hulk"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Iron Man"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Thanos"</span>),</span>
<span id="cb3-296">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label  =</span> name),</span>
<span id="cb3-297">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.padding =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>,</span>
<span id="cb3-298">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label.size    =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-299">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel         =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-300">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">guides</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-301">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power Origin"</span>,</span>
<span id="cb3-302">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marvel Character Network: Examining the Signs"</span>,</span>
<span id="cb3-303">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Visualizing network balance"</span>,</span>
<span id="cb3-304">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Source: {migraph} (2021)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-305">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-306">  ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-307">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-308">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-309">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-310">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-311">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-312">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-313">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-314">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-315">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb3-316">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-317">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>),</span>
<span id="cb3-318">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb3-319">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.subtitle    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>,</span>
<span id="cb3-320">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>,</span>
<span id="cb3-321">                                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size  =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>))</span></code></pre></div>
</div>
</section>
<section id="references" class="level1">




</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References:</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-estrada2019rethinking" class="csl-entry">
Estrada, Ernesto. 2019. <span>“Rethinking Structural Balance in Signed Social Networks.”</span> <em>Discrete Applied Mathematics</em> 268: 70–90.
</div>
<div id="ref-heider1958psychology" class="csl-entry">
Heider, Fritz. 1958. <em>The Psychology of Interpersonal Relations</em>. Psychology Press.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>NB: we consider only the universe where Hulk doesn’t become a bad character.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2021" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2021. <span>“Marvellous Social Networks: An Applied
Project.”</span> December 15, 2021. <a href="https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/">https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/</a>.
</div></div></section></div> ]]></description>
  <category>Networks</category>
  <category>Marvel</category>
  <category>R</category>
  <guid>https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/</guid>
  <pubDate>Wed, 15 Dec 2021 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2021-12-15-marvellous-social-networks-an-applied-project/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Quickstart guide to blogdown</title>
  <dc:creator>Bernhard Bieri</dc:creator>
  <link>https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/</link>
  <description><![CDATA[ 

<!-- source: https://github.com/gadenbuie/garrickadenbuie-com/blob/main/_partials/title-block-link-buttons/title-block.html

  Repurposed following reading Silvia Canelón's post on repurposing Garrick Aden-Buie's title-block.html
-->
<!-- 
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title"> 
-->
<!-- <header id="title-block-header" class="quarto-title-block default page-columns"> -->
<!-- <div class="quarto-title page-columns page-full featured-image p-4" style="background-image: url(featured.png), url(featured.jpg), url(../featured.jpg);"> -->
    


<p>Hi and welcome to my first post on my new <code>{blogdown}</code> website! This post will walk you through the setup of your very own personal website with <a href="https://www.wowchemy.com/docs/getting-started/hugo-github-quickstart/">wowchemy’s awesome Academic Theme</a> and allow me to try out the functionalities of this new blogging workflow!</p>
<section id="the-what-the-how-and-the-why-of-blogdown" class="level2">
<h2 class="anchored" data-anchor-id="the-what-the-how-and-the-why-of-blogdown">The what, the how and the why of blogdown?</h2>
<section id="the-what-and-the-why" class="level3">
<h3 class="anchored" data-anchor-id="the-what-and-the-why">The what and the why</h3>
<p><code>{blogdown}</code> is an R package that makes blogging for R users as straightforward as possible. While there is a little bit of an overhead in learning the structure of a site created with this package, it offers in my opinion the easiest way to share code-centric content on the web for non-web developers like me!</p>
</section>
<section id="the-how" class="level3">
<h3 class="anchored" data-anchor-id="the-how">The how</h3>
<p>The workflow of a <code>{blogdown}</code> website depicted below is almost all automatic and designed to run in the background. While you won’t have to worry about it on most days, it’s useful to take a quick look at how everything works to be able do diagnose the point of failure when an issue eventually arises. A blog post workflow can be described as follows. You’ll edit your code in R-Markdown within R-Studio, then knit that file which will execute the code chunks and render the content of your blog post into a Mardown document. <a href="https://gohugo.io/">Hugo</a>, an open source website generator, will then render these elements into an HTML website you can preview locally while editing or push to a GitHub repository to deploy. In the last step of the workflow, Netlify deploys your website from your GitHub repository and publishes it. The awesome thing is that all of this happens automatically, which allows you to focus on the content of your website without having to deal with the hassle of deploying your website!</p>
<div class="cell">
<div class="cell-output-display">
<div class="grViz html-widget html-fill-item" id="htmlwidget-22d823235d32e49a5bad" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-22d823235d32e49a5bad">{"x":{"diagram":"digraph {\n\ngraph [layout = dot, rankdir = LR, label = \"A {blogdown} Workflow\",\nlabelloc = t]\n\n# define the global styles of the nodes. We can override these in box if we wish\nnode [shape = rectangle, style = filled, fillcolor = WhiteSmoke]\n\nRStudio [label =  \"Edit in\nR-Studio\"]\nknitr [label = \"Knit your\nscripts\"]\nHugo [label= \"Observe\nHugo magic\"]\nGithub [label= \"Push to\nGithub\"]\nNetlify [label= \"Deploy on\nNetlify\"]\n\n# edge definitions with the node IDs\n RStudio -> knitr -> Hugo -> Github  -> Netlify\n}","config":{"engine":"dot","options":null}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section>
</section>
<section id="prerequisites" class="level2">
<h2 class="anchored" data-anchor-id="prerequisites">Prerequisites</h2>
<p>Netlify, R-Studio, GitHub… If these names don’t sound familiar to you, this section is made for you! Here is a quick run down of what you need to get started:</p>
<ul>
<li>A <a href="https://education.github.com/pack">GitHub account</a>
<ul>
<li>See <a href="https://docs.github.com/en/get-started/quickstart">this quickstart guide</a></li>
</ul></li>
<li><a href="https://git-scm.com/">Git</a></li>
<li>A <a href="https://git-fork.com/">Git GUI interface (I recommend Fork)</a></li>
<li>A <a href="https://www.netlify.com/">Netlify account</a>
<ul>
<li>Register with and link your GitHub Account</li>
</ul></li>
<li><a href="https://www.r-project.org/">R</a> and <a href="https://www.rstudio.com/">R-Studio</a></li>
<li>The extended version of <a href="https://gohugo.io/">Hugo</a>
<ul>
<li>See <a href="https://gohugo.io/getting-started/installing">this installation guide</a></li>
</ul></li>
<li>The <a href="https://bookdown.org/yihui/blogdown/installation.html">blogdown package</a></li>
</ul>
<p>Once you have created all the required accounts and installed the required software you’re ready to progress to the next section!</p>
<blockquote class="blockquote">
<p>NB: I found that the easiest way of installing Hugo was through a package manager such as <a href="https://chocolatey.org/">Chocolatey</a> for Windows or <a href="https://brew.sh/">Homebrew</a> for macOS or Linux. See the Hugo installation guide for more details.</p>
</blockquote>
</section>
<section id="the-setup-in-30-minutes" class="level2">
<h2 class="anchored" data-anchor-id="the-setup-in-30-minutes">The setup in 30 minutes</h2>
<p>This section will walk you through the creation of your first <code>{blogdown}</code> site in less than 30 minutes!</p>
<section id="step-1-set-up-a-new-github-repository" class="level3">
<h3 class="anchored" data-anchor-id="step-1-set-up-a-new-github-repository">Step 1: Set up a new GitHub repository</h3>
<p>Go to <a href="https://github.com/">GitHub</a> and set up a new repository with a README.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/GithubRepo.png" class="img-fluid figure-img"></p>
<figcaption>Set up a GitHub Repo</figcaption>
</figure>
</div>
<p>After creating your repository online, clone it by clicking on the clone button and copy-pasting the link to your Git GUI client (Fork if you followed the instructions above). This visual git interface will serve as an interface between the local version of your code (the one on your computer) and the remote one (the one on GitHub’s servers) via pull/push commands.</p>
</section>
<section id="create-the-project-in-r-studio" class="level3">
<h3 class="anchored" data-anchor-id="create-the-project-in-r-studio">Create the project in R-Studio</h3>
<p>After successfully cloning your repository to your local machine, open R and create a new project in the folder that you just created by cloning it. This will be the “root” folder of the project from now on.</p>
<p>Creating a new project from an existing direcotry is simply done by navigating to: File &gt; New Project &gt; Existing Directory in R-Studio and pointing to the folder you just created by cloning your GitHub repository.</p>
</section>
<section id="create-your-site-and-serve-it-locally" class="level3">
<h3 class="anchored" data-anchor-id="create-your-site-and-serve-it-locally">Create your site and serve it locally</h3>
<p>Creating your site is a one liner. Yes you read right, a one line command! Simply open up the project you just created and run the following line where you specify the theme you want to use (wowchemy’s Academic Theme in our case):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(blogdown)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">new_site</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theme =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wowchemy/starter-academic"</span>)</span></code></pre></div>
</div>
<p>This line will automatically create the structure of your new website and spit out some initial configuration messages. After you’ve read through them, simply run the following line to serve (read previewing in your browser while editing) your site locally:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">blogdown<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">serve_site</span>()</span></code></pre></div>
</div>
<p>This will leverage Hugo’s live reload functionality and provide your with a live version of your site in RStudio’s “Viewer” pane. You can easily open this in your browser by clicking on the “Open in new window” button in the viewer pane. Try editing a line in one of the posts in the content &gt; post folder and see how live reload behaves when your save your edits in RStudio.</p>
<p>Now that you have created your site, take the time to scroll through it and check out the different widgets that make up your home page as they will be the building blocks of your site! Once this is done, stop the live server with <code>{r} blogdown::stop_server()</code> and run the following command:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.edit</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".gitignore"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Opens up the .gitignore file</span></span></code></pre></div>
</div>
<p>And paste the following elements in this file before saving it:</p>
<pre><code>.Rproj.user
.Rhistory
.RData
.Ruserdata
.DS_Store
Thumbs.db</code></pre>
<p>This will make sure that git does not upload these files when you push to the remote version of your repository. After this is done, let’s check if everything works fine before pushing our changes to GitHub’s servers. <code>{blogdown}</code> has a function for this fortunately! Simply run:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">blogdown<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_gitignore</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Checks gitignore</span></span>
<span id="cb5-2">blogdown<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_content</span>()   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Checks content</span></span></code></pre></div>
</div>
<p>After taking care of any messages these functions may highlight. You can safely commit and push your changes to GitHub.</p>
</section>
<section id="deploy-your-site-to-netlify" class="level3">
<h3 class="anchored" data-anchor-id="deploy-your-site-to-netlify">Deploy your site to Netlify</h3>
<p>The final step to get your site online, after initializing your site in the previous step is to point Netlify to your GitHub repository. Head over to <a href="https://www.netlify.com/">Netlify</a> and log in with your GitHub account. Then, click on <code>New site from Git</code>, select the repository of your site and don’t change any of the site’s settings in the last pane. Finally, click on deploy to release your site.</p>
<p>Netlify will then deploy your site and allocate an automatically generated and often silly URL to it. Don’t worry, you can change it! Simply head over to the <code>Domain settings</code> on your site’s configuration page and buy or import a domain.</p>
<p>Congratulations! Your site is now online and visible to the world!</p>
</section>
</section>
<section id="cool-my-site-is-online-but-what-now" class="level2">
<h2 class="anchored" data-anchor-id="cool-my-site-is-online-but-what-now">Cool! My site is online but what now?</h2>
<p>The first thing you may want to do is to do a little shakedown of your site. Spend a little bit of time serving your site, browsing through the different widgets, and reading <a href="https://www.wowchemy.com/docs/getting-started/hugo-github-quickstart/">wowchemy’s excellent documentation</a> . While you’re doing this, reflect on what the goal of your site is i.e.&nbsp;what you want to share and how. This will make the configuration process of your site that much easier!</p>
<p>To give you a rough idea on how to start, here was my configuration process:</p>
<ol type="1">
<li>Opened the <code>config</code> folder and edited the basic configuration of the site.</li>
<li>Commented out the widgets I didn’t need in the <code>config/_menus_default/menus.yaml</code> file.</li>
<li>Edited the <code>.yaml</code> files in the <code>content</code> folder</li>
<li>Added my profile picture under <code>content\authors\admin\avatar.jpg</code></li>
<li>Added my PDF CV to <code>static/uploads</code></li>
<li>Added some icons to the <code>assets/media/icons/brands</code> folder</li>
<li>Removed template blog posts</li>
<li>Created this post by running <code>blogdown::new_post()</code></li>
</ol>
</section>
<section id="customize-your-site-further" class="level2">
<h2 class="anchored" data-anchor-id="customize-your-site-further">Customize your site further!</h2>
<p>Want to go further? Change the color of this element or increase the font size of that piece of text? Here are some neat documentation sites, blogs and examples!</p>
<ul>
<li><a href="https://www.apreshill.com/blog/2020-12-new-year-new-blogdown/">Alison Hill’s awesome blog</a></li>
<li><a href="https://isabella-b.com/">Isabella Benabaye’s blog</a></li>
<li>…</li>
</ul>
</section>
<section id="sources" class="level2">
<h2 class="anchored" data-anchor-id="sources">Sources</h2>
<ul>
<li><a href="https://unsplash.com/photos/Ylk5n_nd9dA">Cover image</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-bieri2021" class="csl-entry quarto-appendix-citeas">
Bieri, Bernhard. 2021. <span>“Quickstart Guide to Blogdown.”</span>
November 13, 2021. <a href="https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/">https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/</a>.
</div></div></section></div> ]]></description>
  <category>Guides</category>
  <category>blogdown</category>
  <category>R</category>
  <guid>https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/</guid>
  <pubDate>Sat, 13 Nov 2021 00:00:00 GMT</pubDate>
  <media:content url="https://bernhardbieri.ch/blog/2021-11-13-quickstart-guide-to-blogdown/featured.jpg" medium="image" type="image/jpeg"/>
</item>
</channel>
</rss>
