Sentiment Analysis of ChatGTP Posts on Twitter

Get tweets

Sentiment Analysis

# View the tweets
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   1.0.0 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.5.0 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
tweets <- read.csv("tweets-chatGPT.csv")
glimpse(tweets)
## Rows: 1,000
## Columns: 90
## $ user_id                 <chr> "x2606887044", "x1349018869495689217", "x14212…
## $ status_id               <chr> "x1617606563069366278", "x1617606539996721152"…
## $ created_at              <chr> "2023-01-23 19:33:52", "2023-01-23 19:33:47", …
## $ screen_name             <chr> "alrlodallas", "cruptogeorge", "yesthatkarim",…
## $ text                    <chr> "Pretty absurd that chatGPT has passed the Uni…
## $ source                  <chr> "Twitter for iPhone", "Twitter Web App", "Twit…
## $ display_text_width      <int> 140, 140, 304, 116, 140, 140, 140, 140, 140, 1…
## $ reply_to_status_id      <chr> "", "", "x1617597127588474880", "", "", "", ""…
## $ reply_to_user_id        <chr> "", "", "x1113541", "", "", "", "", "", "", ""…
## $ reply_to_screen_name    <chr> "", "", "bruces", "", "", "", "", "", "", "", …
## $ is_quote                <lgl> FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE…
## $ is_retweet              <lgl> TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRU…
## $ favorite_count          <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ retweet_count           <int> 2371, 137, 0, 19, 1, 2, 940, 16, 1, 1554, 0, 0…
## $ quote_count             <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ reply_count             <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ hashtags                <chr> "", "", "OffByOne DidYouNeedThatFinger", "", "…
## $ symbols                 <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ urls_url                <chr> "", "", "twitter.com/yesthatkarim/s…", "dlvr.i…
## $ urls_t.co               <chr> "", "", "https://t.co/MLRfK2z6x9", "https://t.…
## $ urls_expanded_url       <chr> "", "", "https://twitter.com/yesthatkarim/stat…
## $ media_url               <chr> "", "", "http://pbs.twimg.com/media/FnLkkeeWYA…
## $ media_t.co              <chr> "", "", "https://t.co/7swTGo3dc7", "https://t.…
## $ media_expanded_url      <chr> "", "", "https://twitter.com/yesthatkarim/stat…
## $ media_type              <chr> "", "", "photo", "photo", "", "", "", "", "", …
## $ ext_media_url           <chr> "", "", "http://pbs.twimg.com/media/FnLkkeeWYA…
## $ ext_media_t.co          <chr> "", "", "https://t.co/7swTGo3dc7", "https://t.…
## $ ext_media_expanded_url  <chr> "", "", "https://twitter.com/yesthatkarim/stat…
## $ ext_media_type          <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ mentions_user_id        <chr> "x411219969", "x1335132884278108161", "x111354…
## $ mentions_screen_name    <chr> "noor_siddiqui_", "stats_feed", "bruces", "Giz…
## $ lang                    <chr> "en", "en", "en", "en", "en", "en", "en", "en"…
## $ quoted_status_id        <chr> "", "", "x1602902711883530240", "", "", "", ""…
## $ quoted_text             <chr> "", "", "this AI talks like a… politician 😱\n…
## $ quoted_created_at       <chr> "", "", "2022-12-14 05:46:01", "", "", "", "",…
## $ quoted_source           <chr> "", "", "Twitter for iPhone", "", "", "", "", …
## $ quoted_favorite_count   <int> NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ quoted_retweet_count    <int> NA, NA, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ quoted_user_id          <chr> "", "", "x14212319", "", "", "", "", "", "", "…
## $ quoted_screen_name      <chr> "", "", "yesthatkarim", "", "", "", "", "", ""…
## $ quoted_name             <chr> "", "", "такцеkарім 🐀", "", "", "", "", "", "…
## $ quoted_followers_count  <int> NA, NA, 619, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ quoted_friends_count    <int> NA, NA, 718, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ quoted_statuses_count   <int> NA, NA, 49082, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ quoted_location         <chr> "", "", "Mega-City One", "", "", "", "", "", "…
## $ quoted_description      <chr> "", "", "♫ I used to be amused, now I try to b…
## $ quoted_verified         <lgl> NA, NA, FALSE, NA, NA, NA, NA, NA, NA, NA, NA,…
## $ retweet_status_id       <chr> "x1617194845810077697", "x1617594546631606307"…
## $ retweet_text            <chr> "Pretty absurd that chatGPT has passed the Uni…
## $ retweet_created_at      <chr> "2023-01-22 16:17:51", "2023-01-23 18:46:08", …
## $ retweet_source          <chr> "Twitter Web App", "Twitter for iPhone", "", "…
## $ retweet_favorite_count  <int> 11364, 821, NA, 27, 4, 3, 8356, 213, 4, 9184, …
## $ retweet_retweet_count   <int> 2371, 137, NA, 19, 1, 2, 940, 16, 1, 1554, NA,…
## $ retweet_user_id         <chr> "x411219969", "x1335132884278108161", "", "x28…
## $ retweet_screen_name     <chr> "noor_siddiqui_", "stats_feed", "", "Gizmodo",…
## $ retweet_name            <chr> "Noor Siddiqui", "World of Statistics", "", "G…
## $ retweet_followers_count <int> 3995, 1378742, NA, 2776708, 22993, 973, 18815,…
## $ retweet_friends_count   <int> 850, 12, NA, 162, 513, 228, 2355, 3166, 3097, …
## $ retweet_statuses_count  <int> 1482, 2129, NA, 177669, 9151, 1845, 50692, 183…
## $ retweet_location        <chr> "San Francisco, CA", "Los Angeles, CA", "", ""…
## $ retweet_description     <chr> "Helping everyone have a healthy baby @OrchidI…
## $ retweet_verified        <lgl> FALSE, FALSE, NA, TRUE, FALSE, FALSE, FALSE, F…
## $ place_url               <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ place_name              <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ place_full_name         <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ place_type              <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ country                 <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ country_code            <chr> "", "", "", "", "", "", "", "", "", "", "", ""…
## $ geo_coords              <chr> "NA NA", "NA NA", "NA NA", "NA NA", "NA NA", "…
## $ coords_coords           <chr> "NA NA", "NA NA", "NA NA", "NA NA", "NA NA", "…
## $ bbox_coords             <chr> "NA NA NA NA NA NA NA NA", "NA NA NA NA NA NA …
## $ status_url              <chr> "https://twitter.com/alrlodallas/status/161760…
## $ name                    <chr> "al", "georgeadams.eth", "такцеkарім 🐀", "Whe…
## $ location                <chr> "Dallas, TX", "", "Mega-City One", "SPACE!!!",…
## $ description             <chr> "Playing with data. Sailin’ da south.", "#Bitc…
## $ url                     <chr> "", "", "https://t.co/pTQTYwxwS7", "", "", "",…
## $ protected               <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS…
## $ followers_count         <int> 555, 645, 619, 130, 12, 236, 236, 2073, 5464, …
## $ friends_count           <int> 1240, 815, 718, 27, 171, 720, 192, 2194, 1871,…
## $ listed_count            <int> 3, 10, 16, 9, 0, 14, 3, 163, 107, 0, 5, 5, 1, …
## $ statuses_count          <int> 13620, 24424, 49082, 25080, 976, 11100, 18693,…
## $ favourites_count        <int> 5503, 43244, 158323, 2337, 1924, 4415, 44009, …
## $ account_created_at      <chr> "2014-07-06 06:32:10", "2021-01-12 15:42:59", …
## $ verified                <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALS…
## $ profile_url             <chr> "", "", "https://t.co/pTQTYwxwS7", "", "", "",…
## $ profile_expanded_url    <chr> "", "", "http://about.me/yesthatkarim", "", ""…
## $ account_lang            <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
## $ profile_banner_url      <chr> "https://pbs.twimg.com/profile_banners/2606887…
## $ profile_background_url  <chr> "http://abs.twimg.com/images/themes/theme1/bg.…
## $ profile_image_url       <chr> "http://pbs.twimg.com/profile_images/160602785…
#print(tweets$text) 

#sentiment analysis
library(syuzhet)

# Perform sentiment analysis on the tweets
sentiment_scores <- get_nrc_sentiment(tweets$text)
## Warning: `spread_()` was deprecated in tidyr 1.2.0.
## ℹ Please use `spread()` instead.
## ℹ The deprecated feature was likely used in the syuzhet package.
##   Please report the issue to the authors.
# View the sentiment scores
#print(sentiment_scores)

# Convert sentiment scores to a data frame

library(tidyverse)
sentiment_df <- data.frame(sentiment_scores)
glimpse(sentiment_df)
## Rows: 1,000
## Columns: 10
## $ anger        <dbl> 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, …
## $ anticipation <dbl> 2, 0, 0, 2, 0, 0, 1, 0, 0, 1, 0, 1, 0, 2, 1, 0, 0, 0, 1, …
## $ disgust      <dbl> 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, …
## $ fear         <dbl> 2, 0, 0, 0, 0, 0, 0, 3, 1, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, …
## $ joy          <dbl> 1, 0, 0, 2, 0, 0, 2, 0, 0, 1, 0, 0, 1, 2, 1, 0, 0, 0, 1, …
## $ sadness      <dbl> 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, …
## $ surprise     <dbl> 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, …
## $ trust        <dbl> 6, 0, 0, 2, 0, 0, 3, 1, 1, 8, 0, 0, 1, 1, 1, 0, 0, 0, 1, …
## $ negative     <dbl> 3, 0, 1, 0, 0, 2, 1, 3, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, …
## $ positive     <dbl> 3, 0, 0, 2, 1, 3, 2, 0, 1, 9, 1, 0, 2, 3, 1, 0, 0, 0, 1, …
long <- pivot_longer(data = sentiment_df , 
                     cols = anger:positive, 
                     names_to = c("sentiment"),
                     values_to = "sentiment_scores")

# Plot the sentiment scores
ggplot(data = long, aes(x = sentiment, y = sentiment_scores, fill=as.factor(sentiment))) + 
  geom_bar(stat = "identity") + 
  ggtitle("Sentiment Analysis of Collected Tweets") + 
  xlab("Sentiment") + 
  ylab("Count") +
  coord_flip() + 
  #scale_fill_brewer(palette = "Set1") +
  theme_minimal() +
  guides(fill = "none")
#Plot distribution
ggplot(long, aes(x = sentiment, y= sentiment_scores, fill = sentiment)) + 
  geom_violin() +
  geom_boxplot(width = .2, fatten = NULL, position = position_dodge(.9)) +
  stat_summary(fun = "mean", geom = "point", position = position_dodge(.9)) +
  stat_summary(fun.data = "mean_se", geom = "errorbar", width = .1, position = position_dodge(.9)) +
  ggtitle("Distribution of Sentiment Scores of Collected Tweets") + 
  xlab("Sentiment") + 
  ylab("Sentiment scores") +
  coord_flip() + 
  #scale_fill_brewer(palette = "Set1") +
  theme_minimal() +
  guides(fill = "none")
## Warning: Removed 1 rows containing missing values (`geom_segment()`).
## Warning: Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
## Removed 1 rows containing missing values (`geom_segment()`).
Bernard Enjolras
Research Professor at The Institute for Social research in Oslo and the Director of the Center for Research on Civil Society and Voluntary Sector.

My research interests include volunteering, voluntary organizations, governance, social capital and trust, civic engagement as well as the digitization of the public sphere, social media and freedom of expression.