Twitter sentiment analysis

Analyzing Social Media Data in R

Vivek Vijayaraghavan

Data Science Coach

Lesson Overview

  • What is sentiment analysis?
  • Perform sentiment analysis on tweets
  • Interpret to understand people's feelings and opinions
Analyzing Social Media Data in R

Sentiment analysis

  • Retrieve information on perception of a product or brand
  • Extract and quantify positive, negative and neutral opinions
  • Emotions like trust, joy, and anger from the text

Sentiment analysis emotions

Analyzing Social Media Data in R

Significance of sentiment analysis

  • Customer perceptions influence purchasing decisions
  • Helps understand the pulse of what customers feel
  • Proactive approach to listen to the customer and engage directly
Analyzing Social Media Data in R

How sentiment analysis works

  • Pre-defined sentiment libraries to calculate scores
  • Trained and scored based on meaning or intent of words
  • Each word is scored based on its nearness to a positive or negative word
  • Same concept is extended to words expressing specific emotions
Analyzing Social Media Data in R

Sentiment analysis steps

Step1 in sentiment analysis

Analyzing Social Media Data in R

Sentiment analysis steps

Step2 in sentiment analysis

Analyzing Social Media Data in R

Sentiment analysis steps

Step3 in sentiment analysis

Analyzing Social Media Data in R

Sentiment analysis steps

Step4 in sentiment analysis

Analyzing Social Media Data in R

Extract tweets for sentiment analysis

# Extract tweets on galaxy fold
twts_galxy  <-  search_tweets("galaxy fold", n = 5000, 
                                lang = "en", include_rts = FALSE)
Analyzing Social Media Data in R

Perform sentiment analysis

# Perform sentiment analysis for tweets on galaxy fold
library(syuzhet)
sa.value <- get_nrc_sentiment(twts_galxy$text)
Analyzing Social Media Data in R

View sentiment scores

# View the sentiment scores
sa.value[1:5,1:7]
anger    anticipation    disgust    fear    joy    sadness    surprise
<dbl>       <dbl>         <dbl>     <dbl>  <dbl>    <dbl>       <dbl> 
0             0             0         0      0        0           0
1             0             0         0      0        0           0
1             1             0         2      1        1           1
0             0             0         1      0        0           0
0             0             0         0      0        0           0
Analyzing Social Media Data in R

Sum of sentiment scores

# Calculate sum of sentiment scores
score <- colSums(sa.value[,])
Analyzing Social Media Data in R

Data frame of sentiment scores

# Convert to data frame
score_df <- data.frame(score)
# View the data frame
score_df
               score
               <dbl>
anger           211            
anticipation    825            
disgust         214            
fear            253            
joy             412            
sadness         197            
surprise        315            
trust           641            
negative        487            
positive       1351
Analyzing Social Media Data in R

Data frame of sentiment scores

# Convert row names into 'sentiment' column
# Combine with sentiment scores
sa.score <- cbind(sentiment = row.names(score_df), 
                  score_df, row.names=NULL)
Analyzing Social Media Data in R

Data frame of sentiment scores

# View data frame with sentiment scores
print(sa.score)
sentiment      score
<fct>         <dbl>
anger           211            
anticipation    825            
disgust         214            
fear            253            
joy             412            
sadness         197            
surprise        315            
trust           641            
negative        487            
positive        1351    
Analyzing Social Media Data in R

Plot and visualize sentiments

  • Plot and visualize sentiments using ggplot()
# Plot the sentiment scores
ggplot(data = sa.score2, aes(x = sentiment, y = score, 
       fill = sentiment)) +
       geom_bar(stat = "identity") +
       theme(axis.text.x = element_text(angle = 45, hjust = 1))
Analyzing Social Media Data in R

Visualize the sentiments

Visualize the sentiments

Analyzing Social Media Data in R

Let's practice!

Analyzing Social Media Data in R

Preparing Video For Download...