<p>Last summer, David Robinson did this interesting <a href="http://varianceexplained.org/r/trump-tweets/">text analysis</a> of Donald Trump’s tweets and found that they more angry ones came from Android (which Trump is known to use). But he didn’t consider how Trump’s emotional state varies over time and he certainly couldn’t have considered what the impact of the election and recent events would have been on Trump.</p>
<p>Using the <code>twitteR</code> package and the <code>tidyverse</code> ecosystem (plus <code>tidytext</code>) this is actually a very simple analysis.</p>
<p>For starters, pulling Trump’s tweets (the last 3,200) is very simple:</p>
<pre><code>library(twitteR)
library(tidyverse)
library(tidytext)

source(&quot;~/twitter_key.R&quot;)

setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)</code></pre>
<pre><code>## [1] &quot;Using direct authentication&quot;</code></pre>
<pre><code>trump &lt;- userTimeline(&quot;realDonaldTrump&quot;,
                      n = 3100,
                      includeRts = TRUE,
                      excludeReplies = FALSE) %>%
  twListToDF() %>%
  as_tibble()</code></pre>
<p>And then we have a tidy tibble with Trump’s tweets:</p>
<pre><code>glimpse(trump)</code></pre>
<pre><code>## Observations: 3,099
## Variables: 16
## $ text          &lt;chr> &quot;Heading to Joint Base Andrews on #MarineOne wit...
## $ favorited     &lt;lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,...
## $ favoriteCount &lt;dbl> 77699, 85576, 71312, 220083, 64348, 84125, 62284...
## $ replyToSN     &lt;chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ created       &lt;time> 2017-02-10 23:24:51, 2017-02-10 13:35:50, 2017-...
## $ truncated     &lt;lgl> FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, ...
## $ replyToSID    &lt;chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ id            &lt;chr> &quot;830195857530183684&quot;, &quot;830047626414477312&quot;, &quot;830...
## $ replyToUID    &lt;chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ statusSource  &lt;chr> &quot;&lt;a href=\&quot;http://twitter.com/download/iphone\&quot; ...
## $ screenName    &lt;chr> &quot;realDonaldTrump&quot;, &quot;realDonaldTrump&quot;, &quot;realDonal...
## $ retweetCount  &lt;dbl> 21473, 19779, 15069, 64363, 10082, 14185, 11294,...
## $ isRetweet     &lt;lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,...
## $ retweeted     &lt;lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,...
## $ longitude     &lt;chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...
## $ latitude      &lt;chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ...</code></pre>
<p>Using <code>tidytext</code>, it is straightforward to unnest and tokenize the words in the body of the tweets:</p>
<pre><code>words &lt;- trump %>%
  select(id, statusSource, retweetCount, favoriteCount, created, isRetweet,
         text) %>%
  unnest_tokens(word, text)
words</code></pre>
<pre><code>## # A tibble: 57,239 x 7
##                    id
##                 &lt;chr>
## 1  830195857530183684
## 2  830195857530183684
## 3  830195857530183684
## 4  830195857530183684
## 5  830195857530183684
## 6  830195857530183684
## 7  830195857530183684
## 8  830195857530183684
## 9  830195857530183684
## 10 830195857530183684
## # ... with 57,229 more rows, and 6 more variables: statusSource &lt;chr>,
## #   retweetCount &lt;dbl>, favoriteCount &lt;dbl>, created &lt;time>,
## #   isRetweet &lt;lgl>, word &lt;chr></code></pre>
<p>Given what David Robinson found, we might want to convert the <code>statusSource</code> variable into a flag for whether it was posted via an Android device:</p>
<pre><code>words &lt;- words %>%
  mutate(android = stringr::str_detect(statusSource, &quot;Android&quot;)) %>%
  select(- statusSource)
words</code></pre>
<pre><code>## # A tibble: 57,239 x 7
##                    id retweetCount favoriteCount             created
##                 &lt;chr>        &lt;dbl>         &lt;dbl>              &lt;time>
## 1  830195857530183684        21473         77699 2017-02-10 23:24:51
## 2  830195857530183684        21473         77699 2017-02-10 23:24:51
## 3  830195857530183684        21473         77699 2017-02-10 23:24:51
## 4  830195857530183684        21473         77699 2017-02-10 23:24:51
## 5  830195857530183684        21473         77699 2017-02-10 23:24:51
## 6  830195857530183684        21473         77699 2017-02-10 23:24:51
## 7  830195857530183684        21473         77699 2017-02-10 23:24:51
## 8  830195857530183684        21473         77699 2017-02-10 23:24:51
## 9  830195857530183684        21473         77699 2017-02-10 23:24:51
## 10 830195857530183684        21473         77699 2017-02-10 23:24:51
## # ... with 57,229 more rows, and 3 more variables: isRetweet &lt;lgl>,
## #   word &lt;chr>, android &lt;lgl></code></pre>
<p>Let’s now code the tweets using the <code>afinn</code> sentiment set:</p>
<pre><code>words &lt;- words %>%
  inner_join(get_sentiments(&quot;afinn&quot;))</code></pre>
<pre><code>## Joining, by = &quot;word&quot;</code></pre>
<pre><code>words</code></pre>
<pre><code>## # A tibble: 5,093 x 8
##                    id retweetCount favoriteCount             created
##                 &lt;chr>        &lt;dbl>         &lt;dbl>              &lt;time>
## 1  830047626414477312        19779         85576 2017-02-10 13:35:50
## 2  830047626414477312        19779         85576 2017-02-10 13:35:50
## 3  830042498806460417        15069         71312 2017-02-10 13:15:27
## 4  829721019720015872        10082         64348 2017-02-09 15:58:01
## 5  829721019720015872        10082         64348 2017-02-09 15:58:01
## 6  829689436279603206        14185         84125 2017-02-09 13:52:31
## 7  829689436279603206        14185         84125 2017-02-09 13:52:31
## 8  829689436279603206        14185         84125 2017-02-09 13:52:31
## 9  829689436279603206        14185         84125 2017-02-09 13:52:31
## 10 829689436279603206        14185         84125 2017-02-09 13:52:31
## # ... with 5,083 more rows, and 4 more variables: isRetweet &lt;lgl>,
## #   word &lt;chr>, android &lt;lgl>, score &lt;int></code></pre>
<p>And now let’s see how the typical sentiment of those tweets has varied since April 2016 (midsts of the Republican primary) to present:</p>
<pre><code>words %>%
  filter(isRetweet == FALSE) %>%
  group_by(id, created) %>%
  summarize(sentiment = mean(score)) %>%
  ggplot(aes(x = created, y = sentiment)) + 
  geom_smooth() + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2017-01-20&quot;)))) + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2016-11-08&quot;)))) + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2016-05-03&quot;)))) + 
  labs(x = &quot;Date&quot;, y = &quot;Mean Afinn Sentiment Score&quot;)</code></pre>
<p></p>
<p>The vertical lines denote the date he was named as the Republican candidate (May 3rd 2016), the date of the election (Nov 8th 2016) and inauguration day. Thing aren’t looking up for Trump. He seems to be more angry/sad/negative now than any prior point during the past year.</p>
<p>What if we consider the grouping by using Android vs not:</p>
<pre><code>words %>%
  filter(isRetweet == FALSE) %>%
  group_by(id, created, android) %>%
  summarize(sentiment = mean(score)) %>%
  ggplot(aes(x = created, y = sentiment, color = android)) + 
  geom_smooth() + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2017-01-20&quot;)))) + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2016-11-08&quot;)))) + 
  geom_vline(xintercept = as.numeric(as.POSIXct((&quot;2016-05-03&quot;)))) + 
  labs(x = &quot;Date&quot;, y = &quot;Mean Afinn Sentiment Score&quot;)</code></pre>
<p></p>
<p>We see the general trend that David Robinson identified - the Android tweets tended to be more negitive than the other platforms. It is interesting that they were more positive than the tweets presumed to be by staff right before the election. Also, we can see the non-Android tweets were more positive during the transition than the Android tweets that clearly became more negitive. Perhaps the <a href="https://www.washingtonpost.com/opinions/the-9th-circuit-ruling-is-the-most-hopeful-sign-yet/2017/02/10/78a7b9ca-efba-11e6-9973-c5efb7ccfb0d_story.html?utm_term=.3a533a8e4087">limits of Presidential powers</a> are stricter than he expected. It is interesting that the Android tweets are now more negitive than positive, the first time this has occurred.</p>
<p>Interestingly, there seems to be no effect of being positive/negitive on the number of retweets</p>
<pre><code>words %>%
  filter(isRetweet == FALSE) %>%
  group_by(id, created, android) %>%
  summarize(sentiment = mean(score)) %>%
  inner_join(select(words, id, retweetCount, favoriteCount) %>%
               distinct()) %>%
  ggplot(aes(x = sentiment, y = retweetCount, color = android)) + 
  geom_smooth() + 
  geom_point() + 
  scale_y_log10() + 
  labs(x = &quot;Mean Afinn Sentiment Score&quot;, y = &quot;Number of Retweets&quot;)</code></pre>
<pre><code>## Joining, by = &quot;id&quot;</code></pre>
<p></p>
<p>or the number of favorites</p>
<pre><code>words %>%
  filter(isRetweet == FALSE) %>%
  group_by(id, created, android) %>%
  summarize(sentiment = mean(score)) %>%
  inner_join(select(words, id, retweetCount, favoriteCount) %>%
               distinct()) %>%
  ggplot(aes(x = sentiment, y = favoriteCount, color = android)) + 
  geom_smooth() + 
  geom_point() + 
  scale_y_log10() + 
  labs(x = &quot;Mean Afinn Sentiment Score&quot;, y = &quot;Number of Favorites&quot;)</code></pre>
<pre><code>## Joining, by = &quot;id&quot;</code></pre>
<p></p>
<p>that a tweet gets.</p>
<p>Regression analysis suggests that the number of retweets is increased significantly by a more negitive tweet but that also the effect wears off with time (very very slightly):</p>
<pre><code>words %>%
  filter(isRetweet == FALSE, android) %>%
  group_by(id, created) %>%
  summarize(sentiment = mean(score)) %>%
  inner_join(select(words, id, retweetCount, favoriteCount) %>%
               distinct()) %>%
  lm(log(retweetCount) ~ created * (sentiment &lt; 0), data = .) %>%
  summary()</code></pre>
<pre><code>## Joining, by = &quot;id&quot;</code></pre>
<pre><code>## 
## Call:
## lm(formula = log(retweetCount) ~ created * (sentiment &lt; 0), data = .)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.7744 -0.3806  0.0005  0.3576  3.2661 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               -1.012e+02  3.942e+00 -25.679  &lt; 2e-16 ***
## created                    7.488e-08  2.680e-09  27.939  &lt; 2e-16 ***
## sentiment &lt; 0TRUE          1.959e+01  6.086e+00   3.219  0.00132 ** 
## created:sentiment &lt; 0TRUE -1.313e-08  4.135e-09  -3.175  0.00154 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5923 on 1198 degrees of freedom
## Multiple R-squared:  0.5195, Adjusted R-squared:  0.5183 
## F-statistic: 431.7 on 3 and 1198 DF,  p-value: &lt; 2.2e-16</code></pre>
<p>A similar pattern exists for the number of favorites</p>
<pre><code>words %>%
  filter(isRetweet == FALSE, android) %>%
  group_by(id, created) %>%
  summarize(sentiment = mean(score)) %>%
  inner_join(select(words, id, retweetCount, favoriteCount) %>%
               distinct()) %>%
  lm(log(favoriteCount) ~ created * (sentiment &lt; 0), data = .) %>%
  summary()</code></pre>
<pre><code>## Joining, by = &quot;id&quot;</code></pre>
<pre><code>## 
## Call:
## lm(formula = log(favoriteCount) ~ created * (sentiment &lt; 0), 
##     data = .)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.75782 -0.35691 -0.00795  0.33800  2.48914 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               -1.176e+02  3.452e+00 -34.068  &lt; 2e-16 ***
## created                    8.689e-08  2.347e-09  37.020  &lt; 2e-16 ***
## sentiment &lt; 0TRUE          1.435e+01  5.329e+00   2.692  0.00721 ** 
## created:sentiment &lt; 0TRUE -9.648e-09  3.621e-09  -2.664  0.00781 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5187 on 1198 degrees of freedom
## Multiple R-squared:  0.6525, Adjusted R-squared:  0.6517 
## F-statistic: 749.9 on 3 and 1198 DF,  p-value: &lt; 2.2e-16</code></pre>
<p>The words used by the Android postings that were positive and negitive varied from before the election, during the transition and after Trump was sworn in:</p>
<pre><code>words %>%
  filter(android) %>%
  mutate(phase = ifelse(as.POSIXct(&quot;2016-11-08&quot;) > created, &quot;Before the election&quot;,
                        ifelse(as.POSIXct(&quot;2017-01-20&quot;) > created, &quot;Transition&quot;,
                                          &quot;In the White House&quot;))) %>%
  group_by(phase, pos_sentiment = score >= 0, word) %>%
  count() %>%
  group_by(phase, pos_sentiment) %>%
  filter(word != &quot;no&quot;) %>%
  top_n(3, wt = n) %>%
  arrange(pos_sentiment, phase, desc(n))</code></pre>
<pre><code>## Source: local data frame [18 x 4]
## Groups: phase, pos_sentiment [6]
## 
##                  phase pos_sentiment      word     n
##                  &lt;chr>         &lt;lgl>     &lt;chr> &lt;int>
## 1  Before the election         FALSE       bad    62
## 2  Before the election         FALSE dishonest    27
## 3  Before the election         FALSE    rigged    25
## 4   In the White House         FALSE       bad    10
## 5   In the White House         FALSE      fake    10
## 6   In the White House         FALSE       ban     6
## 7           Transition         FALSE       bad    13
## 8           Transition         FALSE     wrong    11
## 9           Transition         FALSE dishonest    10
## 10 Before the election          TRUE     great   175
## 11 Before the election          TRUE     thank    69
## 12 Before the election          TRUE       big    54
## 13  In the White House          TRUE     great     8
## 14  In the White House          TRUE       big     5
## 15  In the White House          TRUE       win     5
## 16          Transition          TRUE     great    56
## 17          Transition          TRUE       big    16
## 18          Transition          TRUE       win    14</code></pre>
<p>We have the fake news to thank for the <code>fake</code> debut post-being sworn in. At least the election was no longer <code>rigged</code> after he worn it.</p>