Dear R users, 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? Thanks and regards, Tim
Cross correlation between two time series over nested time periods?
3 messages · Tim, Franklin Bretschneider
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
Thanks! Here the period of my time series B is a proper subinterval of the period of A. Does ccf(A,B) requires A and B span the same period? If A and B don't span the same period, what does ccf do? When moving B along the period of A by a lag, does ccf(A,B) calculate the cross correlation between B and the part of A overlapping with B? Or does ccf(A,B) calculate the cross correlation between A and the extension of B to the period of A by zero padding? --------------------------------------------
On Thu, 5/14/15, Franklin Bretschneider <bretschr at xs4all.nl> wrote:
Subject: Re: [R] Cross correlation between two time series over nested time periods?
Date: Thursday, May 14, 2015, 6:14 AM
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