Timetk: The R Library for Time Series Analysis | Towards Data Science

Have you ever seen data like weather temperature, stock price, or sales numbers that change over time? I’m pretty sure that you’ve already seen that data in real life at least once. Those are time-series data. Time Series is data where it changes over time, whether it’s caused by other factors or the time itself.
Analyzing time-series data can be a challenging task. For example, detecting anomalies or even want to see seasonality trends by months or weeks. If you want to do that, you need to do scripting and also use so many libraries.
Now thankfully, there’s a library for R programming language that can help us. It’s called timetk. Timetk is a library created by Matt Dancho and Davis Vaughan for analyzing and visualizing the time series data. Timetk is easy to use, and it’s way easier if you ever play with tidyverse.
In this article, I will show you how to use timetk in R.
Without further, let’s get started!
The first thing that we need to do is to install the libraries. In this case, we will install timetk and also tidyverse. After that, we can import the library. Here is the command look like,
install.packages("timetk")install.packages("tidyverse")library(tidyverse)library(lubridate)library(timetk)
After you import the libraries, now let’s import the data. In this case, we will use data from UCI Machine Learning Repository called Bike Sharing Dataset. This data contains information about the number of bike users on a single day from 2011 until 2012.
Thankfully, the timetk does include the data, so you don’t need to download it. If you still want to use the original data, You can access the dataset here.
To see the data, you can use the command here,
View(bike_sharing_daily)
Here is the preview of the data,
As you can see above, there are several columns inside the data. There is information regarding time, temperature, humidity, and the number of bike users. In this case, we only use dteday and cnt columns, where each represents the date and the number of bike users. Here is the code to retrieve the data,
# Get the databike_sharing <- bike_sharing_daily %>% # Get the columns select(dteday, cnt) %>% # Change the columns name set_names(c("date", "value"))bike_sharing
Here is the preview of the data,
After we get the data, now let’s get into the visualization. To visualize the time series, we can use the plot_time_series function from the timetk library. Here is the command look like,
bike_sharing %>%  plot_time_series(date, value)
Here is the command’s result,
As you can see above, the function returns an interactive visualization. That visualization is based on a library called Plotly. Because of that, you can hover to get the data point and zoom to see details of the visualization.
You can also add a slider to filter the data observation by adding the .plotly_slider parameter to true. Here is the command look like,
bike_sharing %>%  plot_time_series(date, value, .interactive=TRUE, .plotly_slider=TRUE)
Here is the result,
If you only want the static visualization, you can set the .interactive parameter to false. The function will return a static visualization which is based on the ggplot library. The command looks like this,
bike_sharing %>% plot_time_series(date, value, .interactive=FALSE)
Here is the result,
Just like with other visualizations, you can do grouping visualization with the timetk library. As you can see, the data ranges from 2011 and 2012. In this case, we will group the visualization based on the year it occurs. Here is the command looks like,
bike_sharing %>%  group_by(year(date)) %>%  plot_time_series(date, value, .facet_scales="free")
The result looks like this,
To make the visualization more eye-catching, we can add the .color_var parameter to color the data based on the month it occurs. Also, we will remove the smooth blue line by setting the .smooth parameter to false. Here is the command looks like,
bike_sharing %>%  group_by(year(date)) %>%  plot_time_series(date, value, .facet_scales="free", .smooth=FALSE .color_var=month(date), .interactive=FALSE)
Here is the result,
Besides that, we can also visualize the time series by its seasonality, whether in hours, days, weeks, or even months. You can get it all by using the plot_seasonal_diagnostics function. Here is the command looks like,
bike_sharing %>%  group_by(year(date)) %>%  plot_seasonal_diagnostics(date, value, .x_lab='Date', .y_lab='# of users', .title='Bike Sharing Users')
Here is the result,
Finally, I want to show you one of the main features of this library. You can detect anomalies by using only one function.
Anomaly detection is a task for finding data that has unusual value to it. In other words, the data is different from the other.
In timetk, we can do two things for anomaly detection. The first thing that you can do is to visualize the anomalies in the data. In this case, We will visualize the time series and the anomalies. Also, we divide them by year.
We can use the plot_anomaly_diagnostics function to visualize the time series and the anomalies and split them by year. The command looks like this,
# Get the databike_sharing %>%  # Group the data by year  group_by(year(date)) %>%  # Visualize the result  plot_anomaly_diagnostics(date, value)
Here is the visualization look like,
The second thing that you can do is to gather data that has anomaly to it. We group the data by year. Also, we filter the data to contain only the data that has anomaly to it. We can use the tk_anomaly_diagnostics function to gather the anomalies. Here is the command look like,
anomaly_data <- bike_sharing %>%  group_by(year(date)) %>%  tk_anomaly_diagnostics(date, value) %>%  filter(anomaly == 'Yes')View(anomaly_data)
Here is the preview of the data,
Congratulations! Now you have learned how to use timetk in R for time series analysis. With timetk, we can analyze the time series data easier than before.
I hope the article useful to you. If you want to see more articles like this, you can follow my Medium. If you have any questions or want to have conversations, you can connect with me on LinkedIn.
Thank you for reading my article!