
Calculate rolling (moving-window) correlation between two time series
Source:R/analysis_functions.R
rolling_corr.RdComputes the Pearson correlation between x_var and y_var
over a rolling window of n time periods within each group, using
runCor. The first n - 1 observations in each
group will be NA because there are insufficient data to fill the
window.
Arguments
- dat
A data frame containing the two time series, a date column, and a grouping column.
- date_var
Character string giving the name of the date column. Must be of class
Date. Default is"date". Rows are sorted bydate_varwithin each group before the rolling correlation is computed (required becauserunCorassumes row order is chronological).- grp_var
Character string giving the name of the grouping column. Rolling correlation is computed separately within each group.
- x_var
Character string giving the name of the primary time series column.
- y_var
Character string giving the name of the secondary time series column.
- n
Integer. Width of the rolling window in time periods. Default is 14.
Value
A data frame with the same columns as the input plus one additional
numeric column, roll_corr, containing the rolling Pearson
correlation between x_var and y_var. Values range from -1
to 1. The first n - 1 observations per group are NA. Note
that rows where x_var or y_var are NA are removed
before the rolling correlation is computed, so the returned frame may have
fewer rows than the input.
See also
cross_corr to identify the optimal lag before
computing rolling correlation; runCor for the
underlying method; plot_corr to visualise the result.