stl() decomposition won't accept univariate ts object?

前端 未结 6 1462
悲&欢浪女
悲&欢浪女 2020-12-29 17:08

I\'m have issues with stl() time series decomposition function in R telling me my ts object is not univariate when it actually is?

tsData <- ts(data = dum         


        
6条回答
  •  难免孤独
    2020-12-29 17:23

    I'm not 100% sure about what the exact cause of the problem is, but you can fix this by passing dummyData$index to ts instead of the entire 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'm guessing that when you pass a data.frame to the data argument of ts, some extra attributes carry over, and although this generally doesn't seem to be an issue with many functions that take a ts class object (univariate or otherwise), apparently it is an issue 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:

    Looking into this a little further, I think the problem has to do with the dimnames attribute being carried over from the dummyData when it is passed as a whole. Note this excerpt from the body of stl:

    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 a "dim" attribute of length 2) and FALSE otherwise

    so although you are passing stl a univariate time series (the original tsData), as far as the function is concerned, a vector with a length 2 dimnames attribute (i.e. a matrix) is not a univariate series. It seems a little strange to do error handling in this way, but I'm sure the author of the function had a very good reason for this.

提交回复
热议问题