Visualizing Inter-Arrival Times of Tweets

Feb 14, 2017

How often do people talk about a specific topic? How popular is a hashtag in twitter? In order to answer this kind of questions, we may examine how fast the next tweet will arrive. This post shows how to visualize inter-arrival times of tweets with a specific hashtag.

Collecting Tweets

Calculating inter-arrival times

Creating a histogram plot of the inter-arrival times

Calculating cumulative probabilities

Grouping breaks, cumulative probabilites and the hashtag into a data frame

Plotting the cumulative probability distribution of the inter-arrival times

Here, we choose the hashtag #coffee for the query to request 1000 sample tweets. The function twListToDF converts the tweet list into a data frame tweets.df from which we will extract the creation time of the tweets by accessing the created field in the data frame.

tweets<-searchTwitter("#coffee",n=1000)tweets.df<-twListToDF(tweets)

Creating a Histogram Plot of the Creation Times

We will firstly take a look at the creation time of all the tweets. Each tweet has a key whose value is its creation time with the UTC time zone. The following shows statistics of the creation time by calling the R function summary.

2. Calculating Inter-Arrival Times

How soon will be the next tweet arriving? We need to calculate the time intervals between every two consecutive tweets. The sample tweets are not ordered. Thus, we need to sort the tweets by the creation time in ascending order.

Run the following function which shows the type of the created vector is POSIXct.

class(tweets.df$created)

[1] “POSIXct” “POSIXt”

It needs to be coerced to integer type for sorting.

The function as.integer will convert the times to integers. The function sort will sort the time integers in ascending order. Run the following sort function.

Density of a random variable describes the relative likelihood for this random variable to take on a given value.

In the next step, we will calculate the cumulative probabilities for each possible interval. For each time interval, its cumulative probability describes the likelihood of expecting the next tweet with a shorter wait time than the length of interval.

4. Calculating cumulative probabilities

The following snippet will calculate the cumulative probabilities in cumProb.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Calculating the cumulative probabilities of created.diff
# 1. create bins
bin.width<-1#specify the width of each bin
min<-min(created.diff)max<-max(created.diff)breaks<-seq(min,max+1,by=bin.width)# specify end points of bins
cuts<-cut(created.diff,breaks,right=FALSE)# assign each interval with a bin
# 2. the table function returns a table for counts/frequency of each level/bin
freq<-table(cuts)# 3. returns a vector whose elements are the cumulative sums
cumFreq<-cumsum(freq)# 4. divide freqency by the total to get cumulative probability
cumProb<-cumFreq/length(created.diff)

5. Grouping breaks, cumulative probabilities and the hashtag into a data frame

Before plotting, we want to make a data frame to wrap all the data that will be used in the plot.

1
2
3
4
5
6
7
8
9
10
11

# create a sequence from min to max
x<-c(min(created.diff):max(created.diff))# convert cumProb to a vector
y<-as.vector(cumProb)# create a factor for legend
tag<-c('#coffee')legend<-as.factor(rep(tag,times=length(y)))#factor vector of same length of y
# group x, y and legend into a data frame
dat<-data.frame(x,y,legend)# inspect dat1
dat[1:5,]

Then run the following snippet which will plot the cumulative probability distribution of inter-arrival times for #coffee.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# one
p1<-ggplot(data=dat,aes(x=x,y=y))+geom_line()# two
p2<-p1+geom_line(aes(colour=legend))+xlim(-1,60)+xlab('Inter-Arrival Time in Seconds')+ylab('Cumulative Probability')+theme(axis.text=element_text(size=7),axis.title=element_text(size=7),plot.title=element_text(lineheight=.2),legend.text=element_text(size=7),legend.position='bottom')# three
p3<-p2+geom_point(aes(colour=legend))library(easyGgplot2)ggplot2.multiplot(p1,p2,p3,cols=3)

The Possion distribution is a discrete frequency distribution that gives the probability of a number of independent events occurring in a fixed time. The Poisson distribution deals with mutually independent events, occurring at a known and constant rate r per unit (of time or space). The rate r is the expected or most likely outcome.

If the tweets arrive rapidly, the curve will become steep.

7. Finding the Probability of the Next Tweet Arriving Less Than x Seconds

To find the likelihood of seeing the next tweet less than 5 seconds with the hashtag coffee, run the following snippet:

total<-1000sum(created.diff<=5)/length(created.diff)

0.456456456456456

The result above tells us that the probability of the wait time being less than 5 seconds is about 0.46. This means it is not very realistic to expect the next tweet with #coffee less than 5 seconds.

If we want to find out the amount of seconds that 75 percent of tweets will arrive in less than that amount of seconds, run the quantile function:

quantile(created.diff,0.75)

75%: 13

The result shows that 75 percent of tweets will arrive in less than 13 seconds.

The poisson test shows that for \(95%\) of the samples from twitter with #coffee with a sample size of 1000 and the proportion of samples that arrive in mu seconds or less will fall into the confidence interval: 58.2% to 68.1%