[R] Understanding TS objects
Achim.Zeileis at uibk.ac.at
Tue Mar 13 15:49:13 CET 2018
On Tue, 13 Mar 2018, JEFFERY REICHMAN wrote:
> R Help Community
> I'm trying to understand time series (TS) objects. Thought I understood
> but recently have run into a series of error messages that I'm not sure
> how to handle. I have 15 years of quarterly data and I typically create
> a TS object via something like...
> data.ts <- ts(mydata, start = 2002, frequency = 4)
> this create a matric as opposed to a vector object
This depends on what "mydata" is which you haven't shown...
If "mydata" is a univariate vector, everything works ok:
mydata <- rnorm(15 * 4)
data.ts <- ts(mydata, start = 2002, frequency = 4)
data.stl <- stl(data.ts, "periodic")
However, if "mydata" is a matrix, e.g., a 3-column matrix in the example
mydata <- matrix(rnorm(15 * 4 * 3), ncol = 3)
then the error occurs.
Furthermore, the same problem will occur if mydata is 1-column matrix or a
1-column data frame.
> as I receive a univariate error when I try to decompose the data using
> the STL function
> data.stl <- stl(data.ts, "periodic")
> Error in stl(data.ts, "periodic") : only univariate series are allowed
> ok so
>  FALSE
This is always FALSE for a "ts" object, even if it is univariate, because
it has further attributes, namely the time-series properties (tsp).
> so to convert to a vector I'll use
> data.ts <- as.vector(data.ts)
This will drop the "ts" class.
The cleanest way is probably to create a vector "mydata" and then the
> but then I lose the frequency as the periods as the data becomes frequency = 1
> data.ts <- stl <- stl(data.ts, "periodic")
> Error in stl(data.ts, "periodic") :
> series is not periodic or has less than two periods.
> So am I missing a parameter or is there a more general/proper way to create a time series object? First time I've run into this problem . I can always decompose via an alternative methods so there are work arounds. But just trying to understand what I'm not doing programmatically at this point.
> Jeff Reichman
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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