Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a irregular time series to a regular time series

I am having a problem when converting irregular time series to regular time series. Below a simplified example can be found:

require(zoo)
t <- as.character(c(1981,1984,1985))
d <- c(1,3,6)
dt <- data.frame(d,t)
t <- as.Date(t,"%Y")
z <- zoo(d,t)
plot(z)
ts.d <- as.ts(as.zooreg(z,freq=1)) # create a regular ts object
ts.d # regular time series

I would like to create a regular time series ts.d that looks like this c(1981,NA,NA,1984,1985).

The amazing thing is that the first time that I run this: it works! but when I want to run it again or repeat it (the as.ts()line) it stops workings and I obtain a very long time series:

ts.d # regular time series
Time Series:
Start = 4299 
End = 5760 
Frequency = 1 
  [1]  1 NA NA NA NA NA NA NA NA NA NA NA NA NA
 [15] NA NA NA NA NA NA NA NA 

etc.

What is going wrong?

like image 959
Janvb Avatar asked Oct 09 '10 20:10

Janvb


People also ask

What is irregular time series?

Definition: The irregular component of a time series is the residual time series after the trend-cycle and the seasonal components (including calendar effects) have been removed. It corresponds to the high frequency fluctuations of the series.

What are the frequencies in time series?

Frequency of a time series The “frequency” is the number of observations before the seasonal pattern repeats. When using the ts() function in R, the following choices should be used. Actually, there are not 52 weeks in a year, but 365.25/7=52.18 365.25 / 7 = 52.18 on average, allowing for a leap year every fourth year.


2 Answers

As has been pointed out the as.Date(as.character(t), "%Y") is incorrect as it does not give the desired month and day. If we wanted to convert years to "Date" class we could do this as.Date(as.yearmon(t)) using zoo's as.yearmon; however, then we would have the further problem that different years have different numbers of days so there is no way to have a regular series using dates to represent years.

Really we don't want dates in the first place. We just want to work with years in which case it simplifies to just:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985))
> 
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

or if we want to be safe we could do this which will force it to be annual even if the input has, by chance, a lower frequency: frequency(z) <- 1; as.ts(z) or just define the original zoo series to have a frequency of 1 right from the beginning:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985), frequency = 1)
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

With this example it does not make a difference but in this case z <- zoo(c(1, 3, 6), c(1981, 1983, 1985), frequency = 1) the explicit frequency would be needed to prevent it from having a frequency of 0.5 .

like image 109
G. Grothendieck Avatar answered Nov 10 '22 05:11

G. Grothendieck


It's not a bug. There are 1,461 days spanning the 4 years in your time series. And it doesn't work for me the first time I run it. as.Date(t,"%Y") doesn't know what month/day to use to make a date, so it uses today's month/day. That does not make for reproducible analysis. Try this instead:

t <- c(1981,1984,1985)
d <- c(1,3,6)
z <- zoo(d,t)
z <- merge(z,zoo(,c(1981,1982,1983,1984,1985)))
ts.d <- as.ts(z)

Which yields:

> ts.d
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6
like image 33
Joshua Ulrich Avatar answered Nov 10 '22 06:11

Joshua Ulrich