[R] Building a time series.

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Fri Sep 5 00:14:28 CEST 2008


On Thu, 4 Sep 2008, rkevinburton at charter.net wrote:

> I have a need to build a time series and there are a couple of aspects about the time series object that are confusing me. First it seems that ts.union is not doing what I would expect. For example:
>
> x0 <- rep(0,10)
> x1 <- rep(1,10)
>
> xt0 <- ts(x0, frequency=10)
> xt1 <- ts(x1, frequency=10)
>
> st2 <- ts.union(xt0, xt1)
>> xt2
> Time Series:
> Start = c(1, 1)
> End = c(1, 10)
> Frequency = 10
>    xt0 xt1
> 1.0   0   1
> 1.1   0   1
> 1.2   0   1
> 1.3   0   1
> 1.4   0   1
> 1.5   0   1
> 1.6   0   1
> 1.7   0   1
> 1.8   0   1
> 1.9   0   1
>
>> spectrum(xt2)
> Error in plot.window(...) : need finite 'ylim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log = log) :
>  10 y values <= 0 omitted from logarithmic plot
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> 4: In xy.coords(x, y, xlabel, ylabel, log) :
>  5 y values <= 0 omitted from logarithmic plot
>
> I would expect that ts.union would concatenate the time series.

You expect wrong. ts.union() is more like cbind() for all combined time 
indexes, ts.intersect() for the intersection of time indexes.

> I would expect xt2 from above to be a time series from 1:20. If I do

This would never make sense. You have defined both xt0 and xt1 to be time 
series starting at 1(1) and ending at 1(10). Why should xt1 be shifted to 
start at 2(1) and end at 2(10)?

> xt2 <- c(xt0, xt1)
>
>> xt2
> [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
>>
>
> This seems to get rid of the time series nature of each of the objects.
>
> I can do:
>
>> xt2 <- ts(c(xt0,xt1), frequency=10)
>> xt2
> Time Series:
> Start = c(1, 1)
> End = c(2, 10)
> Frequency = 10
> [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1
>>
>
> But whst am I to interpret the start and end (c(1,1), and c(2,10)) to be?
>
> Is this the best way to concatenate two time series?

Package "zoo" has a c() command for zoo series. For your example this 
complains

R> c(as.zoo(xt0), as.zoo(xt1))
Error in rbind.zoo(...) : indexes overlap

But if a concatenation makes sense, this works

R> xt1 <- ts(x1, start = 2, frequency=10)

R> c(as.zoo(xt0), as.zoo(xt1))
  1(1)  1(2)  1(3)  1(4)  1(5)  1(6)  1(7)  1(8)  1(9) 1(10)  2(1)  2(2)  2(3)
     0     0     0     0     0     0     0     0     0     0     1     1     1
  2(4)  2(5)  2(6)  2(7)  2(8)  2(9) 2(10)
     1     1     1     1     1     1     1

R> as.ts(c(as.zoo(xt0), as.zoo(xt1)))
Time Series:
Start = c(1, 1)
End = c(2, 10)
Frequency = 10
  [1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1

hth,
Z

> Thank you.
>
> Kevin
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list