Lets begin by including libraries

library(rtweet)
library(tidyverse)
library(tidytext)
library(stringr)
library(textdata)
library(DT)
library(RSentiment)
#detach(plyr)

We then gain access to the twitter api

#appname
appname <- "zagooddad"
## api key
key <- "bKi2FQLQIZq8Gkw9UgHqqsZLK"
## api secret 
secret <- "zfAB7oEsfj81PYid3FS6ikFdErJIPekETVKdwYZi8cTTvg8QKF"
access_token <- '1261149569989001217-aPjx2ONqjwdnKnO2fqIyfA3XDV3XBD'
access_secret <- 'Sb6tRGpYb6U74net2Mjv0EgrDZ8MvDchdHgW1uJS1c4rw'


# create token named "twitter_token"
twitter_token <- create_token(
  app = appname,
  consumer_key = key,
  consumer_secret = secret,
  access_token = access_token,
  access_secret = access_secret)

Goal:

At around 1am (UTC) on 12/3/2020, ESPN reported that former MVP, Russell Westbrook has been traded to Washington Wizards.Russell Westbrook has often been described as a “lighting rod” - those who love him will defend him till eternity and those who hate him will slander him until no words remain. I’ve been consulted to guage how people on twitter are feeling about this trade- are they in love with this trade?

To figure this out, I searched for 5000 tweets with the word “Russell Westbrook” between 2020/12/03 and 2020/12/04

num_tweets <- 5000
russ_trade <- search_tweets('Russell Westbrook', n = num_tweets, include_rts = FALSE,lang = "en",since='2020-12-03', until='2020-12-04')

I then looked to answer two questions:

sentiment <- russ_trade[,3:5] %>% unnest_tokens(output = 'word', input ='text')
#add sentiment dataset & merge with words from tweets
sentiment_df<- get_sentiments("afinn")
sentiment_df2<- get_sentiments("bing")
sentiment_df <- arrange(sentiment_df, -value)
sentiment <- merge(sentiment, sentiment_df, by = 'word')
sentiment2 <- merge(sentiment,sentiment_df2, by='word')
#select 
sentiment <- sentiment %>%select('word','created_at','value')
#Find Sentiment Score over time.
sentiment$hour <- format(round(sentiment$created_at, units="hours"), format="%H:%M")
sentiment_over_time <- sentiment[,2:4] %>%
group_by(hour) %>%
summarise(sentiment = mean(value))
## `summarise()` ungrouping output (override with `.groups` argument)
#plot
ggplot(sentiment_over_time[-1,], aes(x = hour, y = sentiment, color="red")) + geom_line(group = 1,width=2) + geom_point() + ggtitle("Sentiment score over time on the Westbrook trade ( +5 positive, -5 negative)") + theme(axis.text.x = element_text(angle = 90))

sentiment2<- sentiment2['sentiment'] %>%
             group_by(sentiment) %>% tally()
sentiment2['counts'] <- sentiment2['n']

Sentiment_from_words = c("Negative", "Postive")
ggplot(sentiment2,aes(x = sentiment, y = counts, fill =Sentiment_from_words))+
geom_bar(alpha = 0.8, stat = "identity", show.legend = TRUE) + ggtitle("Overall counts for positive vs Negative reactions to the Westbrook trade.") + theme_minimal()

### Conclusion:

The table below indicates that tweets about the Russell Westbrook trade were just above neutral(mean of 0.8). Although, we did find more positive words as compared to negative words - we can’t draw much conclusion from that since we’re unsure of the strength of those positive words. Ultimately, a better approach would have been to use entire sentences to obtain sentiment scores as opposed to words only (library(RSentiment) has a calculate_score function that analyzes the complete sentence for a representative score)