R

Have a snickers

With all the work around sentiment analysis of Trump’s Tweets, I thought that I would add my observation to the discipline. And with vague memories of an analysis of favourability of judges rulings by time of day

http://www.economist.com/node/18557594

I thought I would check Trump’s tweets for negativity by time of day. This code is essentially that used in the blog piece

http://jacobsimmering.com/2017/02/10/TrumpTweets/

then tweaked by me to get the hour of the UTC tweet and subtract 5 for East Coast U.S. Time (as a quick and dirty time zone adjustment)

library(twitteR)
library(tidyverse)
library(tidytext)
library(lubridate)
api_key = "you_need_to_use_your_own"
api_secret = "you_need_to_use_your_own"
access_token = "you_need_to_use_your_own"
access_token_secret = "you_need_to_use_your_own"
options(httr_oauth_cache=F)
setup_twitter_oauth(api_key,api_secret,access_token,access_token_secret)
trump <- userTimeline("realDonaldTrump",
 n = 3100,
 includeRts = TRUE,
 excludeReplies = FALSE) %>% twListToDF()

words <- trump %>%
 select(id, statusSource, retweetCount, favoriteCount, created, isRetweet, text) %>%
 unnest_tokens(word, text) %>% 
 mutate(android = stringr::str_detect(statusSource, "Android"), tweethour = hour(created) - 5) %>%
 select(- statusSource) %>%
 inner_join(get_sentiments("afinn"))

trumphrs <- words %>% filter(android)

boxplot(trumphrs$score ~ trumphrs$tweethour, varwidth=TRUE)

As indicated in the code, you would need to user your own Twitter developer details to get the code to run.

Which makes a boxplot like:

trumphour

Which suggests the most negative tweets are at about 8am, 10am, 1pm, and 4pm East Coast U.S. time. I will speculate there is a food related pattern, but there is a lot of overlap in the box plots, which is not to surprising given the range of the sentiment index and the range of peoples vocabulary.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s