Error when trying to use stl and decompose functions in R

后端 未结 1 1675
清歌不尽
清歌不尽 2021-01-17 10:34

I have made a simple time-series, i added a little noise to a sin function and tried to decompose it using the \"stl\" and \"decompose\" function in R, while my series defin

相关标签:
1条回答
  • 2021-01-17 11:16

    Isn't it obvious from the error message:

    time series has no or less than 2 periods
    

    ? R's not telling you your data aren't periodic, just that the data you passed it have no indication that they are periodic.

    Your time series x doesn't have an periodicity from the point of view of R. It looks like you forgot to tell, or made an error in telling, ts() what the periodicity is. As you don't show how x was created, there isn't much we can do except tell you to go back and create x so that it does have >=2 periods.

    The point here is that on it's own, R can't deduce what the frequency of observation is per unit time. You have to tell ts() that information. You can do this in a number of ways:

    frequency: the number of observations per unit of time.

    deltat: the fraction of the sampling period between successive observations; e.g., 1/12 for monthly data. Only one of frequency or deltat should be provided.

    If you don't provide one of these, ts() uses the defaults frequency = 1, deltat = 1 which would indicate a time series of one observation per unit time (one per year for example).

    stl() requires a "ts" classed object - if you don't provide one, it will coerce the input data to a "ts" object via as.ts(). This function will use the defaults which I describe above.

    What I think has happened here is that you didn't realise that stl() requires a "ts" class object nor that it created an inappropriate one for your data when you just supplied the vector of observations.

    The solution would be to explicitly create the "ts" classed object via ts() and specify one of frequency or deltat.

    E.g.

    dat <- cumsum(rnorm(12*4))
    x <- ts(dat)
    stl(x, "periodic")
    xx <- ts(dat, frequency = 12)
    stl(xx, "periodic")
    

    Here I used frequency = 12 to indicate that there are 12 observations per unit time --- such as with monthly data.

    The above gives for me

    R> stl(x, "periodic")
    Error in stl(x, "periodic") : 
      series is not periodic or has less than two periods
    
    R> stl(xx, "periodic")
     Call:
     stl(x = xx, s.window = "periodic")
    
    Components
           seasonal    trend remainder
    Jan 1 -0.103529  0.55245  -0.44301
    Feb 1  0.001333  0.56981   0.86135
    Mar 1 -0.382075  0.58717   1.11162
    Apr 1  0.010552  0.59891  -1.04966
    ....
    

    For your data I suspect you want frequency = 10 given the length of the time series; that would say that you have ten observations per year. If the series has more observations per year, say 12 for monthly data, but you don;t have the last two or first two months (i.e. there is no missing data, NAs) you just started (ended) later (earlier) in the year and hence don't have a full years worth of data at one or both ends of the series.

    0 讨论(0)
提交回复
热议问题