A text-based analysis of Toronto Transit Commission Twitter data.
In this post I thought it would be interesting to download some data from the Toronto Transit Commission’s (TTC) twitter page and conduct some text mining on said data to get a better sense of interruptions of service (IOS) within the city of Toronto. Specifically, I want to understand which subway stops, and streetcar routes seem to receive the most attention on the TTC’s twitter feed and also appear to suffer from some sort of IOS. In order to do that I have downloaded six months’ worth of data from the @TTCNotices twitter feed. My intention was to do this post with only three months’ worth of data; however this post got delayed by several months, providing me with the opportunity to collect even more data (hooray!). Just to give you a sense of what to expect I created a neat distribution of total tweets, broken down by day below.
Main Findings and Considerations
I’ve decided to summarise all my findings first so that if you aren’t interested in seeing the more technical aspects of this post then you don’t need to read past this section(hooray!?x2).
- IOS tweets about streetcar routes are significantly more frequent than those about subway stations. Over the 6 months’ worth of data there are 2,279 tweets that have to do with IOS for streetcar routes compared to 912 for subway stations.
- The subway stations with the largest amount of IOS tweets are Union, Bloor, and Dundas West stations with 44, 37, and 34 IOS tweets.
- The 501 Queen streetcar recorded the most IOS tweets and by a large margin, the next streetcar the 504 King recorded 127 fewer IOS tweets.
- IOS tweets and which day of the week it is are independent of one another (i.e., There is not enough evidence to suggest that IOS tweets and day of the week are dependent of one another). For the figure below one represents Monday and seven represents Sunday.
- IOS tweets by hour suggest that the fewest amount of interruptions occur in the morning, between 8-10am, and more frequently late at night, between 8pm to 2am.
- This can differ significantly by subway station and to show that I created a Shiny app that will dynamically update a graph for you depending on the subway station you select.
I think its worth bringing up that these findings are all based on estimates that I constructed and should be thought of as informative but obviously not definitive, there is a lot of work to go before we can get to that point. However, I do think this analysis is promising and I look forward to making improvements and adjustments as time progresses. I was also going to include results with bus routes but I’ve run into some technical difficulties identifying bus routes correctly. I hope to make an edit to this post or do a separate post where I incorporate those results into this work.
As mentioned in the summary section I acquired this data from the @TTCNotices twitter account in two pulls. The first pull occurred a couple months ago and the most recent pull was performed approximately 5 days ago. Requesting information from the twitter API restricts the number of observations you can get any time so by waiting three months I was able to get another three months’ worth of data. In total, the entire sample contains roughly 6.3 thousand data points and if we take the straight average of the number of data points divided by the number of weeks that translates to approximately 245 tweets a week or 1,062 tweets a month.
In order to get a list of the subways stops and streetcar routes in the city I pulled information off of the TTC’s main webpage.
All the information I used for this project is located in my github page here.
If you are interested in learning about how you can pull data from twitter for yourself, through R then I recommend you read the following resources:
So first I am going to load all the necessary packages into R, show the line of code I used to pull twitter data into R and then use a function called ‘textScrubber’ to basically remove everything except words from each tweet in the data I download. I snagged this useful function from the second post above.
This analysis would have been incredibly difficult if the TTC did not utilize some sort of standardized language in describing its IOS. After reviewing this language, I was able to create a dictionary of IOS terms that segments the ‘type’ of interruption that is occurring. In total, the IOS dictionary I created contains seven terms: bypassing, delay, diverting, holding, suspended, longer than normal travel, and turning back.
I can also provide some examples of what a tweet looks like with each of these dictionary terms:
- Bypassing – 506 carlton bypassing howard park ave due to a collision at roncesvalles and howard park.
- Delay – all clear: the delay westbound at yonge station has now cleared and regular service has resumed on line 2.
- Diverting – 504 king route diverting westbound via queen, parliament due to a stalled streetcar at king & sumach.
- Holding – trains holding southbound at bloor station due to mechanical difficulties on board a train.
- Suspended – service suspended on line 2, bloor danforth, from yonge to broadview, due to a trespasser at track level at sherbourne station.
- Longer than Normal Travel – longer than normal travel expected on line 1, wilson to downsview, due to signal related issues at downsview station.
- Turning Back – 501 queen turning back from queen and kingston road, due to a collision at queen and neville park.
Now that the data is clean and tidy I can count those dictionary terms and place each of them their own separate variables using dplyr’s mutate command and an if-else chain. I am also going to create some additional date variables beyond what was provided to me in the original dataset.
Now that all the additional variables are created we can take a look at each of the total tweets, by subway station and streetcar route and a breakdown of each by the different IOS types.In order to create the plots I used a function that loops through each of the stations or routes and IOS words and provides a count for each.
We can also look at total broken down by which day of the week it is and use a Pearson’s chi-squared test to determine if the number of IOS tweets are independent of which day of the week it is. We can also do this including and excluding weekends to see if there is any significant difference in our result.
Based on the results of our chi-square test we can state that there is not enough evidence to suggest that IOS tweets and days of the week are dependent on another.
Determining how IOS tweets are distributed throughout the day should also be fairly interesting. I always remember the times where I am on the subway in the morning during rush hour and the TTC prompts that there is a delay but I don’t pay attention to all the days when the subway,bus or streetcar is running smoothly. So let’s create a distribution of IOS tweets by hour of the day.
The first time I looked at these results I was a little surprised. I expected there to be more interruptions earlier on in the day but that doesn’t seem to be the case at all. If anything the most interruptions occur between 1 to 2am.
Creating an aggregate plot of IOS tweets by hour was fairly straightforward but it doesn’t make sense to show you the 73 individual hourly plots by subway station. So I decided to learn how to make a shiny app that will allow you to choose a station and create a histogram of IOS tweets by hour of the day. You can access the Shiny app here.
So this wraps up my second blog post – I think looking through this TTC twitter data has been interesting and has generated some value in terms of determining which stations or routes appear to get a lot more attention on the TTC’s twitter feed. It has also made me realise that even rudimentary text-mining on unstructured data can take time and you need to know what you are looking for before you begin your analysis. In terms of next steps I think once I get a hold of a full year’s worth of data it will be worthwhile to develop some sort of prediction model and start to make some educated guesses when disruptions mights occur on Toronto’s subway line.