Skip to content

Instantly share code, notes, and snippets.

@reuning
Last active February 7, 2018 21:59
Show Gist options
  • Save reuning/86450cc8c7aa72bce2c9c280da990935 to your computer and use it in GitHub Desktop.
Save reuning/86450cc8c7aa72bce2c9c280da990935 to your computer and use it in GitHub Desktop.
Uses the twitteR and SentimentAnalysis package to grab tweets over the last 7 days mentioning 'stock market' and extract sentiment
library(twitteR)
library(tm)
library(SentimentAnalysis)
setup_twitter_oauth(consumer_key = ,
consumer_secret =,
access_token = ,
access_secret = )
days <- seq.Date(from=as.Date("2018-01-31"), to= as.Date("2018-02-07"), by="day")
sent <- matrix(NA, nrow=(length(days)-1), ncol=4)
se <- matrix(NA, nrow=(length(days)-1), ncol=4)
tweet.store <- list()
for(ii in 2:length(days)){
tweets <- searchTwitter("stock market", n =500, lang="en", resultType = "mixed",
since=as.character(days)[ii-1],
until=as.character(days)[ii])
tweet.store[[ii-1]] <- tweets
fn <- unlist(lapply(tweets, function(x) x$text))
fn <- gsub('http.*\\s*', '', fn)
fn <- gsub('@\\S+\\s*', '', fn)
fn <- gsub('#', '', fn)
fn <- iconv(fn, "latin1", "ASCII", sub="")
vs <- VectorSource(fn)
corp <- Corpus(vs)
dtm <- toDocumentTermMatrix(corp)
out <- analyzeSentiment(corp)
sent[ii-1,1] <- mean(out$SentimentGI, na.rm=T)
se[ii-1, 1] <- sd(out$SentimentGI, na.rm=T)
sent[ii-1,2] <- mean(out$SentimentHE, na.rm=T)
se[ii-1, 2] <- sd(out$SentimentHE, na.rm=T)
sent[ii-1,3] <- mean(out$SentimentLM, na.rm=T)
se[ii-1, 3] <- sd(out$SentimentLM, na.rm=T)
sent[ii-1,4] <- mean(out$SentimentQDAP, na.rm=T)
se[ii-1, 4] <- sd(out$SentimentQDAP, na.rm=T)
}
#png("twitter_stock.png", height=6, width=6,
# res=400, units='in')
plot.new()
plot.window(ylim=c(-.10, 0.10), xlim=c(1, length(days)-1))
rect(xleft=2.5, xright=3.5, ybottom = -1, ytop=1,
col=ggplot2::alpha("red", .25), border=NA)
for(ii in 1:(length(days)-1)){
points(y=sent[ii, 1], x=ii, lwd=2)
lines(y=sent[ii, 1] + c(se[ii, 1], -se[ii,1])/sqrt(500), x=c(ii,ii), lwd=2)
}
axis(2)
box()
axis(1, at=1:7, labels=format(days[-7], "%b %d"))
abline(h=0, lty=2)
title(main="Sentiment of Tweets mentioning 'stock market'",
xlab="Day", ylab="Sentiment")
#dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment