[R] Cross correlation between two time series over nested time periods?

Franklin Bretschneider bretschr at xs4all.nl
Thu May 14 12:14:06 CEST 2015


On 2015-05-14 , at 02:11, Tim via R-help <r-help at r-project.org> wrote:


Hello Tim,


Re:


> I have two time series
> 
> 
> Calculate and plot cross correlation between two time series over nested time periods. Each point in either time series is for a week (not exactly a calendar week, but the first week in a calendar year always starts from Jan 1, and the other weeks in the same year follow that, and the last week of the year may contain more than 7 days but no more than 13 days).
> 
> The first time series A is stored in a compressed (.gz) text file, which looks like (each week and the corresponding time series value are separated by a comma in a line):
> week,value
> 20060101-20060107,0
> 20060108-20060114,5
> ...
> 20061217-20061223,0
> 20061224-20061230,0
> 20070101-20070107,0
> 20070108-20070114,4
> ...
> 20150903-20150909,0
> 20150910-20150916,1
> 
> The second time series B is similarly stored in a compressed (.gz) text file, but over a subset of period of A, which looks like:
> week,value
> 20130122-20130128,509
> 20130129-20130204,204
> ...
> 20131217-20131223,150
> 20131224-20131231,148.0
> 20140101-20140107,365.0
> 20140108-20140114,45.0
> ...
> 20150305-20150311,0
> 20150312-20150318,364
> 
> I wonder how to calculate the cross correlation between the two time series A and B (up to a specified maximum lag), and plot A and B in a single plot? 




The auto- and crosscorrelation functions are in the stats package:

acf(x, lag.max = NULL,
    type = c("correlation", "covariance", "partial"),
    plot = TRUE, na.action = na.fail, demean = TRUE, ...)

ccf(x, y, lag.max = NULL, type = c("correlation", "covariance"),
    plot = TRUE, na.action = na.fail, ...)

See further: ?ccf

Succes and
Best wishes,


Frank
---



Franklin Bretschneider
Dept of Biology
Utrecht University
bretschr at xs4all.nl



More information about the R-help mailing list