[R] Time Series

Gabor Grothendieck ggrothendieck at gmail.com
Mon Sep 6 16:56:56 CEST 2010


On Mon, Sep 6, 2010 at 10:24 AM, trb1 <thomasrbolton at yahoo.co.uk> wrote:
>
> Hi
>
> How would I analyse time series with
> - different lengths (i.e. one has 9 entries and the other has 14 entries)
> - different frequency (i.e. dates are random - no repeated length)
> - multiple values for the same time entry (e.g. 2009-10-23 below)
>
> i.e. my data takes the form:
> 1st time series
>
> 2009-10-07      0.009378
> 2009-10-19      0.014790
> 2009-10-23      -0.005946
> 2009-10-23      0.009096
> 2009-11-08      0.004189
> 2009-11-10      -0.004592
> 2009-11-17      0.009397
> 2009-11-24      0.003411
> 2009-12-02      0.003300
> 2010-01-15      0.010873
> 2010-01-20      0.010712
> 2010-01-20      0.022237
>
> 2nd time series
>
> 2009-09-23      0.076253
> 2009-10-07      0.039255
> 2010-02-17      0.039045
> 2010-03-09      0.024201
> 2010-03-25      -0.039810
> 2010-04-13      -0.012428
>
> I am unable to get any functions to work.
> A simple plot would be nice!
>


Try this.  We read in each series taking the last point in the event
that there are multiple points with the same date.  Then we plot each.

If we wish to plot them on the same plot then we merge each series
(for producing points) together with linear interpolations of both
series (for producing lines) and plot.

Lines1 <- "2009-10-07      0.009378
2009-10-19      0.014790
2009-10-23      -0.005946
2009-10-23      0.009096
2009-11-08      0.004189
2009-11-10      -0.004592
2009-11-17      0.009397
2009-11-24      0.003411
2009-12-02      0.003300
2010-01-15      0.010873
2010-01-20      0.010712
2010-01-20      0.022237"

Lines2 <- "2009-09-23      0.076253
2009-10-07      0.039255
2010-02-17      0.039045
2010-03-09      0.024201
2010-03-25      -0.039810
2010-04-13      -0.012428"

library(zoo)

z1 <- read.zoo(textConnection(Lines1), aggregate = function(x) tail(x, 1))
z2 <- read.zoo(textConnection(Lines2), aggregate = function(x) tail(x, 2))

plot(z1, type = "o")
plot(z2, type = "o")

# or together on the same plot
zz <- merge(z1, z2)
zz <- merge(z1, z2, na.approx(zz, na.rm = FALSE))
plot(zz, type = c("p", "p", "l", "l"), screen = 1:2)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list