Twitter Analysis of the US Presidential Debate

The following are word clouds of tweets for each candidate from the October 16, 2012 debate with the bigger words the more often they were used in tweets (click on each word cloud to enlarge):

And the net-negative posts for each candidate:

Please note that the bigger the word is in the word cloud the more often it was used.

The R code for creating the word clouds

The following code was adapted from here and is an extension of previous work:

ap.corpus <- Corpus(DataframeSource(data.frame(as.character(romneypositive[,3]))))
ap.corpus <- tm_map(ap.corpus, removePunctuation)
ap.corpus <- tm_map(ap.corpus, tolower)
ap.corpus <- tm_map(ap.corpus, function(x) removeWords(x, c(r,stopwords("english"))))
ap.tdm <- TermDocumentMatrix(ap.corpus)
ap.m <- as.matrix(ap.tdm)
ap.v <- sort(rowSums(ap.m),decreasing=TRUE)
ap.d <- data.frame(word = names(ap.v),freq=ap.v)
pal2 <- brewer.pal(8,"Dark2")
png("romneypositive.png", width=1280,height=800)
wordcloud(ap.d$word,ap.d$freq, scale=c(8,.2),min.freq=3,
max.words=Inf, random.order=FALSE, rot.per=.15, colors=pal2)

Disclaimer: Any errors can be attributed to the fact that I was drinking heavily, that I was in Dallas, and that it was half four in the morning when I finished writing this.

11 thoughts on “Twitter Analysis of the US Presidential Debate

  1. Love it! But in what possible sentence can the word “ass” be interpreted as positive? (Romney, blue, starting at the “E” of “love”)

  2. Have you run into the following error when grabbing tweets:

    > pres_data <- president()
    Error in .self$twFromJSON(out) :
    Error: Malformed response from server, was not JSON

    If so, how did you get around it?

    1. Funny enough, I ran into this problem just yesterday for the first time, and I fixed it by running the package “RJSONIO”. Let me know if this works for you as well.

  3. I frequently encountered

    Error in .self$twFromJSON(out) :
    Error: Malformed response from server, was not JSON

    when using the searchTwitter() function of the R twitteR package.

    It does not depende on the number of tweets fetched as n=1500 worked well with some queries and not with others.
    my guess is that it depends on specific tweet contents.

    loading RJSONIO after twitteR solved the problem.
    RJSONIO masks the functions fromJSON() and toJSON() functions of the rjson package (which is loaded by loading the twitteR package)

    Apparently RJSONIO is able to handle some tweets better than rjson…

    1. It’s because it appeared in the post with a positive sentiment score for one and in negative sentiment score posts for the other candidate.

Leave a Reply to Majeed Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s