timevarcorr

An R package to compute time-varying correlations

https://github.com/courtiol/timevarcorr

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
    Found 1 DOI reference(s) in README
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

An R package to compute time-varying correlations

Basic Info
Statistics
  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • Open Issues: 7
  • Releases: 1
Created over 4 years ago · Last pushed over 2 years ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
editor_options: 
  chunk_output_type: console
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  fig.align = "center",
  out.width = "70%",
  warning = FALSE
)
```

# timevarcorr


[![CRAN
status](https://www.r-pkg.org/badges/version/timevarcorr)](https://CRAN.R-project.org/package=timevarcorr)
[![R-CMD-check](https://github.com/courtiol/timevarcorr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/courtiol/timevarcorr/actions/workflows/R-CMD-check.yaml)
[![test-coverage](https://github.com/courtiol/timevarcorr/actions/workflows/test-coverage.yaml/badge.svg)](https://github.com/courtiol/timevarcorr/actions/workflows/test-coverage.yaml)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)


This R package aims at measuring how the correlation between 2 time-series changes over time, following the method described in Choi & Shin (2021):

Choi, JE., Shin, D.W. Nonparametric estimation of time varying correlation coefficient.
J. Korean Stat. Soc. 50, 333–353 (2021). 

The chief idea is to perform a non-parametric kernel smoothing (using a common bandwidth) of all underlying components required for the computation of a correlation coefficient (i.e., $x$, $y$, $x^2$, $y^2$, $x*y$).

The automatic selection procedure for the bandwidth parameter proposed in the paper is implemented in this package.
The same goes for the computation of confidence intervals.

We also implemented the possibility to use Epanechnikov, Gaussian, or box kernels, as well as to estimate either the Pearson or the Spearman correlation coefficient.


## Installation

You can install the CRAN version of **timevarcorr** with:

```{r CRAN_install, eval=FALSE}
install.packages("timevarcorr")
```

You can install the development version of **timevarcorr** from GitHub with:

```{r GH_install, eval=FALSE}
# install.packages("remotes") ## uncomment and run if you don't have this package installed
remotes::install_github("courtiol/timevarcorr")
```

That should suffice!

Note that this package relies so far on only one direct dependency -- [**lpridge**](https://github.com/cran/lpridge) -- which itself depends on nothing but a plain R install.

Nonetheless, in some of the examples below, we also rely on [**dplyr**](https://dplyr.tidyverse.org) and  [**ggplot2**](https://ggplot2.tidyverse.org), so you would need to install these packages as well to reproduce the content of this README:

``` r
install.packages("dplyr")
install.packages("ggplot2")
```


## Examples

The main function of this package is called `tcor` and its documentation is available here:

```{r help, eval=FALSE}
help(tcor, package = timevarcorr)
```

Here is a very simple example using base-R syntax:

```{r example}
library(timevarcorr)

d <- stockprice[1:500, ]
example1 <- with(d, tcor(x = SP500, y = FTSE100, t = DateID, kernel = "normal"))
plot(example1, type = "l")
```

Here is the same example using tidyverse syntax (with confidence interval):

```{r example2, message=FALSE}
library(dplyr)
library(ggplot2)

d |> 
  reframe(tcor(x = SP500, y = FTSE100, t = DateID,
               kernel = "normal", CI = TRUE)) |>
  ggplot() +
    aes(x = t, y = r, ymin = lwr, ymax = upr) +
    geom_ribbon(fill = "grey") +
    geom_line() +
    labs(title = "SP500 vs FTSE100", x = "Time", y = "Correlation") +
    theme_classic()
```

And now, the same example showing gaps of observations in the time series:

```{r example3}
d |> 
  reframe(tcor(x = SP500, y = FTSE100, t = DateID,
               kernel = "normal", CI = TRUE, keep.missing = TRUE)) |>
  ggplot() +
    aes(x = t, y = r, ymin = lwr, ymax = upr) +
    geom_ribbon(fill = "grey") +
    geom_line() +
    labs(title = "SP500 vs FTSE100", x = "Time", y = "Correlation") +
    theme_classic()
```

You can do more. For example, you can use other kernels, fix the bandwidth manually, or use the Spearman's rather than the Pearson's correlation coefficient:

```{r example4}
example2 <- with(d, tcor(x = SP500, y = FTSE100, t = DateID,
                 cor.method = "spearman", kernel = "box", h = 10))
plot(example2, type = "l")
```

You can also test the difference in correlation coefficients between two time points:

```{r example5}
example3 <- with(d, tcor(x = SP500, y = FTSE100, t = DateID, kernel = "normal", CI = TRUE))
test_equality(example3, t1 = "2000-05-02", t2 = "2001-05-02")
```

Or you can test if specific time points (or all) differ from a reference value:

```{r example6}
test_ref(example3, t = c("2000-05-02", "2001-05-02"), r_ref = 0.5)
```


## Devel corner

This README file has been compiled using `devtools::build_readme()`, with the following setup:

```{r sessioninfo}
devtools::session_info()
```

Owner

  • Name: Alexandre Courtiol
  • Login: courtiol
  • Kind: user
  • Location: Berlin
  • Company: IZW

Quantitative wildlife biologist at Leibniz-IZW Berlin / Rstats lecturer at FU Berlin / Rstats consultant. R & LaTeX fulfill 99% of my coding needs!

GitHub Events

Total
Last Year

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 8
  • Total pull requests: 1
  • Average time to close issues: 5 days
  • Average time to close pull requests: less than a minute
  • Total issue authors: 1
  • Total pull request authors: 1
  • Average comments per issue: 0.13
  • Average comments per pull request: 0.0
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • courtiol (6)
Pull Request Authors
  • courtiol (1)
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 186 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: timevarcorr

Time Varying Correlation

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 186 Last month
Rankings
Dependent packages count: 28.9%
Dependent repos count: 36.9%
Average: 50.8%
Downloads: 86.6%
Last synced: 10 months ago

Dependencies

DESCRIPTION cran
  • R >= 2.10 depends
  • lpridge * imports
.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v3 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.4.1 composite
  • actions/checkout v3 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v3 composite
  • actions/upload-artifact v3 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite