<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/"></div></div><div><a data-url="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/" data-counturl="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/" data-count="horizontal" data-text="Analyzing the US elections with Facebook and R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://thinktostart.com/analyzing-us-elections-facebook-r/"> R Tutorials – ThinkToStart</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" href="https://de.wikipedia.org/wiki/Hillary_Clinton">Hillary Clinton</a> and <a target="_blank" href="https://de.wikipedia.org/wiki/Donald_Trump">Donald Trump</a> seem to be the nominees for the upcoming <a target="_blank" href="https://en.wikipedia.org/wiki/United_States_presidential_election,_2016">US presidential election</a> in November 2016. The US presidential election in five months provokes already harsh attacks between the nominees – the election campaigns of Trump und Clinton evolve to one of the toughest competitions in the recent history of US presidential elections – primary via their social media channels.</p>
<p>To derive a more detailed understanding of the nominees’ appearance and fan base in social media channels, we analyzed the Facebook traffic of Clinton (approximately 3.6m Facebook fans) and Trump (approximately 8m Facebook fans) for over 12 months. Facebook is the leading social media websites in the US in 2016 (<a target="_blank" href="http://www.experian.com/">Experian</a>, 2016) and may provide relevant and representative information about the nominees’ supporters and fans.</p>
<h3>Analyzing the US elections with the CTSA index:</h3>
<ol>
<li>The first figure shows the emotional atmosphere within the nominees’ communities. Specifically, negative expressed emotions (relative to positive expressed emotions) varies considerably among Clinton and Trump. Whereas Trump’s community tends to be more positive engaged (positive comments: 69.46%; negative comments: 30.46%), Clinton’s community is relative more negative engaged (positive comments: 65.94%; negative comments: 33.94%). However, the dispersion of emotions (controversy of comments), as shown in Figure 2, is much more notable in Trump’s community (coefficient of variation, as percent: Trump: 122.23 / Clinton: 115.31).</li>
</ol>
<figure class="wp-caption"><a target="_blank" href="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions.png"><img sizes="(max-width: 1211px) 100vw, 1211px" srcset="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions.png?resize=456%2C268 1211w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-300x176.png 300w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-768x452.png 768w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-1024x602.png 1024w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-696x409.png 696w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-1068x628.png 1068w, http://thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions-714x420.png 714w" alt="Negative_Emotions" class="wp-image-2185" src="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Negative_Emotions.png?resize=456%2C268" data-recalc-dims="1"></a><br>
<figcaption class="wp-caption-text">Figure 1: Negative Emotions</figcaption>
</figure>
<figure class="wp-caption"><a target="_blank" href="http://i0.wp.com/thinktostart.com/wp-content/uploads/2016/06/Discrepancy.png"><img sizes="(max-width: 1211px) 100vw, 1211px" srcset="http://i0.wp.com/thinktostart.com/wp-content/uploads/2016/06/Discrepancy.png?resize=456%2C268 1211w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-300x176.png 300w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-768x451.png 768w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-1024x601.png 1024w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-696x409.png 696w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-1068x627.png 1068w, http://thinktostart.com/wp-content/uploads/2016/06/Discrepancy-715x420.png 715w" alt="Discrepancy" class="wp-image-2183" src="http://i0.wp.com/thinktostart.com/wp-content/uploads/2016/06/Discrepancy.png?resize=456%2C268" data-recalc-dims="1"></a><br>
<figcaption class="wp-caption-text">Figure 2: Discrepancy</figcaption>
</figure>
<ol start="2">
<li>The third figure shows how optimistic the followers express themselves. The data show that Clinton’s fan base is slightly more optimistic (31.13%), than Trump’s fans base (29.57%), whereby the trend shows, that there is a more positive development in Trump’s community. Again, the dispersion of optimistic comments is much more notable in Trump’s community (coefficient of variation, as percent: Trump: 130.12 / Clinton: 126.11).</li>
</ol>
<figure class="wp-caption"><a target="_blank" href="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Optimism.png"><img sizes="(max-width: 1210px) 100vw, 1210px" srcset="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Optimism.png?resize=456%2C268 1210w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-300x177.png 300w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-768x452.png 768w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-1024x603.png 1024w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-696x410.png 696w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-1068x628.png 1068w, http://thinktostart.com/wp-content/uploads/2016/06/Optimism-714x420.png 714w" alt="Optimism" class="wp-image-2186" src="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Optimism.png?resize=456%2C268" data-recalc-dims="1"></a><br>
<figcaption class="wp-caption-text">Figure 3: Optimism</figcaption>
</figure>
<ol start="3">
<li>Finally, the fourth figure shows the communities degree of self-centered style of expression, that is, if the communities write more in a more ‘inclusive’ (e.g., ‘we’) or ‘exclusive’ (e.g., ‘I’) style. The style of expression shows significant differences between Clinton and Trump. Trump’s fans express themselves in a more ‘exclusive’ style (36.12%), whereas Clinton’s fans express themselves in a more ‘inclusive’ style (30.28%).</li>
</ol>
<figure class="wp-caption"><a target="_blank" href="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Exclusive.png"><img sizes="(max-width: 1208px) 100vw, 1208px" srcset="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Exclusive.png?resize=456%2C268 1208w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-300x176.png 300w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-768x451.png 768w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-1024x602.png 1024w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-696x409.png 696w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-1068x628.png 1068w, http://thinktostart.com/wp-content/uploads/2016/06/Exclusive-715x420.png 715w" alt="Exclusive" class="wp-image-2184" src="http://i1.wp.com/thinktostart.com/wp-content/uploads/2016/06/Exclusive.png?resize=456%2C268" data-recalc-dims="1"></a><br>
<figcaption class="wp-caption-text">Figure 4: Self-centered Style of Expression</figcaption>
</figure>
<p>Data: We crawled the nominees’ public page Facebook data, starting May 01, 2015 until May 31, 2016 via R ‘<a target="_blank" href="https://cran.r-project.org/web/packages/Rfacebook/index.html">Rfacebook</a>’. Specifically, we request all posts and corresponding comments for the entire time period (Clinton: approx. 1.2m comments / Trump: approx. 1.4m comments). Following this, each comment was analyzed separately with respect to emotional and psychological constructs (the categories are based on the <a target="_blank" href="http://liwc.wpengine.com/">LIWC dictionary</a>) with R ‘<a target="_blank" href="https://cran.r-project.org/web/packages/tm/index.html">tm</a>’ and ‘<a target="_blank" href="https://cran.r-project.org/web/packages/quanteda/index.html">quanteda</a>’. The analysis does not include non-English content. Finally, we aggregate the data (comments) on a daily basis.</p>
<p>The data files are attached (for interactive graphics, open the txt files via Internet browser).</p>
<p><a target="_blank" href="http://thinktostart.com/wp-content/uploads/2016/06/Figure4.txt">Figure1 Figure2 Figure3 Figure4</a></p>
<p>Here is a stylized example of the basic code (the code is limited to one candidate (Hillary Clinton), one day (2016-07-07), and refers to a <a target="_blank" href="https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html#lexicon">public available dictionary</a> (positive/negative word). The original analysis is based on the <a target="_blank" href="http://liwc.wpengine.com/">LIWC dictionary</a>.</p>
<h3>Analyzing the US elections with Facebook and R</h3>

<pre>library(&quot;Rfacebook&quot;)
library(&quot;stringr&quot;)
library(&quot;reshape2&quot;)
#############################################################################
# Request Acces token (!!!!access token will only be valid for two hours!!!!)via: 
# https://developers.facebook.com/tools/explorer/
# Requires Facebook Account
token &lt;- &quot;XXXX&quot;

#############################################################################
# Request posts
# Simple Example: Hillary Clinton (posts for one day: 2017-07-07)
fb_page&lt;- getPage(page=&quot;889307941125736&quot;, token=token,  since='2016/07/07', until='2016/07/08')
fb_page$order &lt;- 1:nrow(fb_page) 

# Function to download the comments
download.maybe &lt;- function(i, refetch=FALSE, path=&quot;.&quot;) {
  post &lt;- getPost(post=fb_page$id[i], comments = TRUE, likes = TRUE, token=token)
  post1&lt;- as.data.frame(melt(post)) 
}

# Apply function to download comments
files &lt;-data.frame(melt(lapply(fb_page$order, download.maybe)))

# Select only comments
files_c&lt;-files[complete.cases(files$message),]

# Split ID to abstract POST_ID
files_c$id2 &lt;- lapply(strsplit(as.character(files_c$id), &quot;_&quot;), &quot;[&quot;, 1)
files_c$ch  &lt;- nchar(files_c$id2) 
files_a &lt;- files_c[ which(files_c$ch >12), ]

# Change column name
names(files_a)[11]&lt;-&quot;POST_ID&quot;

# Define date
files_a$date &lt;- lapply(strsplit(as.character(files_a$created_time), &quot;T&quot;), &quot;[&quot;, 1)
files_a$date1&lt;-as.character(files_a$date)

# Dine identifier to count comments
files_a$tempID&lt;-1

#############################################################################
# Clean Data
dat2&lt;-gsub(&quot;[^[:alnum:]///' ]&quot;, &quot;&quot;, files_a$message)
dat2&lt;-data.frame(dat2)

dat3&lt;-gsub(&quot;([.-])|[[:punct:]]&quot;, &quot; &quot;, dat2$dat2)
dat3&lt;-data.frame(dat3)
dat4&lt;-iconv(dat3$dat3, &quot;latin1&quot;, &quot;ASCII&quot;, sub=&quot;&quot;)
dat4&lt;-data.frame(dat4)

dat5&lt;-gsub('[[:digit:]]+', '',dat4$dat4)
dat5&lt;-data.frame(dat5)

dat6&lt;-tolower(dat5$dat5)
dat6&lt;-data.frame(dat6)

dat7&lt;-gsub(&quot;'&quot;, &quot; &quot;, dat6$dat6)
dat7&lt;-data.frame(dat7)

dat8&lt;-gsub(&quot;/&quot;, &quot; &quot;, dat7$dat7)
dat8&lt;-data.frame(dat8)

#############################################################################
# Steps to replace empty entries
# Function to replace blanks with missing NA 
blank2na &lt;- function(x){
  z &lt;- gsub(&quot;\\s+&quot;, &quot;&quot;, x)  #make sure it's &quot;&quot; and not &quot; &quot; etc
  x[z==&quot;&quot;] &lt;- NA
  return(x)
}

# Replace blanks with 'NA'
dat10&lt;-data.frame(sapply(dat8,  blank2na))
dat10&lt;-data.frame(dat10)

# Define the relevant column as numeric
dat12&lt;-as.numeric(dat10$dat8)
dat12&lt;-data.frame(dat12)

# Define function if entry is numeric(non-numeric) 
f &lt;- function(x) is.numeric(x) &amp; !is.na(x) # Apply function
dat14&lt;-f(dat12$dat12)
dat14&lt;-data.frame(dat14)

# Reverse definition of numeric/character
dat16&lt;-as.character(ifelse(dat14$dat14 == &quot;FALSE&quot;, dat8$dat8, 1010101010101010))
dat16&lt;-data.frame(dat16)
dat16&lt;-as.character(dat16$dat16)

# Combine NA and real value !!!!Select a individual Term (here: &quot;Hallo&quot;)!!!!
dat8 &lt;-as.character(dat8$dat8)
dat17&lt;-ifelse(dat16 != 1010101010101010, &quot;HALLO&quot;, dat8)
dat17&lt;-data.frame(dat17)

#############################################################################
# Define Corpus
dat17$ch           &lt;- nchar(as.character(dat17$dat17))
dat17$bb           &lt;- ifelse(dat17$ch&lt;4, &quot;HALLO&quot;, as.character(dat17$dat17))
dat18              &lt;- as.data.frame(dat17$bb [ grep(&quot;nchar&quot;, dat17$bb ) ])
dat17$dat17        &lt;-as.character(dat17$dat17)
dat_r              &lt;-as.data.frame(dat17)
colnames(dat_r)[1] &lt;-&quot;dat_r&quot;
dat_r$dat_r        &lt;-as.character(dat_r$dat_r)
corpus             &lt;- corpus(dat_r$dat_r)

#############################################################################
# Load Dictionary (https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html#lexicon)
# Negative/Positive Words
hu.liu.pos=scan('D:/positive-words.txt', what='character', comment.char = ';')
hu.liu.neg=scan('D:/negative-words.txt', what='character', comment.char = ';')
# Optional: Add Words to List
pos.words=c(hu.liu.pos, 'like')
neg.words=c(hu.liu.neg, 'bad')

# Combine Dictionnarys
myDict &lt;- dictionary(list(positive = pos.words,
                          negative = neg.words))

#############################################################################
# Apply Dictionary
fb_liwc &lt;-dfm(corpus, dictionary=myDict)
fb1&lt;-as.data.frame(fb_liwc)

#############################################################################
# Combine Analysis Data and Original Data
ALL&lt;-cbind(files_a,fb1)</pre>

<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="http://thinktostart.com/analyzing-us-elections-facebook-r/">Analyzing the US elections with Facebook and R</a> appeared first on <a target="_blank" rel="nofollow" href="http://thinktostart.com/">ThinkToStart</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/"></div></div><div><a data-url="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/" data-counturl="http://www.r-bloggers.com/analyzing-the-us-elections-with-facebook-and-r/" data-count="vertical" data-text="Analyzing the US elections with Facebook and R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://thinktostart.com/analyzing-us-elections-facebook-r/"> R Tutorials – ThinkToStart</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/8KxfbBWXouA"></a></div>