Decomposition stl () will not accept one-dimensional ts object?

Am I having problems with the stl () time series decomposition function in R telling me that my ts object is not one-dimensional, when is it really?

tsData <- ts(data = dummyData, start = c(2012,1), end = c(2014,12), frequency = 12) > tsData Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2012 22 26 34 33 40 39 39 45 50 58 64 78 2013 51 60 80 80 93 100 96 108 111 119 140 164 2014 103 112 154 135 156 170 146 156 166 176 193 204 > class(tsData) [1] "ts" > stl(tsData, s.window = "periodic") Error in stl(tsData, s.window = "periodic") : only univariate series are allowed > dput(dummyData) structure(list(index = c(22L, 26L, 34L, 33L, 40L, 39L, 39L, 45L, 50L, 58L, 64L, 78L, 51L, 60L, 80L, 80L, 93L, 100L, 96L, 108L, 111L, 119L, 140L, 164L, 103L, 112L, 154L, 135L, 156L, 170L, 146L, 156L, 166L, 176L, 193L, 204L)), .Names = "index", class = "data.frame", row.names = c(NA, -36L)) 

Does anyone know how to fix this problem?

+4
source share
5 answers

I am not 100% sure about the exact cause of the problem, but you can fix this by passing dummyData$index to ts instead of the whole object:

 tsData2 <- ts( data=dummyData$index, start = c(2012,1), end = c(2014,12), frequency = 12) ## R> stl(tsData2, s.window="periodic") Call: stl(x = tsData2, s.window = "periodic") Components seasonal trend remainder Jan 2012 -24.0219753 36.19189 9.8300831 Feb 2012 -20.2516062 37.82808 8.4235219 Mar 2012 -0.4812396 39.46428 -4.9830367 Apr 2012 -10.1034302 41.32047 1.7829612 May 2012 0.6077088 43.17666 -3.7843705 Jun 2012 4.4723800 45.22411 -10.6964877 Jul 2012 -7.6629462 47.27155 -0.6086074 Aug 2012 -1.0551286 49.50673 -3.4516016 Sep 2012 2.2193527 51.74191 -3.9612597 Oct 2012 7.3239448 55.27391 -4.5978509 Nov 2012 18.4285405 58.80591 -13.2344456 Dec 2012 30.5244146 63.70105 -16.2254684 

...


I assume that when you pass data.frame to the data ts argument, some additional attributes are carried over, and although this is usually not a problem with many functions that take a ts class object (one-dimensional or another), apparently this problem for stl .

 R> all.equal(tsData2,tsData) [1] "Attributes: < Names: 1 string mismatch >" [2] "Attributes: < Length mismatch: comparison on first 2 components >" [3] "Attributes: < Component 2: Numeric: lengths (3, 2) differ >" ## R> str(tsData2) Time-Series [1:36] from 2012 to 2015: 22 26 34 33 40 39 39 45 50 58 ... ## R> str(tsData) 'ts' int [1:36, 1] 22 26 34 33 40 39 39 45 50 58 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr "index" - attr(*, "tsp")= num [1:3] 2012 2015 12 

Edit:

Having carefully studied this, I think that the problem is related to the dimnames attribute, which is transferred from dummyData when it is transferred as a whole. Pay attention to this excerpt from stl body:

 if (is.matrix(x)) stop("only univariate series are allowed") 

and from the definition of matrix :

is.matrix returns TRUE if x is a vector and has the attribute "dim" length 2) and FALSE otherwise

therefore, if you pass stl one-dimensional time series (the original tsData ), then with respect to this function a vector with an attribute of length 2 dimnames (i.e. a matrix ) is not a one-dimensional series. It seems a little strange to do error handling this way, but I'm sure the author of this function had a very good reason.

+5
source

To avoid such problems or errors, try making a one-dimensional time series only by forming raw points or data values โ€‹โ€‹by calling the ts () function.

Better to say, you should always put only the values โ€‹โ€‹of your variable not the entire structure of the variable. Let me explain this with a slightly simple example:

Imagine that you have a variable X, which is a vector (most likely, imported or generated from other data sources) of size 100x1, that is, it contains 100 values โ€‹โ€‹or data points. If you want to make a one-dimensional time series from this vector, the wrong way to do it is the same as for your case:

ts (X, frequency = 24)

BE CAREFUL, THE RIGHT way to make it look like this:

ts (X [1: 100], frequency = 24)

or even so:

ts (X [1: 100.1], frequency = 24)

I hope, my dear friend, that you can avoid this the next time you need to do a one-dimensional time series. !!

+9
source

I had the same error, and as mentioned in nrussell, the problem was that I was going through a time series, which was also a matrix.

(However, the index $ index did not work for me, and R complained that the ts object should have one or more observations. I am new to R and do not know why this is, but for me the following approach worked)

you can fix it with:

 dummyVector = as.vector(t(dummyData)) 

And then keep getting stl:

 tsData = ts(dummyVector, start=2012, end=(2014, 12), frequency = 12) stl = stl(tsData, "periodic") 

If you use R Studio, you can see that now your timers are listed in the section

Time-Series [1:36] from 2012 to 2015: yourData p>

whereas earlier, most likely, it was indicated as

int [1: 3, 1:12] yourData p>

0
source

The problem may be that you are not specifying end = c (yyyy, mm).

If you use decomposition, you do not need to specify end =. If you go to stl and use some old code, you need to add this parameter (if you did not use it with decomposition).

This fixed a โ€œone-dimensionalโ€ error for me.

0
source

Another way to fix the problem with time series data:

 stl(tsData[, 1], s.window = "periodic") 

Please compare

 str(tsData) 

from

 str(tsData[, 1]) 

to see the difference.

0
source

Source: https://habr.com/ru/post/915150/


All Articles