Skip to main content

Introduction to Data Science Version 3

Chapter 11 Tweet, Tweet!

We’ve come a long way already: Basic skills in controlling R, some exposure to R-studio, knowledge of how to manage add-on packages, experience creating a function, essential descriptive statistics, and a start on sampling distributions and inferential statistics. In this chapter, we use the social media service Twitter to grab some up-to-the minute data and begin manipulating it.

Sources.

R Script - Create Vector of Probabilities From Arrival Times
# ArrivalProbability - Given a list of arrival times
# calculates the delays between them using lagged differences
# then computes a list of cumulative probabilities of arrival
# for the sequential list of time increments
# times - A sorted, ascending list of arrival times in POSIXct
# increment - the time increment for each new slot, e.g. 10 sec
# max - the highest time increment, e.g., 240 sec
#
# Returns - an ordered list of probabilities in a numeric vector
# suitable for plotting with plot()
ArrivalProbability<-function(times, increment, max)
{
# Initialize an empty vector
plist <- NULL
# Probability is defined over the size of this sample
# of arrival times
timeLen <- length(times)
# May not be necessary, but checks for input mistake
if (increment>max) {return(NULL)}
for (i in seq(increment, max, by=increment))
{
# diff() requires a sorted list of times
# diff() calculates the delays between neighboring times
# the logical test <i provides a list of TRUEs and FALSEs
# of length = timeLen, then sum() counts the TRUEs.
# Divide by timeLen to calculate a proportion
plist<-c(plist,(sum(as.integer(diff(times))<i))/timeLen)
}
return(plist)
}
R Functions Used in This Chapter
attach() - Makes the variables of a dataset available without $
as.integer() - Coerces data into integers
detach() - Undoes an attach function
diff() - Calculates differences between neighboring rows
do.call() - Calls a function with a variable number of arguments
function() - Defines a function for later use
hist() - Plots a histogram from a list of data
install.packages() - Downloads and prepares a package for use
lapply() - Applies a function to a list
library() - Loads a package for use; like require()
mean() - Calculates the arithmetic mean of a vector
median() - Finds the statistical center point of a list of numbers
mfv() - Most frequent value; part of the modeest() package
mode() - Shows the basic data type of an object
order() - Returns a sorted list of index numbers
rbind() - Binds rows into a dataframe object
require() - Tests if a package is loaded and loads it if needed
searchTwitter() - Part of the twitteR package
str() - Describes the structure of a data object
sum() - Adds up a list of numbers