Site icon R-bloggers

How to perform Rolling Correlation in R

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Rolling Correlation in R, Correlations between two-time series on a rolling window are known as rolling correlations.

Correlations in time series are extremely valuable since they may be used to model and forecast if a relationship exists. But there’s a catch: a correlation isn’t static! It evolves over time.

The rolling correlation is one of the most important calculations in time series analysis.

Rank Order analysis in R » Optimal order & Probability

This sort of correlation has the advantage of allowing you to visualize the correlation between two-time series across time.

This article will show you how to use R to calculate rolling correlations.

Rolling Correlation in R

In R, how do you calculate rolling correlations?

Consider the following data frame, which shows the total profit for two separate products (x and y) over the course of a 12-month period:

SharePoint R integration and analysis » Automation »

Let’s create a data frame

tsData <- data.frame(month=1:12,
                   x=c(9, 12, 20, 12, 14, 15, 18, 12, 27, 33, 35, 21),
                   y=c(19, 36, 29, 26, 33, 18, 15, 28, 18, 19, 14, 25))
head(tsData)
  month  x  y
1     1  9 19
2     2 12 36
3     3 20 29
4     4 12 26
5     5 14 33
6     6 15 18

The rollapply() function from the zoo package can be used to calculate a rolling correlation in R.

The syntax for this function is as follows:

tidyverse in r – Complete Tutorial » Unknown Techniques »

rollapply(data, width, FUN, by.column=TRUE)

where:

data: The data frame’s name

width: The window width for the rolling correlation is specified as an integer.

FUN: The function that will be used.

by.column: Specifies whether the function should be applied to each column separately. This is TRUE by default, but we need to set it to FALSE to calculate a rolling correlation.

To compute the 3-month rolling correlation in sales between x and y, use the following formula:

library(zoo)
rollapply(tsData, width=3, function(x) cor(x[,2],x[,3]), by.column=FALSE)

To calculate correlations, the width (i.e. the rolling window) should be 3 or more.

Because the two columns we wanted to calculate correlations between were in positions 2 and 3, we used cor(x[,2],x[3]) in the calculations above.

If the columns you’re interested in are in different places, change these numbers.

R compiler Application-Installation Guide »

[1]  0.3602064 -0.2250176  0.1595863 -0.3634274 -0.7970168 -0.9549191 -0.6518694 -0.9316225
[9] -0.5447048 -0.9431854

This function returns the three-month correlation between product profit x and y. Consider the following scenario:

The correlation in profit during months 1 through 3 was 0. 3602064.

The correlation in profit during months 2 through 4 was -0. 2250176.

The correlation in profit during months 3 through 5 was -0. 1595863.

And so forth.

This formula can simply be changed to determine the rolling correlation over a different time period.

The following code, for example, demonstrates how to compute the 5-month rolling correlation in profit between the two products:

Repeated Measures of ANOVA in R Complete Tutorial »

rollapply(tsData, width=5, function(x) cor(x[,2],x[,3]), by.column=FALSE)
[1]  0.3781826 -0.1882708 -0.1850412 -0.7344118 -0.5643492 -0.4000877 -0.6476290 -0.9401665

For the last 5 months, this function returns the correlation between the two product profits. Consider the following scenario,

The correlation in profit during months 1 through 5 was 0. 3781826.

The correlation in profit during months 2 through 6 was -0. 1882708.

The correlation in profit during months 3 through 7 was -0. 1850412.

And so on.

Deep Neural Network in R » Keras & Tensor Flow

The post How to perform Rolling Correlation in R appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.