Sentiment Analysis of Twitter Data

2019-04-02

Sentiment Analysis of Twitter Data (saotd)

Twitter Introduction

Recent years have witnessed the rapid growth of social media platforms in which users can publish their individual thoughts and opinions (e.g., Facebook, Twitter, Google+ and several blogs). The rise in popularity of social media has changed the world wide web from a static repository to a dynamic forum for anyone to voice their opinion across the globe. This new dimension of User Generated Content opens up a new and dynamic source of insight to individuals, organizations and governments.

Social network sites or platforms, are defined as web-based services that allow individuals to:

The nature and nomenclature of these connections may vary from site to site.

This package, saotd is focused on utilizing Twitter data due to its widespread global acceptance. Harvested data, analyzed for sentiment can provide powerful insight into a population. This insight can assist organizations, by letting them better understand their target population. This package will allow a user to acquire data using the Public Twitter Application Programming Interface (API), to obtain tweets.

The saotd package is broken down into five different phases:

Packages

Acquire

To explore the data manipulation functions of saotd we will use the built in dataset saotd::raw_tweets.

However is you want to acquire your own tweets, you will first have to:

  1. Create a twitter account or sign into existing account.

  2. Use your twitter login, to sign into Twitter Developers

  3. Navigate to My Applications.

  4. Fill out the new application form.
  5. Create access token.
    • Record twitter access keys and tokens

With these steps complete you now have access to the twitter API.

To acquire your own dataset of tweets you can use the saotd::tweet_acquire function and insert your consumer key, consumer secret key, access token and access secret key gained from the Twitter Developers page. You will also need to select the #hashtags you are interested in and the numer of tweets requested per #hashtag.

Explore

You can acquire your own data or use the dataset included with the package. We will be using the included data raw_tweets. This dataset was acquired from a Twitter US Airline Sentiment Kaggle competition, from December 2017. The dataset contains 14,487 tweets from 6 different hashtags (2,604 x #American, 2,220 x #Delta, 2,420 x #Southwest, 3,822 x #United, 2,913 x #US Airways, 504 x #Virgin America).

The second tweet of the dataset is: @united still waiting for a reply, and when it is cleaned and tidy’d it becomes:

x
beat
bush
youre
flight
cancelled
flightled

The cleaning process removes: “@”, “#” and “RT” symbols, Weblinks, Punctuation, Emojis, and Stop Words like (“the”, “of”, etc.).

We will now investigate Uni-Grams, Bi-Grams and Tri-Grams.

Twitter data Uni-Grams
word n
united 1418
flight 1356
usairways 1065
americanair 982
southwestair 888
jetblue 781
cancelled 400
service 327
im 267
time 266
Twitter data Bi-Grams
word1 word2 n
cancelled flightled 188
customer service 171
late flight 95
cancelled flighted 84
cancelled flight 66
late flightr 55
2 hours 48
usairways americanair 35
flight cancelled 32
2 hrs 28
flight booking 28
united flight 28
Twitter data Tri-Grams
word1 word2 word3 n
NA NA NA 47
flight cancelled flightled 20
cancelled flightled flight 15
hours late flightr 15
worst customer service 13
hours late flight 10
cancelled flighted flight 8
hour late flight 8
cancelled flight flight 6
cancelled flighted flights 6
terrible customer service 6

Now that we have the Uni-Grams we can see that cancelled and fligt are referring to cancelled flight and may be good set of words to merge into a single term. Additionally, pet and pets could also be merged to observe more uniqueness in the data.

Now that the terms have been merged, the new N-Grams are re-computed.

Twitter data Uni-Grams
word n
united 1418
flight 1290
usairways 1065
americanair 982
southwestair 888
jetblue 781
service 327
im 267
time 266
hours 258
Twitter data Bi-Grams
word1 word2 n
customer service 171
late flight 95
late flightr 55
2 hours 48
usairways americanair 35
2 hrs 28
flight booking 28
united flight 28
3 hours 27
hours late 25
Twitter data Tri-Grams
word1 word2 word3 n
NA NA NA 47
hours late flightr 15
worst customer service 13
hours late flight 10
hour late flight 8
terrible customer service 6
customer service rep 5
jetblues ceo battles 5
customer service line 4
hold 3 hours 4
horrible customer service 4
poor customer service 4
southwestair lovedragonss lauren 4
street waterbury republican 4
wall street waterbury 4
waterbury republican american 4

Now we can look at Bi-Gram Networks.

Additionally we can observe the Correlation Network.

Sentiment Calculation

Now that the data has been explored we will need to compute the Sentiment scores for the hashtags.

With the scores computed we can then observe the positive and negative words within the dataset.

As an example we can see that the negative term “fail” is dwarfing all other responses. If we would like to remove “fail” we can easily do it.

We can see the most positive tweets hashtags within the the data set.

## # A tibble: 6 x 10
## # Groups:   text, method, hashtag, created, key [6]
##   text  method hashtag created key   negative positive TweetSentimentS…
##   <chr> <chr>  <chr>   <chr>   <chr>    <dbl>    <dbl>            <dbl>
## 1 @uni… Bing   United  2015-0… kevi…        0        5                5
## 2 @Ame… Bing   Americ… 2015-0… wooq…        0        4                4
## 3 @USA… Bing   US Air… 2015-0… NoDo…        0        4                4
## 4 .@Je… Bing   Delta   2015-0… shar…        0        3                3
## 5 .@Je… Bing   Delta   2015-0… unve…        0        3                3
## 6 @Ame… Bing   Americ… 2015-0… kath…        0        3                3
## # … with 2 more variables: TweetSentiment <chr>, date <date>

We can also see the most negative hashtag tweets within the data set.

## # A tibble: 6 x 10
## # Groups:   text, method, hashtag, created, key [6]
##   text  method hashtag created key   negative positive TweetSentimentS…
##   <chr> <chr>  <chr>   <chr>   <chr>    <dbl>    <dbl>            <dbl>
## 1 @USA… Bing   US Air… 2015-0… Mega…        6        0               -6
## 2 @uni… Bing   United  2015-0… czam…        6        0               -6
## 3 @Jet… Bing   Delta   2015-0… alex…        5        0               -5
## 4 @Sou… Bing   Southw… 2015-0… port…        5        0               -5
## 5 @USA… Bing   US Air… 2015-0… Joan…        5        0               -5
## 6 @Ame… Bing   Americ… 2015-0… m_jl…        4        0               -4
## # … with 2 more variables: TweetSentiment <chr>, date <date>

Furthermore if we wanted to observe the most positive or negative hashtag scores associated with a specific hashtag we could also do that.

## # A tibble: 0 x 10
## # Groups:   text, method, hashtag, created, key [0]
## # … with 10 variables: text <chr>, method <chr>, hashtag <chr>,
## #   created <chr>, key <chr>, negative <dbl>, positive <dbl>,
## #   TweetSentimentScore <dbl>, TweetSentiment <chr>, date <date>

Topic Analysis

If we were interested in conducting a topic analysis on the tweets we would then determine the number of latent topics within the tweet data.

The number of topics plot shows that between 5 and 7 latent topics reside within the dataset. For this example we could select between 5 and 7 topics to categorize this data. In this case 5 topics will be selected to continue the analysis.

In a markdown product the topics table does not print clearly, unlike when it is printed in the console. However the words associated with each topic can be observed in the below table.

Number Topic 1 Topic 2 Topic 3 Topic 4 Topic 5
1 united usairways americanair southwestair flight
2 service time usairways jetblue cancelled
3 customer plane amp im hours
4 dont gate hold virginamerica flights
5 bag jetblue call guys 2
6 check hour phone fly delayed
7 luggage waiting wait airline flightled
8 dm delay ive flying late
9 lost people cange seat 3
10 worst minutes day love weather

One of the challenges of using a topic model is selecting the correct number of topics. As we can see in the above chart. We went from 6 hashtags to 5 different topics.

While this may not be the best example to use, we will continue the topic modeling example. We would first want to rename the topics into something that would make sense. In this case Topic 1 could be luggage, Topic 2 could be delay, Topic 3 could be customer_service, Topic 4 could be enjoy, and Topic 5 could be delay These topics were chosen by observing the words associated with each topic. This selection could be different depending on experience and a deeper understanding of the topics.

We would then want to rename the topics in the dataframe

Next we would want to tidy and then score the new topic dataset.

We can see the most positive topic tweets within the data set.

## # A tibble: 6 x 10
## # Groups:   text, method, Topic, created, key [6]
##   text  method Topic created key   negative positive TweetSentimentS…
##   <chr> <chr>  <chr> <chr>   <chr>    <dbl>    <dbl>            <dbl>
## 1 @uni… Bing   enjoy 2015-0… kevi…        0        5                5
## 2 @Ame… Bing   othe… 2015-0… wooq…        0        4                4
## 3 @USA… Bing   enjoy 2015-0… NoDo…        0        4                4
## 4 .@Je… Bing   cust… 2015-0… shar…        0        3                3
## 5 .@Je… Bing   cust… 2015-0… unve…        0        3                3
## 6 @Ame… Bing   lugg… 2015-0… kath…        0        3                3
## # … with 2 more variables: TweetSentiment <chr>, date <date>

We can also see the most negative topics tweets within the data set.

## # A tibble: 6 x 10
## # Groups:   text, method, Topic, created, key [6]
##   text  method Topic created key   negative positive TweetSentimentS…
##   <chr> <chr>  <chr> <chr>   <chr>    <dbl>    <dbl>            <dbl>
## 1 @USA… Bing   gate… 2015-0… Mega…        6        0               -6
## 2 @uni… Bing   lugg… 2015-0… czam…        6        0               -6
## 3 @Jet… Bing   lugg… 2015-0… alex…        5        0               -5
## 4 @Sou… Bing   lugg… 2015-0… port…        5        0               -5
## 5 @USA… Bing   gate… 2015-0… Joan…        5        0               -5
## 6 @Ame… Bing   othe… 2015-0… m_jl…        4        0               -4
## # … with 2 more variables: TweetSentiment <chr>, date <date>

Furthermore if we wanted to observe the most positive or negative scores associated with a specific topic we could also do that.

## # A tibble: 0 x 10
## # Groups:   text, method, Topic, created, key [0]
## # … with 10 variables: text <chr>, method <chr>, Topic <chr>,
## #   created <chr>, key <chr>, negative <dbl>, positive <dbl>,
## #   TweetSentimentScore <dbl>, TweetSentiment <chr>, date <date>

Visualizations

Hashtags

Now we will begin visualizing the hashtag data. The distribution of the sentiment scores can be found in the below plot.

Additionally if we wanted to see the score distributions per each hashtag, we can find it below.

## Warning: Ignoring unknown parameters: binwidth, bins, pad

We can also observe the hashtag distributions as a Box plot.

Also as a Violin plot. The chevrons in each violin plot denote the median of the data and provide a quick reference point to see if a hashtag is generally positive or negative. For example the “random” hashtag has a generally negative sentiment, where as the “kitten” hashtags has a generally positive sentiment.

One of the more interesting ways to visualize the Twitter data is to observe the change in sentiment over time. This dataset was acquired on a single day and therefore some of the hashtags did not overlap days. However some did and we can see the change in sentiment scores through time.

Finally if a Twitter user has not disabled georeferencing data the location of the tweet can be observed. However in many cases this may not be very insightful because of the lack of data.

Topics

Now we will begin visualizing the topic data. The distribution of the sentiment scores can be found in the below plot.

Additionally if we wanted to see the score distributions per each topic, we can find it below.

## Warning: Ignoring unknown parameters: binwidth, bins, pad

We can also observe the topic distributions as a Box plot.

Also as a Violin plot. The chevrons in each violin plot denote the median of the data and provide a quick reference point to see if a hashtag is generally positive or negative. For example the “random” hashtag has a generally negative sentiment, where as the “kitten” hashtags has a generally positive sentiment.