<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-the-igraph-package-to-analyse-the-enron-corpus/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/analyse-enron-corpus/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This blog post analyzes an extensive collection of e-mails from former Enron employees. The Enron corpus is analysed using network analysis tools provided by the <a rel="nofollow" href="http://igraph.org/r/" target="_blank">iGraph package</a>. Network analysis is a versatile technique that can be used to add value to a lot of different data sets, including the complex corporate relationships of <a rel="nofollow" href="http://r.prevos.net/trumpworld-analysis/" target="_blank">Donald Trump</a>.</p>
<p>The Enron scandal is one of the most famous corporate governance failures in the history of capitalism. The case itself is interesting for its sake, but in this post, I am more interested in one of the data sets that the subsequent investigation has provided.</p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/jrEf8uabe7E?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" width="450" type="text/html"></iframe></p>
<h2>The Enron Corpus</h2>
<p>As part of their inquiries, The Federal Energy Regulatory Commission used an extensive collection of emails from Enron employees. The Enron corpus is one of the only publicly available collections of emails available for research. This dataset also provides a fascinating playground for citizen data scientists.</p>
<p>The set has privacy issues as it contains messages from living people. When analysing this data set, we need to keep in mind that the majority of former Enron employees were innocent people who lost their jobs due to the greed of their overlords. The code in this post only analyses the e-mail headers, ignoring the content.</p>
<div class="wp-caption"><a rel="nofollow" href="http://www.cnbc.com/2014/10/13/energy-industry-the-10-biggest-energy-company-bankruptcies.html" target="_blank"><img alt="Laid-off Enron employees outside Enron headquarters as the company collapsed in 2001 - Enron corpus analysis" src="https://i2.wp.com/fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2014/10/13/102082546-675237.530x298.jpg?w=450&ssl=1" data-recalc-dims="1"></a>
<p class="wp-caption-text">Laid-off Enron employees outside Enron headquarters as the company collapsed in 2001.</p>
</div>
<p>The Enron Corpus is a large database of half a million of emails generated by more than 100 Enron employees. You can download the corpus from the <a rel="nofollow" href="https://www.cs.cmu.edu/~./enron/" target="_blank">Carnegie Mellon School of Computer Science</a>. The first code snippet downloads the 7 May 2015 version of the dataset (about 423Mb, tarred and gzipped) and untars it to your working directory.</p>
<pre>
# Enron Email Dataset: https://www.cs.cmu.edu/~./enron/
download.file(&quot;https://www.cs.cmu.edu/~./enron/enron_mail_20150507.tgz&quot;, destfile = &quot;enron_mail_20150507.tgz&quot;)
untar(&quot;enron_mail_20150507.tgz&quot;)
</pre>
<h2>Preparing the Data</h2>
<p>The main folder is <code>maildir</code>, which holds all the personal accounts. Our first task is to load the required libraries and create a list of available emails. This code results in 517,401 e-mail files with 44,859 emails in the inboxes of users.</p>
<pre>
# E-mail corpus consists of nested folders per user with e-mails as text files
# Create list of all available e-mails
emails &lt;- list.files(&quot;maildir/&quot;, full.names = T, recursive = T)
length(emails)
# Filter by inbox only
emails &lt;- emails[grep(&quot;/inbox&quot;, emails)]
length(emails)
</pre>
<p>The bulk of the code creates a list of emails between Enron employees. The code performs a lot of string manipulations to extract the information from the text files. The content of the e-mails is ignored, the code only retrieves the sender and the receiver. The analysis is limited to e-mails between employees in the corpus. Only those receivers whose inbox forms part of the analysis are included. The result of this code is a data frame with the usernames of the sender and receiver for each email. The data frame contains 2779 emails that meet the criteria.</p>
<pre>
# Create list of sender and receiver (inbox owner)
inboxes &lt;- data.frame(
    from = apply(as.data.frame(emails), 1, function(x){readLines(x, warn = F)[3]}),
    to = emails,
    stringsAsFactors = F
)

# Keep only enron.com and strip all but username
library(stringr) # String manipulation
inboxes &lt;- inboxes[grepl(&quot;@enron.com&quot;, inboxes$from),]
inboxes$from &lt;- str_sub(inboxes$from, 7, nchar(inboxes$from) - 10)
to &lt;- str_split(inboxes$to, &quot;/&quot;)
inboxes$to &lt;- sapply(to, &quot;[&quot;, 3)

# Create list of usernames
users &lt;- data.frame(user = paste0(&quot;maildir/&quot;, unique(inboxes$to)))

# Remove those without sent mails
sent &lt;- apply(users, 1, function(x){sum(grepl(&quot;sent&quot;, dir(x)))})
users &lt;- subset(users, sent != 0)

# Replace username with e-mail name
users$mailname &lt;- NA
for (i in 1:nrow(users)){
sentmail &lt;- dir(paste0(users$user[i], &quot;/sent_items/&quot;))
name &lt;- readLines(paste0(users$user[i], &quot;/sent_items/&quot;, sentmail[1]), warn = F)[3]
name &lt;- str_sub(name, 7, nchar(name)-10)
users$mailname[i] &lt;- name
}
users$user &lt;- str_sub(users$user, 9)
inboxes &lt;- merge(inboxes, by.x=&quot;to&quot;, users, by.y=&quot;user&quot;)
inboxes &lt;- data.frame(from = inboxes$from, to = inboxes$mailname)

inboxes$from &lt;- as.character(inboxes$from)
inboxes$to &lt;- as.character(inboxes$to)

# Only e-mails between inbox users
inboxes &lt;- inboxes[inboxes$from %in% inboxes$to,]

# Remove no.address
inboxes &lt;- subset(inboxes, from != &quot;no.address&quot; &amp; to != &quot;no.address&quot;)

# Remove emails to self
inboxes &lt;- subset(inboxes, inboxes$from != inboxes$to)
</pre>
<h2>Network Analysis</h2>
<p>The last code snippet defines a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)" target="_blank">graph</a> from the table of e-mails. Each employee is a node in the network, and each e-mail is an edge (line). The iGraph package is a powerful tool to analyse networks. The <code>graph_from_edgelist</code> function creates a network object that can be analysed using the iGraph package. The graph is directed because the information flows from the sender to the receiver.</p>
<p>In the next step, the Spingglass algorithm finds <a rel="nofollow" href="https://en.wikipedia.org/wiki/Community_structure" target="_blank">community structure</a> within the data. A community is a group of nodes that are more connected with each other than with any other node.</p>
<p>The last step visualises the network. The diagram is drawn using the Fruchterman-Reingold algorithm, which places the most connected nodes at the centre of the picture. The background colours in the diagram indicate the eight communities.</p>
<p>The graph tells us a lot about the group of employees in the Enron corpus and how they relate to each other. Each of the communities represents a tightly connected group of employees that mainly e-mail each other. Any connections between communities are shown in red. When the <code>vertex.label = NA</code> line is removed, the usernames are displayed at each node.</p>
<p>We can see groups that never mail each other, lonely hangers-on and tightly knit cliques within Enron. In the centre fo the graph we see w few individuals who are connectors between groups because they send a lot of emails to people outside their community. On the fringes of the graph are the hangers-on who only once or twice emailed somebody in the corpus but were still included in the investigation.</p>
<pre>
library(igraph)
g &lt;- graph_from_edgelist(as.matrix(inboxes), directed = T)
coms &lt;- spinglass.community(g)

# Plot network
par(mar = c(0,0,2,0))
plot(coms, g, 
     vertex.label=NA, 
     layout = layout.fruchterman.reingold,
     vertex.size = 3
)
</pre>
<p>This analysis provides only a quick glimpse into the knowledge contained in the Enron corpus. An extensive range of tools is available to analyse such networks. An interesting exercise would be to overlap this network with the organisation chart to see the relationships between teams. Have fun playing with this fantastic data set!</p>
<p><a rel="nofollow" href="http://r.prevos.net/?attachment_id=756" target="_blank"><img data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/enron.png?w=450" data-orig-size="2048,2048" data-medium-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/07/enron-300x300.png" data-attachment-id="756" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/07/enron.png?w=450" data-permalink="http://r.prevos.net/analyse-enron-corpus/enron/" data-large-file="http://r.prevos.net/wp-content/uploads/sites/11/2017/07/enron-1024x1024.png" alt="Enron corpus network with communities." data-image-title="Enron corpus e-mail network" sizes="(max-width: 2048px) 100vw, 2048px" data-comments-opened="1" class="wp-image-756" data-recalc-dims="1"></a></p>
<p> </p>
<p>The post <a rel="nofollow" href="http://r.prevos.net/analyse-enron-corpus/" target="_blank">Using the iGraph package to Analyse the Enron Corpus</a> appeared first on <a rel="nofollow" href="http://r.prevos.net/" target="_blank">The Devil is in the Data</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/analyse-enron-corpus/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ishdtrdqq_8" height="1"></a></div>