[R] leap years in temporal series command ts

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 17 04:52:15 CET 2011


On Wed, Feb 16, 2011 at 11:37 AM, Peter Zorglub <peter.zorglub at gmail.com> wrote:
> Ok thanks. I got now a temporal series of 2557 elements thanks to the below
> code:
>
>> seriezoo=zooreg(serie, start=as.Date("2002-01-01"))
>> seriezooTS=as.ts(seriezoo)
>
> The structure of seriezooTS is then the following one (str command):
>
>> str(seriezooTS)
>              Time-Series [1:2557] from 11688 to 14244: 0.03424657 0.03881249
> 0.03078472 0.03536114 ...
>
> When applying the "decompose" command to get a seasonal decomposition, I got
> the following message:
>
>> decompose(seriezooTS, "mult")
>              Error in decompose(seriezooTS, "mult") :
>              time series has no or less than 2 periods
>
> So, I think I have to transform the first and last days of the temporal
> series in terms of Date or some kind of POSIXct number. Am I wrong? And how
> can I do that?

You must supply it a series with equal length cycles.  You can't
decompose the years into ordinary dates since there are not a constant
number of days in a year.  You could remove one day in any leap year
or you could summarize the series into a monthly series.

library(zoo)

# sample input
z <- zooreg(1:2557, start = as.Date("2002-01-01"))

# remove Feb 29's
zz <- z[format(time(z), "%m %d") != "02 29"]

# now each year has 365 points so
# make it into a series with freq 365
TS <- ts(coredata(zz), freq = 365)
d <- decompose(TS)

# put dates (without Feb 29's) back to view
d.mts <- do.call(cbind, d[1:3])
zd <- zoo(coredata(d.mts), time(zz))
View(zd)

# alternative: create monthly series
z.ag <- aggregate(z, as.yearmon, mean)
frequency(z.ag) <- 12
t.ag <- as.ts(z.ag)
d.ag <- decompose(t.ag)



-- 
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