Interpreting 270862 Fitbit footsteps using time series analysis with Prophet
By: Juan De Dios Santos
Tags: Data Journalism, Wander Data, Data Science, Python, Programming

Time Series Analysis

Back when I showed the first graph, I pointed out that it has a zigzag pattern, indicating that there are no two consecutive days with a similar number of steps. Honestly, I do believe this is the most exciting part of the dataset. For starters, it says something about me, and regarding how I led my days; have a nice walk today, take it easy tomorrow. However, the coolest part about it is that it gives me a legit reason to apply time series analysis to the dataset. In layman terms, the idea behind a time series analysis is to learn the patterns and trends from a sequence of events ordered by time to ultimately forecast future outcomes. For this project, I’m only interested in the first portion of this definition: learning the patterns. And for doing so, I’ll be using the Prophet package, a time series analysis library developed by Facebook.

The Petronas Towers in KL. Photo by me.

In this analysis, there are three things I want to focus on: the general trend, weekly and hourly seasonality. The general trend component describes the overall evolution of the series. Then, there’s the weekly seasonality which explains the time series’ behavior over the seven days of the week, and similar to it is the hourly seasonality which provides insight about my hourly steps routine. By studying these three components, we’ll be able to know how my walking pattern evolved over my whole stay in Malaysia, and of course, my preferred times and days to walk.

To fit our model in Prophet, first, we have to call the prophet() function using as a parameter the desired dataset. This input has to be a data frame with two columns: ds and y . The ds column, which stands for a datestamp, should either be a date ( YYYY-MM-DD ) or a timestamp ( YYYY-MM-DD HH:MM:SS ). In this case, our columns are all the 15 minutes intervals elapsed from July 9 00:00:00, until August 2 23:45:00 e.g. “ 2019–07–09 00:00:00 ” and “ 2019–07–09 00:15:00 .” The second column, y, is the numeric value we want to forecast, and again, here this means the steps taken. Now, armed with a tidy dataset, let’s proceed to fit our model, predict the forecast, and draw the seasonalities. The following code snippet shows how you can do it in Python.

Let me explain this a little bit. As with most things in data, the first thing we’ll is loading the dataset. Then, after creating our Prophet object, we’ll fit our model. Once that’s done, we’ll call model.predict(df) to obtain our forecast, and following this, we need to call model.plot_components(forecast) using the newly acquired forecast as the parameter to create the trend and seasonalities components plots. Lastly, we need plt.show() to draw them. (I’m using Seaborn to change the look of the plots). To simplify these three graphs, and for better visibility purposes, I post-processed the returned image to separate each figure and to title them.

Cameron Highlands

Wait! Before I show you the data, I want to quickly explain the meaning behind the numbers you’ll see in the y-axis of the plots. These values aren’t, I repeat, they are not, the actual number of steps taken at that particular time or day. Instead, we can interpret them as the incremental effect on y of that seasonal component (as stated here). For example, without spoiling too much, if you take a look at the following graph, you’ll find that the value of the first day, “2019–07–10”, is around 140, meaning that this day has an effect of +140 on y. Ok, now let’s see the data.

The first element I want to illustrate is the overall trend of the data. This component shows the series’ general changes over time. As we saw earlier, on the last days of my trip, I didn’t walk as much as I did during the first ones, and that’s what we see here. On the initial days, I was all hyped up and hungry for adventures in busy Kuala Lumpur. Then, after these hectic and rush days, I moved to the Cameron Highlands. There, high in the mountains, I slowed down a bit (Stranger Things season 3 is the culprit). After CH, I went down several hundred meters and reached the island of Penang, and its center, George Town. Honestly, the internet here wasn’t that stable, so I took solace in exploring the city, hence the small increase in the timeline. Lastly, at Langwaki, once again, I decided to slow down and enjoy the ocean breeze.