Practice 4.1
In order to perform all these exercises you will need iGraph, vosonSML, rtweet, tidytext, tidywords, stopwords plus API keys. They are not all needed for each exercise but each one requires igraph and vosonSML.
library (iGraph)
library(vosonSML)
Exercise 1. Twitter data (Figures 4.3 and 4.4)
How to obtain Twitter data and construct a figure similar to 4.3. Note this depends on the time of data collection and so will not be exactly the same network as shown in 4.3. We will also need the rtweet library.
library(rtweet)
First we collect the data from Twitter, note this assumes the user has a twitter account. We search 500 tweets that contain the term "sunbelt"
twitterdata<-search_tweets(q="sunbelt",n=500)
class(twitterdata) <- append(class(twitterdata), c("datasource", "twitter"))
We now construct and plot the twitter actor network.
actorNetwork <- twitterdata %>% Create("actor", writeToFile = TRUE, verbose = TRUE)
actorGraph <- actorNetwork %>% Graph(writeToFile = TRUE)
plot(actorGraph,vertex.label="",vertex.size=5,vertex.color="red",edge.arrow.size=0.1,layout=layout_with_kk)
If the user has twitter API keys then the data can be collected as follows. Note for security keys have been hidden. The network and plot are constructed as above.
myDevKeys <- list(appName = "MyApp", apiKey = "xxxxxxxxxxxxx", apiSecret = "xxxxxxxxxxxxxxx", accessToken = "xxxxxxxxxxx", accessTokenSecret = "xxxxxxxxxxx")
twitterAuth <- Authenticate("twitter", appName = myDevKeys$appName, apiKey = myDevKeys$apiKey,apiSecret = myDevKeys$apiSecret, accessToken = myDevKeys$accessToken, accessTokenSecret = myDevKeys$accessTokenSecret)
twitterData <- twitterAuth %>% Collect(searchTerm = "sunbelt",numTweets = 500)
To construct the semantic network from the same downloaded data looking at just the top 1% of the terms we need to proceed as follows. We need to use additional libraries as detailed at the top.
library (tidytext)
library (tidywords)
library (stopwords)
semanticNetwork <- twitterdata %>% Create("semantic",termFreq = 1)
semanticGraph <- semanticNetwork %>% Graph(writeToFile = TRUE, directed = FALSE)
cc <- clusters(semanticGraph)
g3 <- induced_subgraph(semanticGraph,which(cc$membership == which.max(cc$csize)))
plot(g3,vertex.shape="none",vertex.label.cex=0.7,edge.width=E(g3)$weight)
Exercise 2 YouTube network (Figure 4.5)
How to download and plot the YouTube comments network of the Falcon 9 rocket launch. A network generated from this data is probably the same as shown in the book although it is possible comments were added after the data was downloaded so it may be slightly different. This requires API keys (but these are easy to generate see the notes on the website) and just the iGraph and vosonSML libraries.
myAPIKey <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
youtubeAuth <- Authenticate("youtube", apiKey = myAPIKey)
videoIDs <- GetYoutubeVideoIDs(“https://www.youtube.com/watch?v=TxBj8R7XKe4”)
youtubeData <- youtubeAuth %>%
Collect(videoIDs = videoIDs, maxComments = 500, writeToFile = TRUE)
actorNetwork <- youtubeData %>% Create("actor") %>% AddText(youtubeData)
actorGraph <- actorNetwork %>% Graph(writeToFile = TRUE)
V(actorGraph)$color <- ifelse(V(actorGraph)$node_type=="video", "black", "white")
plot(actorGraph, vertex.label="", vertex.size=4, edge.arrow.size=0.05)
Exercise 3 Reddit network (Figure 4.6)
How to download and plot the reddit activity network on fracking. Note this is stable and so should be identical to Figure 4.6
myThreadUrls <- c("https://www.reddit.com/r/engineering/comments/1osbo3/what_are_your_unbiased_thoughts_on_hydraulic/")
redditData <- Authenticate("reddit") %>%
Collect(threadUrls = myThreadUrls, writeToFile = TRUE)
activityNetwork <- redditData %>% Create("activity") %>% AddText(redditData)
activityGraph <- activityNetwork %>% Graph(writeToFile = TRUE)
V(activityGraph)$color <- ifelse(V(activityGraph)$node_type=="thread", "black", "gray")
plot(activityGraph,vertex.label="",vertex.size=7,edge.arrow.size=0.05,layout=layout_with_kk)