Skip to content

An R package to transform time series into networks

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

lnferreira/ts2net

Repository files navigation

ts2net

R-CMD-check Codecov test coverage CRAN/METACRAN License: MIT

ts2net is an R package to transform one or multiple time series into networks. This transformation is useful to model and study complex systems, which are commonly represented by a set of time series extracted from the small parts that compose the system. In this case, the network represents time series by nodes that are linked if their respective time series are similar or associated. Network models can also be used for time series data mining. Single or multiple time series can be transformed into networks and analyzed using network science and graph mining tools.

THIS IS A BETA VERSION - Please report bugs HERE

Reference

For details about this package, check out the paper:

Leonardo N. Ferreira, From Time Series to Networks in R with the ts2net Package (2022)

Please cite this paper if you used ts2net in a publication:

@article{ferreira24,
	author = {Ferreira, Leonardo N.},
	date = {2024/07/08},
	doi = {10.1007/s41109-024-00642-2},
	isbn = {2364-8228},
	journal = {Applied Network Science},
	number = {1},
	pages = {32},
	title = {From time series to networks in R with the ts2net package},
	url = {https://doi.org/10.1007/s41109-024-00642-2},
	volume = {9},
	year = {2024}
}

Installation

From CRAN:

install.packages("ts2net")

The development version can be installed from GitHub using the function install_github() from either devtools or remotes packages:

install.packages("remotes") # if `remotes` package is not installed
remotes::install_github("lnferreira/ts2net")

Usage

The ts2net package provides two modelling approaches: one or a set of time series as a network.

A set of time series as a network

The first modeling approach consists of transforming a set of time series into a network. This transformation typically involves the distance calculation for every pair of time series, represented by the distance matrix D. Then, D is transformed into a network using strategies such as k nearest neighbors, ε nearest neighbors, or complete weighted graphs. The following example shows how to calculate all pairs of distances (D) and construct a ε nearest neighbor network (ε-NN) using a data set (available with ts2net) composed of the temperature variations in 27 cities in the US:

library(ts2net)
# Calculating the distance matrix
D = ts_dist(us_cities_temperature_list, dist_func = tsdist_dtw)
# Finding the epsilon that corresponds to 30% of the shortest distances
eps = dist_percentile(D, percentile = 0.3)
# Constructing the network
net = net_enn(D, eps)

Multiple time series as networksMultiple time series as networks Fig. 1: Transforming time series into a network using ts2net. (a) The historical temperature of 27 cities in the US. (b) The distance matrix D (normalized DTW) for the data set. (c) The ε-NN network was constructed using 30% of the shortest distances. Node colors represent communities.

Functions to calculate the distance matrix:

  • ts_dist(): Calculates all pairs of distances and returns a distance matrix. Runs in parallel.
  • ts_dist_part(): Calculates pairs of distances in part of data set. This function is useful to run in parallel as jobs.
  • ts_dist_part_file(): Similar to ts_dist_part(), but read time series from files. It should be preferred when memory consumption is a concern, e.g., huge data set or very long time series.

Distance functions available:

  • tsdist_cor(): Absolute, positive or negative correlation. Significance test available.
  • tsdist_ccf(): Absolute, positive, or negative cross-correlation.
  • tsdist_dtw(): Dynamic time warping (DTW).
  • tsdist_nmi(): Normalized mutual information.
  • tsdist_voi(): Variation of information.
  • tsdist_mic(): Maximal information coefficient (MIC).
  • tsdist_es(): Events synchronization. Significance test available.
  • tsdist_vr(): Van Rossum. Significance test available.

Methods to transform multiple time series into a network:

  • net_knn(): k-NN network
  • net_knn_approx(): k-NN network. Faster, but may omit some nearest neighbors.
  • net_enn(): ε-NN
  • net_enn_approx(): ε-NN. Faster, but may omit some nearest neighbors.
  • net_weighted(): Full weighted network
  • net_significant_links(): Creates a network from a binary distance matrix (0 means significant links).

A single time series as a network

The second approach consists of transforming a single time series into a network. The following example shows how to transform a time series of monthly atmospheric concentration of carbon dioxide into a proximity network, a visibility graph, a recurrence network, and a transition network:

The second approach consists of transforming a single time series into a network. The following example shows how to transform the time series of monthly atmospheric concentration of carbon dioxide (available by default in R) into a proximity network, a visibility graph, a recurrence network, and a transition network:

co2_ts = as.numeric(co2)
# 1. Proximity (correlation) network
co2_windows = ts_to_windows(co2_ts, width = 12, by = 1)
D = ts_dist(co2_windows, cor_type = "+")
net_p = net_enn(D, eps = 0.25)
# 2. Visibility graph
net_vg = tsnet_vg(co2_ts)
# 3. Recurrence network
net_rn = tsnet_rn(co2_ts, radius = 5)
# 4. Transition (quantile) network
net_qn = tsnet_qn(co2_ts, breaks = 100)

Single time series as networksSingle time series as networks Fig. 2: (a) CO2 concentration time series. (b) Proximity network with time window 12 and one-value step. (c) Natural visibility graph. (d) Recurrence network (ε = 5). (e) Transition (quantile) network (100 equally-spaced bins). Node colors represent temporal order (yellow to purple), except in the transition network where colors represent the sequence of lower (yellow) to higher (purple) bins.).

Methods to transform one time series into a network:

  • ts_to_windows(): Extracts time windows that can be used to construct networks using the same approach used for multiple ones (Fig 1.).
  • tsnet_vg(): Natural and horizontal visibility graphs.
  • tsnet_rn(): Recurrence networks.
  • tsnet_qn(): Transition (quantile) networks.

License

ts2net is distributed under the MIT license.

Bugs

Found an issue 💣 or a bug 🐛 ? Please report it Here.

Suggestions

Do you have suggestions of improvements or new features? Please add them here.

Contact

Leonardo N. Ferreira
leonardoferreira.com
ferreira@leonardonascimento.com

About

An R package to transform time series into networks

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages