Could sentiment analysis be capable of revealing the World Cup 2018 winner?
I don’t usually watch soccer matches, but the World Cup has become a key sporting event in the realm of data science. There are many techniques that companies like Microsoft, UBS, and others are using to attempt to predict this year’s winner.
Some of these techniques involve highly complex mathematical models that take into account factors such as previous player performance, player stats, weather outlook, and many others.
However, could we potentially use a sentiment analysis to determine which team will ultimately take home the trophy? Does overall sentiment significantly affect player performance, and hence the match outcome?
To answer this question, I decided to conduct a sentiment analysis for countries that were at the top of their respective group leaderboards at the time of writing (Belgium, Croatia, England, France, Japan, Portugal, Russia, Senegal, Serbia, Spain, Sweden, and Uruguay).
As a disclosure, I don’t have any bets or other vested interests on any particular team. This is simply an excuse for me to do interesting data science!
Streaming tweets with twitteR library
Firstly, let us load the twitteR library and authenticate:
library(twitteR) setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
The next thing I’m going to do is stream 1000 tweets since 10th June. I’m going to do this by country, so the first term will be “Belgium world cup”, “Croatia world cup”, and so on…
worldcup<-searchTwitter("belgium world cup", n=1000, since='2018-06-10')
df <- twListToDF(worldcup)
As you can see, we are downloading all the relevant tweets for Belgium, and saving them to a data frame titled df. We will now run the sentiment analysis as below.
Sentiment Analysis (dplyr, ggplot2, tidytext, tm)
There are a number of things I am doing here. Firstly, I am unlisting the df dataframe, and storing one word per observation in a new dataframe titled wctext2.
Additionally, I am removing various "stop words", or words that occur frequently but are of no relevance to the overall analysis.
wctext<-paste(unlist(df), collapse=" ") str(df) library(dplyr) wctext <- data_frame(line = 1, text = wctext) wctext library(tidytext) wctext2 <- wctext %>% unnest_tokens(word,text) data(stop_words) wctext2 <- wctext2 %>% anti_join(stop_words)
There are still some stopwords such as twitter, href, and others that are not removed initially. In this instance, I will create a separate text file (stopwords.txt) with the stop words I wish to remove, and use the tm library to remove these from wctext2.
setwd("C:/Users/michaeljgrogan/Documents/Desktop")
library(tm)
stopwords = readLines('stopwords.txt') #Your stop words file
x = wctext2$word #Company column data
x = removeWords(x,stopwords) #Remove stopwords
wctext2 <- x #Add the list as new column and check
wctext2<-data.frame(wctext2)
Now, let's store these words in what is called a tibble. A tibble allows us to rank the words by their frequency. e.g. if a particular word appears 100 times, then n = 100.
tibble<-wctext2 %>%
count(wctext2,sort=TRUE)
col_headings<-c("word","n")
names(tibble) <- col_headings
tibblefiltered = tibble %>% filter(n > 1)
attach(tibblefiltered)
barplot(tibblefiltered$n, main="Word Frequency",
xlab="Word", names.arg=tibblefiltered$word)
str(tibblefiltered)
barsentiment <- tibblefiltered %>%
inner_join(get_sentiments("bing"), by = c("word"))
rm(list= ls()[!(ls() %in% c('barsentiment'))])
attach(barsentiment)
Now, let us use ggplot2 to plot a sentiment analysis.
library(ggplot2)
barsentiment %>%
count(sentiment, word, n=n) %>%
ungroup() %>%
filter(n >= 1) %>%
mutate(n = ifelse(sentiment == "negative", -n, n)) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n, fill = sentiment)) +
geom_bar(stat = "identity") +
ylab("Contribution to sentiment") +
coord_flip()
Here is what the sentiment analysis looks like.
Belgium
Now, let's do this for the other countries.
Croatia
England
France
Japan
Portugal
Russia
Senegal
Serbia
Spain
Sweden
Uruguay
And, who is the winner?
We see from the above that Sweden 🇸🇪 shows the most occurrences of the word win with n = 241. While Uruguay did show a score of 450 for the term "winning", this sentiment analysis was conducted just after Uruguay had won against Saudi Arabia, so I would interpret this with caution.
Overall, there seems to be a very positive sentiment for Sweden in terms of the word "win". Could this be a sign that Sweden is about to take home the cup?
To retest this, I ran another sentiment analysis today on both Sweden and Germany ahead of Saturday's match. Here is what was generated:
Germany
Sweden
We see that the occurrences of the word "win" are still much higher for Sweden than they are for Germany. Maybe this might be a sign of the winner of Saturday's match. Only time will tell - sentiment could well change before then.
Conclusion
In any event, you have learned here:
- How to download Tweets with the twitteR library
- How to conduct text mining with tidytext
- How to generate a sentiment analysis
Many thanks for your time, and feel free to reach out if you have any questions on the above!














Leave a Reply