Did Sentiment Analysis Predict Germany Would Win Against Sweden?

A sentiment analysis of Germany and Sweden just before the World Cup match on the 23rd June reveals a very interesting insight.

To summarise, I used the twitteR library in R to download Twitter data (1000 tweets over the last ten days) based on the two countries, i.e. “Germany World Cup”, “Sweden World Cup”.

Germany

sentiment

Sweden

sentiment

You can see that before the match, Germany had a “win” occurrence of n > 500, while that of Sweden was n < 150.

Granted, Sweden had scored the first goal of the game but Germany then came back strongly and beat Sweden 2-1. Hence, we see that the word “won” was the most frequently occurring just after the match was played:

sentiment

Here is how the analysis is generated:

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 did was stream 1000 tweets since 18th June. I did this by country, so the first term is “Germany World Cup”, and the second is “Sweden World Cup”. I’m using Germany as the example below, but ran it for Sweden as well:

worldcup<-searchTwitter("germany world cup", n=1000, since='2018-06-18')
df <- twListToDF(worldcup)

As you can see, we are downloading all the relevant tweets for Germany, and saving them to a data frame titled df. The sentiment analysis is then run 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 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, these words are stored 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, we 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()

Given that the sentiment was much stronger for Germany than it was for Sweden, could this imply that making predictions using sentiment analysis just before the match is played yield greater value?

After all, sentiment for a particular team can be quite fickle and can change based on different attributes, e.g. a major player is unable to play, bad performance in a prior match leads to negative sentiment, etc.

Therefore, sentiment analysis may be more useful in predicting imminent events rather than making long-term forecasts per se.

That said, the fact that positive sentiment in the form of the word "win" increased sharply for Germany just prior to the match is quite interesting, as we were not seeing such high occurrences prior to this:

sentiment

Many of the models out there take a much more technical approach to predicting match outcomes by taking into account more technical variables such as past performance, weather, match location, etc. However, the World Cup will prove to be very interesting in seeing whether sentiment analysis actually has the ability to predict specific outcomes.

I'll be running this model before different matches in the remainder of the tournament - I'm really excited to test this further!

2 comments

  1. That’s a very practical example of sentiment analysis Michael!
    Can we do something similar from Facebook data?

Leave a Reply

Your email address will not be published. Required fields are marked *

9 − four =