library(dplyr)
library(readr)
library(tidyr)
library(rtweet)
library(writexl)
library(readxl)
library(tidytext)
library(textdata)
library(ggplot2)
library(textdata)
library(scales)
ngss_tweets <- read_xlsx("Data/ngss_tweets.xlsx")
ccss_tweets <- read_xlsx("Data/csss_tweets.xlsx")
ngss_text <- filter(ngss_tweets, lang == "en")
ngss_text <- select(ngss_text,screen_name, created_at, text)
ngss_text <- mutate(ngss_text, standards = "ngss")
ngss_text <- relocate(ngss_text, standards)
ngss_text <-
ngss_tweets %>%
filter(lang == "en") %>%
select(screen_name, created_at, text) %>%
mutate(standards = "ngss") %>%
relocate(standards)
tweets <- bind_rows(ngss_text, ccss_text)
head(tweets)
## # A tibble: 6 × 4
## standards screen_name created_at text
## <chr> <chr> <dttm> <chr>
## 1 ngss loyr2662 2021-02-27 17:33:27 "Switching gears for a bit for the…
## 2 ngss loyr2662 2021-02-20 20:02:37 "Was just introduced to the Engine…
## 3 ngss Furlow_teach 2021-02-27 17:03:23 "@IBchemmilam @chemmastercorey I’m…
## 4 ngss Furlow_teach 2021-02-27 14:41:01 "@IBchemmilam @chemmastercorey How…
## 5 ngss TdiShelton 2021-02-27 14:17:34 "I am so honored and appreciative …
## 6 ngss TdiShelton 2021-02-27 15:49:17 "Thank you @brian_womack I loved c…
tail(tweets)
## # A tibble: 6 × 4
## standards screen_name created_at text
## <chr> <chr> <dttm> <chr>
## 1 ccss JosiePaul8807 2021-02-20 00:34:53 "@SenatorHick You realize science…
## 2 ccss ctwittnc 2021-02-19 23:44:18 "@winningatmylife I’ll bet none o…
## 3 ccss the_rbeagle 2021-02-19 23:27:06 "@dmarush @electronlove @Montgome…
## 4 ccss silea 2021-02-19 23:11:21 "@LizerReal I don’t think that’s …
## 5 ccss JodyCoyote12 2021-02-19 22:58:25 "@CarlaRK3 @NedLamont Fully fund …
## 6 ccss Ryan_Hawes 2021-02-19 22:41:01 "I just got an \"explainer\" on h…
tweet_tokens <-
tweets %>%
unnest_tokens(output = word,
input = text)
tidy_tweets <-
tweet_tokens %>%
anti_join(stop_words, by = "word")
count(tidy_tweets, word, sort = T)
## # A tibble: 7,163 × 2
## word n
## <chr> <int>
## 1 common 1112
## 2 core 1109
## 3 https 623
## 4 t.co 623
## 5 math 450
## 6 ngss 224
## 7 students 141
## 8 science 140
## 9 school 128
## 10 amp 127
## # … with 7,153 more rows
filter(tweets, grepl('amp', text))
## # A tibble: 124 × 4
## standards screen_name created_at text
## <chr> <chr> <dttm> <chr>
## 1 ngss TdiShelton 2021-02-27 14:17:34 "I am so honored and appreciati…
## 2 ngss STEMTeachTools 2021-02-27 16:25:04 "Open, non-hierarchical communi…
## 3 ngss NGSSphenomena 2021-02-25 13:24:22 "Bacteria have music preference…
## 4 ngss CTSKeeley 2021-02-21 21:50:04 "Today I was thinking about the…
## 5 ngss richbacolor 2021-02-24 14:14:49 "Last chance to register for @M…
## 6 ngss MrsEatonELL 2021-02-27 06:24:09 "Were we doing the hand jive? N…
## 7 ngss STEMuClaytion 2021-02-24 14:56:19 "#WonderWednesday w/ questions …
## 8 ngss LearningUNDFTD 2021-02-24 18:13:01 "Are candies like M&Ms and …
## 9 ngss abeslo 2021-02-26 18:54:31 "#M'Kenna, whose story we share…
## 10 ngss E3Chemistry 2021-02-25 14:15:20 "Molarity & Parts Per Milli…
## # … with 114 more rows
tidy_tweets <-
tweet_tokens %>%
anti_join(stop_words, by = "word") %>%
filter(!word == "amp")
We’ve created some unnecessarily lengthy code to demonstrate some of
the steps in the tidying process. Rewrite the tokenization and removal
of stop words processes into a more compact series of commands and save
your data frame as tidy_tweets.
tweet_tokens <-
tweets %>%
unnest_tokens(output = word,
input = text)
tidy_tweets <-
tweet_tokens %>%
anti_join(stop_words, by = "word") %>%
filter(!word == "amp")
afinn <- get_sentiments("afinn")
afinn
## # A tibble: 2,477 × 2
## word value
## <chr> <dbl>
## 1 abandon -2
## 2 abandoned -2
## 3 abandons -2
## 4 abducted -2
## 5 abduction -2
## 6 abductions -2
## 7 abhor -3
## 8 abhorred -3
## 9 abhorrent -3
## 10 abhors -3
## # … with 2,467 more rows
bing <- get_sentiments("bing")
bing
## # A tibble: 6,786 × 2
## word sentiment
## <chr> <chr>
## 1 2-faces negative
## 2 abnormal negative
## 3 abolish negative
## 4 abominable negative
## 5 abominably negative
## 6 abominate negative
## 7 abomination negative
## 8 abort negative
## 9 aborted negative
## 10 aborts negative
## # … with 6,776 more rows
nrc <- get_sentiments("nrc")
nrc
## # A tibble: 13,872 × 2
## word sentiment
## <chr> <chr>
## 1 abacus trust
## 2 abandon fear
## 3 abandon negative
## 4 abandon sadness
## 5 abandoned anger
## 6 abandoned fear
## 7 abandoned negative
## 8 abandoned sadness
## 9 abandonment anger
## 10 abandonment fear
## # … with 13,862 more rows
loughran <- get_sentiments("loughran")
loughran
## # A tibble: 4,150 × 2
## word sentiment
## <chr> <chr>
## 1 abandon negative
## 2 abandoned negative
## 3 abandoning negative
## 4 abandonment negative
## 5 abandonments negative
## 6 abandons negative
## 7 abdicated negative
## 8 abdicates negative
## 9 abdicating negative
## 10 abdication negative
## # … with 4,140 more rows
These lexicons were created in two different ways: either through crowdsourcing through online platforms, or through an individual author’s work (which would then be “validated using some combination of crowdsourcing again, restaurant or movie reviews, or Twitter data” (from Text Mining with R, Chapter 2.))
We want to be careful that we are using lexicons that make sense for the text that we are trying to analyze. One of the examples that the book warns against is using a lexicon built from the works of Jane Austen to analyze contemporary social media posts, because the language would be very different between the two.
sentiment_afinn <- inner_join(tidy_tweets, afinn, by = "word")
sentiment_afinn
## # A tibble: 1,540 × 5
## standards screen_name created_at word value
## <chr> <chr> <dttm> <chr> <dbl>
## 1 ngss loyr2662 2021-02-27 17:33:27 win 4
## 2 ngss Furlow_teach 2021-02-27 17:03:23 love 3
## 3 ngss Furlow_teach 2021-02-27 17:03:23 sweet 2
## 4 ngss Furlow_teach 2021-02-27 17:03:23 significance 1
## 5 ngss TdiShelton 2021-02-27 14:17:34 honored 2
## 6 ngss TdiShelton 2021-02-27 14:17:34 opportunity 2
## 7 ngss TdiShelton 2021-02-27 14:17:34 wonderful 4
## 8 ngss TdiShelton 2021-02-27 14:17:34 powerful 2
## 9 ngss TdiShelton 2021-02-27 15:49:17 loved 3
## 10 ngss TdiShelton 2021-02-27 16:51:32 share 1
## # … with 1,530 more rows
sentiment_bing <- inner_join(tidy_tweets, bing, by = "word")
sentiment_bing
## # A tibble: 1,668 × 5
## standards screen_name created_at word sentiment
## <chr> <chr> <dttm> <chr> <chr>
## 1 ngss loyr2662 2021-02-27 17:33:27 win positive
## 2 ngss Furlow_teach 2021-02-27 17:03:23 love positive
## 3 ngss Furlow_teach 2021-02-27 17:03:23 helped positive
## 4 ngss Furlow_teach 2021-02-27 17:03:23 sweet positive
## 5 ngss Furlow_teach 2021-02-27 17:03:23 tough positive
## 6 ngss TdiShelton 2021-02-27 14:17:34 honored positive
## 7 ngss TdiShelton 2021-02-27 14:17:34 appreciative positive
## 8 ngss TdiShelton 2021-02-27 14:17:34 wonderful positive
## 9 ngss TdiShelton 2021-02-27 14:17:34 powerful positive
## 10 ngss TdiShelton 2021-02-27 15:49:17 loved positive
## # … with 1,658 more rows
sentiment_nrc data frame using the code
above.tidy_tweets and data frames with
sentiment values attached? Why did this happen?The original tidy_tweets data frame had 21,784 observations. However, each of the sentiment data frames saw a dramatic reduction in the number of observations (with the highest being the sentiment_nrc, with 7841 observations, and the lowest being the sentiment_loughran.) This happened because the sentiment lexicons are only keeping words that match its own list, so that it can assign a sentiment value to it. It filters out any words that it can’t cross-reference with the words contained in the lexicon.
ts_plot(tweets, by = "days")
ts_plot(tweets, by = "hours")
ts_plot with the group_by function to
compare the number of tweets over time by Next Gen and Common Core
standardsTwitter users are talking significantly more often about the CCSS standards than the NGSS.
summary_bing <- count(sentiment_bing, sentiment, sort = TRUE)
summary_bing
## # A tibble: 2 × 2
## sentiment n
## <chr> <int>
## 1 negative 992
## 2 positive 676
summary_bing <- sentiment_bing %>%
group_by(standards) %>%
count(sentiment)
summary_bing
## # A tibble: 4 × 3
## # Groups: standards [2]
## standards sentiment n
## <chr> <chr> <int>
## 1 ccss negative 926
## 2 ccss positive 446
## 3 ngss negative 66
## 4 ngss positive 230
summary_bing <- sentiment_bing %>%
group_by(standards) %>%
count(sentiment, sort = TRUE) %>%
spread(sentiment, n)
summary_bing
## # A tibble: 2 × 3
## # Groups: standards [2]
## standards negative positive
## <chr> <int> <int>
## 1 ccss 926 446
## 2 ngss 66 230
summary_bing <- sentiment_bing %>%
group_by(standards) %>%
count(sentiment, sort = TRUE) %>%
spread(sentiment, n) %>%
mutate(sentiment = positive - negative) %>%
mutate(lexicon = "bing") %>%
relocate(lexicon)
summary_bing
## # A tibble: 2 × 5
## # Groups: standards [2]
## lexicon standards negative positive sentiment
## <chr> <chr> <int> <int> <int>
## 1 bing ccss 926 446 -480
## 2 bing ngss 66 230 164
head(sentiment_afinn)
## # A tibble: 6 × 5
## standards screen_name created_at word value
## <chr> <chr> <dttm> <chr> <dbl>
## 1 ngss loyr2662 2021-02-27 17:33:27 win 4
## 2 ngss Furlow_teach 2021-02-27 17:03:23 love 3
## 3 ngss Furlow_teach 2021-02-27 17:03:23 sweet 2
## 4 ngss Furlow_teach 2021-02-27 17:03:23 significance 1
## 5 ngss TdiShelton 2021-02-27 14:17:34 honored 2
## 6 ngss TdiShelton 2021-02-27 14:17:34 opportunity 2
summary_afinn <- sentiment_afinn %>%
group_by(standards) %>%
summarise(sentiment = sum(value)) %>%
mutate(lexicon = "AFINN") %>%
relocate(lexicon)
summary_afinn
## # A tibble: 2 × 3
## lexicon standards sentiment
## <chr> <chr> <dbl>
## 1 AFINN ccss -808
## 2 AFINN ngss 503
For your final task for this walkthough, calculate a single sentiment
score for NGSS and CCSS using the remaining nrc and
loughan lexicons and answer the following questions. Are
these findings above still consistent?
The findings using the nrc lexicon are much different, with CCSS garnering a much more positive sentiment score (1528) than NGSS (492). However, the loughran lexicon continues the previous trend, with CCSS earning a much more negative score (-321) than NGSS (-16), although the negative score for NGSS is a departure from all the other lexicons.
nrc
## # A tibble: 13,872 × 2
## word sentiment
## <chr> <chr>
## 1 abacus trust
## 2 abandon fear
## 3 abandon negative
## 4 abandon sadness
## 5 abandoned anger
## 6 abandoned fear
## 7 abandoned negative
## 8 abandoned sadness
## 9 abandonment anger
## 10 abandonment fear
## # … with 13,862 more rows
## # A tibble: 2 × 5
## # Groups: standards [2]
## standards method negative positive sentiment
## <chr> <chr> <int> <int> <int>
## 1 ccss nrc 766 2294 1528
## 2 ngss nrc 79 571 492
## # A tibble: 2 × 5
## # Groups: standards [2]
## standards method negative positive sentiment
## <chr> <chr> <int> <int> <int>
## 1 ccss loughran 433 112 -321
## 2 ngss loughran 73 57 -16
ngss_text <-
ngss_tweets %>%
filter(lang == "en") %>%
select(status_id, text) %>%
mutate(standards = "ngss") %>%
relocate(standards)
ccss_text <-
ccss_tweets %>%
filter(lang == "en") %>%
select(status_id, text) %>%
mutate(standards = "ccss") %>%
relocate(standards)
tweets <- bind_rows(ngss_text, ccss_text)
tweets
## # A tibble: 1,441 × 3
## standards status_id text
## <chr> <chr> <chr>
## 1 ngss 1365716690336645124 "Switching gears for a bit for the \"Crosscutt…
## 2 ngss 1363217513761415171 "Was just introduced to the Engineering Habits…
## 3 ngss 1365709122763653133 "@IBchemmilam @chemmastercorey I’m familiar w/…
## 4 ngss 1365673294360420353 "@IBchemmilam @chemmastercorey How well does t…
## 5 ngss 1365667393188601857 "I am so honored and appreciative to have an o…
## 6 ngss 1365690477266284545 "Thank you @brian_womack I loved connecting wi…
## 7 ngss 1365706140496130050 "Please share #NGSSchat PLN! https://t.co/Qc2c…
## 8 ngss 1363669328147677189 "So excited about this weekend’s learning... p…
## 9 ngss 1365442786544214019 "The Educators Evaluating the Quality of Instr…
## 10 ngss 1364358149164175362 "Foster existing teacher social networks that …
## # … with 1,431 more rows
sentiment_afinn <- tweets %>%
unnest_tokens(output = word,
input = text) %>%
anti_join(stop_words, by = "word") %>%
filter(!word == "amp") %>%
inner_join(afinn, by = "word")
sentiment_afinn
## # A tibble: 1,540 × 4
## standards status_id word value
## <chr> <chr> <chr> <dbl>
## 1 ngss 1365716690336645124 win 4
## 2 ngss 1365709122763653133 love 3
## 3 ngss 1365709122763653133 sweet 2
## 4 ngss 1365709122763653133 significance 1
## 5 ngss 1365667393188601857 honored 2
## 6 ngss 1365667393188601857 opportunity 2
## 7 ngss 1365667393188601857 wonderful 4
## 8 ngss 1365667393188601857 powerful 2
## 9 ngss 1365690477266284545 loved 3
## 10 ngss 1365706140496130050 share 1
## # … with 1,530 more rows
afinn_score <- sentiment_afinn %>%
group_by(standards, status_id) %>%
summarise(value = sum(value))
afinn_score
## # A tibble: 857 × 3
## # Groups: standards [2]
## standards status_id value
## <chr> <chr> <dbl>
## 1 ccss 1362894990813188096 2
## 2 ccss 1362899370199445508 4
## 3 ccss 1362906588021989376 -2
## 4 ccss 1362910494487535618 -9
## 5 ccss 1362910913855160320 -1
## 6 ccss 1362928225379250179 2
## 7 ccss 1362933982074073090 -1
## 8 ccss 1362947497258151945 -3
## 9 ccss 1362949805694013446 3
## 10 ccss 1362970614282264583 3
## # … with 847 more rows
afinn_sentiment <- afinn_score %>%
filter(value != 0) %>%
mutate(sentiment = if_else(value < 0, "negative", "positive"))
afinn_sentiment
## # A tibble: 820 × 4
## # Groups: standards [2]
## standards status_id value sentiment
## <chr> <chr> <dbl> <chr>
## 1 ccss 1362894990813188096 2 positive
## 2 ccss 1362899370199445508 4 positive
## 3 ccss 1362906588021989376 -2 negative
## 4 ccss 1362910494487535618 -9 negative
## 5 ccss 1362910913855160320 -1 negative
## 6 ccss 1362928225379250179 2 positive
## 7 ccss 1362933982074073090 -1 negative
## 8 ccss 1362947497258151945 -3 negative
## 9 ccss 1362949805694013446 3 positive
## 10 ccss 1362970614282264583 3 positive
## # … with 810 more rows
afinn_ratio <- afinn_sentiment %>%
group_by(standards) %>%
count(sentiment) %>%
spread(sentiment, n) %>%
mutate(ratio = negative/positive)
afinn_ratio
## # A tibble: 2 × 4
## # Groups: standards [2]
## standards negative positive ratio
## <chr> <int> <int> <dbl>
## 1 ccss 421 211 2.00
## 2 ngss 21 167 0.126
afinn_counts <- afinn_sentiment %>%
group_by(standards) %>%
count(sentiment) %>%
filter(standards == "ngss")
afinn_counts %>%
ggplot(aes(x="", y=n, fill=sentiment)) +
geom_bar(width = .6, stat = "identity") +
labs(title = "Next Gen Science Standards",
subtitle = "Proportion of Positive & Negative Tweets") +
coord_polar(theta = "y") +
theme_void()
summary_afinn2 <- sentiment_afinn %>%
group_by(standards) %>%
filter(value != 0) %>%
mutate(sentiment = if_else(value < 0, "negative", "positive")) %>%
count(sentiment, sort = TRUE) %>%
mutate(method = "AFINN")
summary_bing2 <- sentiment_bing %>%
group_by(standards) %>%
count(sentiment, sort = TRUE) %>%
mutate(method = "bing")
summary_nrc2 <- sentiment_nrc %>%
filter(sentiment %in% c("positive", "negative")) %>%
group_by(standards) %>%
count(sentiment, sort = TRUE) %>%
mutate(method = "nrc")
summary_loughran2 <- sentiment_loughran %>%
filter(sentiment %in% c("positive", "negative")) %>%
group_by(standards) %>%
count(sentiment, sort = TRUE) %>%
mutate(method = "loughran")
summary_sentiment <- bind_rows(summary_afinn2,
summary_bing2,
summary_nrc2,
summary_loughran2) %>%
arrange(method, standards) %>%
relocate(method)
summary_sentiment
## # A tibble: 16 × 4
## # Groups: standards [2]
## method standards sentiment n
## <chr> <chr> <chr> <int>
## 1 AFINN ccss negative 740
## 2 AFINN ccss positive 477
## 3 AFINN ngss positive 278
## 4 AFINN ngss negative 45
## 5 bing ccss negative 926
## 6 bing ccss positive 446
## 7 bing ngss positive 230
## 8 bing ngss negative 66
## 9 loughran ccss negative 433
## 10 loughran ccss positive 112
## 11 loughran ngss negative 73
## 12 loughran ngss positive 57
## 13 nrc ccss positive 2294
## 14 nrc ccss negative 766
## 15 nrc ngss positive 571
## 16 nrc ngss negative 79
total_counts <- summary_sentiment %>%
group_by(method, standards) %>%
summarise(total = sum(n))
## `summarise()` has grouped output by 'method'. You can override using the
## `.groups` argument.
sentiment_counts <- left_join(summary_sentiment, total_counts)
## Joining, by = c("method", "standards")
sentiment_counts
## # A tibble: 16 × 5
## # Groups: standards [2]
## method standards sentiment n total
## <chr> <chr> <chr> <int> <int>
## 1 AFINN ccss negative 740 1217
## 2 AFINN ccss positive 477 1217
## 3 AFINN ngss positive 278 323
## 4 AFINN ngss negative 45 323
## 5 bing ccss negative 926 1372
## 6 bing ccss positive 446 1372
## 7 bing ngss positive 230 296
## 8 bing ngss negative 66 296
## 9 loughran ccss negative 433 545
## 10 loughran ccss positive 112 545
## 11 loughran ngss negative 73 130
## 12 loughran ngss positive 57 130
## 13 nrc ccss positive 2294 3060
## 14 nrc ccss negative 766 3060
## 15 nrc ngss positive 571 650
## 16 nrc ngss negative 79 650
sentiment_percents <- sentiment_counts %>%
mutate(percent = n/total * 100)
sentiment_percents
## # A tibble: 16 × 6
## # Groups: standards [2]
## method standards sentiment n total percent
## <chr> <chr> <chr> <int> <int> <dbl>
## 1 AFINN ccss negative 740 1217 60.8
## 2 AFINN ccss positive 477 1217 39.2
## 3 AFINN ngss positive 278 323 86.1
## 4 AFINN ngss negative 45 323 13.9
## 5 bing ccss negative 926 1372 67.5
## 6 bing ccss positive 446 1372 32.5
## 7 bing ngss positive 230 296 77.7
## 8 bing ngss negative 66 296 22.3
## 9 loughran ccss negative 433 545 79.4
## 10 loughran ccss positive 112 545 20.6
## 11 loughran ngss negative 73 130 56.2
## 12 loughran ngss positive 57 130 43.8
## 13 nrc ccss positive 2294 3060 75.0
## 14 nrc ccss negative 766 3060 25.0
## 15 nrc ngss positive 571 650 87.8
## 16 nrc ngss negative 79 650 12.2
sentiment_percents %>%
ggplot(aes(x = standards, y = percent, fill=sentiment)) +
geom_bar(width = .8, stat = "identity") +
facet_wrap(~method, ncol = 1) +
coord_flip() +
labs(title = "Public Sentiment on Twitter",
subtitle = "The Common Core & Next Gen Science Standards",
x = "State Standards",
y = "Percentage of Words")