Overview

NGSS stands for Next Generation Science Standards, which are long-standing standards-based educational reform efforts. We investigated the sentiments of multiple stakeholders towards them. To understand how the public feels about this reform, collect and extract opinion data from the Twitter API and use text mining and sentiment analysis techniques to evaluate the various sentiments in California. There are three major questions we want to investigate: 1. What is the public sentiment expressed toward the NGSS? 2. Compared with CCSS, what’s the difference between NGSS? 3. How does sentiment for the California public?

Procedure

message=FALSE
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readr)
## Warning: package 'readr' was built under R version 4.3.2
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.2
library(rtweet)
## Warning: package 'rtweet' was built under R version 4.3.2
library(writexl)
## Warning: package 'writexl' was built under R version 4.3.2
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.2
library(tidytext)
## Warning: package 'tidytext' was built under R version 4.3.2
library(textdata)
## Warning: package 'textdata' was built under R version 4.3.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.2
library(textdata)
library(scales)
## Warning: package 'scales' was built under R version 4.3.2
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
ngss_tweets <- read_xlsx("~/588/data/ngss_tweets.xlsx") 
ccss_tweets <- read_xlsx("~/588/data/ccss_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 <- select(ngss_text, standards, screen_name, created_at, text)

ngss_text <-
  ngss_tweets %>%
  filter(lang == "en") %>%
  select(screen_name, created_at, text,location) %>%
  mutate(standards = "ngss") %>%
  relocate(standards)
echo=FALSE
ccss_text <-
  ccss_tweets %>%
  filter(lang == "en") %>%
  select(screen_name, created_at, text,location) %>%
  mutate(standards = "ccss") %>%
  relocate(standards)


tweets <- bind_rows(ngss_text, ccss_text)
print(tweets)
## # A tibble: 1,441 × 5
##    standards screen_name    created_at          text                    location
##    <chr>     <chr>          <dttm>              <chr>                   <chr>   
##  1 ngss      loyr2662       2021-02-27 17:33:27 "Switching gears for a… Philade…
##  2 ngss      loyr2662       2021-02-20 20:02:37 "Was just introduced t… Philade…
##  3 ngss      Furlow_teach   2021-02-27 17:03:23 "@IBchemmilam @chemmas… Michigan
##  4 ngss      Furlow_teach   2021-02-27 14:41:01 "@IBchemmilam @chemmas… Michigan
##  5 ngss      TdiShelton     2021-02-27 14:17:34 "I am so honored and a… Kentuck…
##  6 ngss      TdiShelton     2021-02-27 15:49:17 "Thank you @brian_woma… Kentuck…
##  7 ngss      TdiShelton     2021-02-27 16:51:32 "Please share #NGSScha… Kentuck…
##  8 ngss      TdiShelton     2021-02-22 01:57:58 "So excited about this… Kentuck…
##  9 ngss      STEMTeachTools 2021-02-26 23:25:03 "The Educators Evaluat… <NA>    
## 10 ngss      STEMTeachTools 2021-02-23 23:35:05 "Foster existing teach… <NA>    
## # ℹ 1,431 more rows
filtered_tweets<-tweets[grepl("CA|Carlifornia",tweets$location),]
#head(tweets)
#tail(tweets)
print(filtered_tweets)
## # A tibble: 46 × 5
##    standards screen_name     created_at          text                   location
##    <chr>     <chr>           <dttm>              <chr>                  <chr>   
##  1 ngss      rjmama          2021-02-26 17:56:00 "@SRVUSD_NGSS @Melise… Ohlone …
##  2 ngss      flabellinaJG    2021-02-24 19:53:07 "So proud of our @K12… Long Be…
##  3 ngss      flabellinaJG    2021-02-26 03:50:06 "Congrats to this mig… Long Be…
##  4 ngss      DrDSteele_PhD   2021-02-25 23:46:47 "I have 2 days (3 hrs… Los Ang…
##  5 ngss      DrDSteele_PhD   2021-02-26 00:58:39 "@KRenaeP @dtcampbe @… Los Ang…
##  6 ngss      KidsMakingSense 2021-02-25 21:00:00 "Our new #KidsMakingS… Petalum…
##  7 ngss      RocketLitReads  2021-02-24 23:45:11 "It's share time! Wha… Los Ang…
##  8 ngss      the_fish_nerd   2021-02-24 22:49:58 "We had \"art class\"… Winches…
##  9 ngss      InnerOrbitedu   2021-02-24 20:02:04 "What IS Elephant Too… Los Ang…
## 10 ngss      casciencecenter 2021-02-24 20:01:14 "\U0001f514 ATTN: Tea… Los Ang…
## # ℹ 36 more rows
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
## # ℹ 7,153 more rows
filter(tweets, grepl('amp', text))
## # A tibble: 124 × 5
##    standards screen_name    created_at          text                    location
##    <chr>     <chr>          <dttm>              <chr>                   <chr>   
##  1 ngss      TdiShelton     2021-02-27 14:17:34 "I am so honored and a… Kentuck…
##  2 ngss      STEMTeachTools 2021-02-27 16:25:04 "Open, non-hierarchica… <NA>    
##  3 ngss      NGSSphenomena  2021-02-25 13:24:22 "Bacteria have music p… <NA>    
##  4 ngss      CTSKeeley      2021-02-21 21:50:04 "Today I was thinking … Maine a…
##  5 ngss      richbacolor    2021-02-24 14:14:49 "Last chance to regist… Ann Arb…
##  6 ngss      MrsEatonELL    2021-02-27 06:24:09 "Were we doing the han… <NA>    
##  7 ngss      STEMuClaytion  2021-02-24 14:56:19 "#WonderWednesday w/ q… Olean, …
##  8 ngss      LearningUNDFTD 2021-02-24 18:13:01 "Are candies like M&am… Gaither…
##  9 ngss      abeslo         2021-02-26 18:54:31 "#M'Kenna, whose story… Philade…
## 10 ngss      E3Chemistry    2021-02-25 14:15:20 "Molarity &amp; Parts … excite …
## # ℹ 114 more rows
tidy_tweets <-
  tweet_tokens %>%
  anti_join(stop_words, by = "word") %>%
  filter(!word == "amp") %>%
  filter(!word == "t3ic") %>%
  filter(!word == "https")

afinn <- get_sentiments("afinn")

bing <- get_sentiments("bing")

nrc <- get_sentiments("nrc")

loughran <- get_sentiments("loughran")

sentiment_afinn <- inner_join(tidy_tweets, afinn, by = "word")

sentiment_bing <- inner_join(tidy_tweets, bing, by = "word")
#`echo=F, results=F`
sentiment_nrc <- inner_join(tidy_tweets, nrc, by = "word")
## Warning in inner_join(tidy_tweets, nrc, by = "word"): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 24 of `x` matches multiple rows in `y`.
## ℹ Row 7509 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
sentiment_loughran <- inner_join(tidy_tweets, loughran, by = "word")
## Warning in inner_join(tidy_tweets, loughran, by = "word"): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 2297 of `x` matches multiple rows in `y`.
## ℹ Row 2302 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
ts_plot(tweets, by = "days")

echo=F
tweets %>%
  group_by(standards) %>%
  ts_plot(by = "days")

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 <- sentiment_bing %>% 
  group_by(standards) %>% 
  count(sentiment, sort = TRUE) %>% 
  spread(sentiment, n) 

summary_bing <- sentiment_bing %>% 
  group_by(standards) %>% 
  count(sentiment, sort = TRUE) %>% 
  spread(sentiment, n) %>%
  mutate(sentiment = positive - negative) %>%
  mutate(lexicon = "bing") %>%
  relocate(lexicon)

head(sentiment_afinn)
## # A tibble: 6 × 6
##   standards screen_name  created_at          location            word      value
##   <chr>     <chr>        <dttm>              <chr>               <chr>     <dbl>
## 1 ngss      loyr2662     2021-02-27 17:33:27 Philadelphia, PA    win           4
## 2 ngss      Furlow_teach 2021-02-27 17:03:23 Michigan            love          3
## 3 ngss      Furlow_teach 2021-02-27 17:03:23 Michigan            sweet         2
## 4 ngss      Furlow_teach 2021-02-27 17:03:23 Michigan            signific…     1
## 5 ngss      TdiShelton   2021-02-27 14:17:34 Kentucky& Tennessee honored       2
## 6 ngss      TdiShelton   2021-02-27 14:17:34 Kentucky& Tennessee opportun…     2
summary_afinn <- sentiment_afinn %>% 
  group_by(standards) %>% 
  summarise(sentiment = sum(value)) %>% 
  mutate(lexicon = "AFINN") %>%
  relocate(lexicon)

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     
## # ℹ 13,862 more rows
#`echo=FALSE,message=FALSE`
summary_nrc <- sentiment_nrc %>% 
  filter(sentiment %in% c("positive", "negative")) %>%
  group_by(standards) %>% 
  count(sentiment, sort = TRUE) %>% 
  mutate(method = "nrc")  %>%
  spread(sentiment, n) %>%
  mutate(sentiment = positive/negative)

summary_afinn <- sentiment_afinn %>% 
  group_by(standards) %>% 
  summarise(sentiment = sum(value)) %>%
  mutate(lexicon = "AFINN") %>%
  relocate(lexicon)

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 …
## # ℹ 1,431 more rows
message=FALSE
#sentiment_afinn <- tweets %>%
#  unnest_tokens(output = word, 
#                input = text, 
#                token = "tweets")  %>% 
#  anti_join(stop_words, by = "word") %>%
#  filter(!word == "amp") %>%
#  inner_join(afinn, by = "word")

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
## # ℹ 1,530 more rows
message=FALSE
afinn_score <- sentiment_afinn %>% 
  group_by(standards, status_id) %>% 
  summarise(value = sum(value))
## `summarise()` has grouped output by 'standards'. You can override using the
## `.groups` argument.
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
## # ℹ 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 
## # ℹ 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)

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 with `by = join_by(method, standards)`
sentiment_percents <- sentiment_counts %>%
  mutate(percent = n/total * 100)

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")