Skip to content

Instantly share code, notes, and snippets.

@noamross
Created April 24, 2018 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamross/97d75dabfaf33703ec1b38d9803389b3 to your computer and use it in GitHub Desktop.
Save noamross/97d75dabfaf33703ec1b38d9803389b3 to your computer and use it in GitHub Desktop.
hrbrfear index
library(rtweet) #rtweet API creds should already be set up
library(dplyr)
library(ggplot2)
library(hrbrthemes)
library(tidytext)
library(lubridate)
hbt <- get_timeline("hrbrmstr", n=3200)
hbt2 <- hbt %>%
select(status_id, created_at, text) %>%
unnest_tokens(word, text)
af <- get_sentiments("afinn")
hbindex <- inner_join(hbt2, af) %>%
group_by(date = as.Date(created_at)) %>%
summarize(hf = mean(score), hf_sd = sqrt(var(score)/n())) %>%
filter(!is.na(hf_sd))
ggplot(hbindex, aes(x = date, y = hf, ymin = hf - 2*hf_sd, ymax = hf + 2*hf_sd)) +
geom_ribbon(fill="blue", alpha=0.3) +
geom_line(col = "purple") +
scale_x_date(date_breaks = "1 months") +
hrbrthemes::theme_modern_rc() +
labs(x = "Date", y = "hrbrfear index", title = "hrbrfear index",
subtitle = "An Internet security threat index composed purely of @hrbmstr tweet sentiment scores\n
More negative = unplug everything")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment