Twitter analysis

Introduction Into R for Social Scientists

Twitter analysis

Dr. Elze G. Ufkes

Benjamin Ziepert

1 Instructions

This handout shows a step-by-step approach to harvest, visualize and analyze twitter data. You can use it to run the code on your own computer. Copy pasting the codes in the gray blocks and running it should produce the same outcomes as shown in the document.

2 Activating libraries used for this assignment

First we'll need to activate the libraries used for this assignment. Remember that if this is the first time you use a package that you need to install the package using install.packages() first.

library(twitteR)
library(tm)
library(wordcloud)
library(psych)

3 Load functions

To make the lecture easier for you we created some functions that will do some work for you. You can load these functions with the command below.

source("S03D02-Twitter-Functions.R")

4 Log in to twitter

Before we can start we need to log in to a twitter account (make sure you're connected to internet). For this assignment I will put the login details on Canvas. If you want to use your own twitter account you can find the codes used for these keys and tokens on apps.twitter.com.

5 Harvesting twitter data

Okay we're set! Now we can "harvest" data currently shared at twitter. For instance try the following example to get the 5 most recent tweets from our departments twitter account use the following code. Note that setting includeRts to TRUE or FALSE determines if you only harvest original tweets or re-tweets as well.

Or use the following code to search for 10 tweets containing a specific keyword. Feel free to change the keyword to something you find interesting.

# Search for specific keyword
searchTwitter("trump", n = 10)

The next step is to not only look at the tweets (we can do that online in Twitter). We want to analyze the tweets! The first step is to save the tweets we're interested in an R-object. You can give any name to this object, so let's use tweets. See how we used the previous command to create this R-object?

Okay now we can inspect the information that is stored in the object tweets we just created. For instance try the following code just to show the first tweet stored in this object. Since tweets is a list we created with 100 twitter messages, we can type the name of the object tweets followed by the number of the Tweet you want to see [1] to show the first message.

tweets[1]

## [[1]]
## [1] "MurfAD: RT @mchooyah: ADM McRaven was born to lead this mission. Ill follow him anywhere. If only people heard the real speech he gave the Team... "

We also can have a look at the so-called meta-data connected to this tweet. For instance how often did Twitter users re-tweet, this message?

tweets[[1]]$retweetCount

## [1] 750

Similarly you can use the following functions (try it!):

$screenName to get the name of the twitter account tweeting this message

$favoriteCount to get the number of likes or favorites

$retweetCount to get the number of re-tweets

Finally you can also download meta-data (information) of any public twitter profile. For instance try the following.

# Save information from a specific user, based on their twitter name
user <- getUser("PCRSuTwente")

# Now you can inspect for instance the number of followers for this user.
user$followersCount

## [1] 91

Similarly you can use the following functions (try it! What do they do?):

user$location

user$profileImageUrl

user$name

6 Consquences of a terrorist attack

On Tuesday November 31st 2017 a man drove his truck into people on a bike path in Manhattan, NYC. Terror attacks may have consequences for how people feel. But do they also have effects on how they Tweet? For instance how people tweet about the US president? Let's find out!

First we can download a random selection of twitter messages mentioning Trump in the days before (pre) and after (post) the attack in NYC. In this case we download the maximum number of tweets Twitter allows you to download in one time using their public access point: 1500.

Note that Twitter only allows you to download messages based on a keyword for 6-9 days back. Therefore you can skip the next step and use the commands below for your own twitter search.

Then as a final preparation step the text needs to be "cleaned". That is we'll remove all formatting, punctuation and other information irrelevant for the next steps. We will use one of the custom built functions twitter_clean_up to do this.

Now open the environment (on the right-top of R Studio) and click on nyc_post_df and/or nyc_pre_df to inspect the data-sets.

A powerful advantage of R is the possibilities in visualizing the data. For text data an ideal method to visualize the data is creating a word cloud with the most frequently used words. The word cloud below also uses font size to show how often a word is used: the bigger words representing words that are tweeted more often.

To get to the word cloud we need to transform the data-set a couple of times. Don't worry about these steps, we also created a custom function (twitter_word_freq) for you that is already loaded.

You only need to define words that will be excluded (stop words) from the word cloud. For instance, we don't want to see words like "and", "or", or "trump".