user3642360
user3642360

Reputation: 792

decomposition of xts weekly time series in R

I have a weekly time series data x (x is an 'xts' object) as follows:

date    value
2/1/19  3801
2/8/19  5114
2/15/19 6437
2/22/19 5772
3/1/19  4878
3/8/19  5913
3/15/19 7466
3/17/19 1630

I want to decompose the time series data. But I am getting error if I use:

plot(decompose(x))
Error in decompose(x) : time series has no or less than 2 periods

I found that:

frequency(x)
[1] 1

So I did this:

attr(x, 'frequency') <- 7

But I am still getting error to decompose:

plot(decompose(ts(x,frequency = 7)))
Error in decompose(ts(x, frequency = 7)) : 
  time series has no or less than 2 periods

Any help will be highly appreciated. TIA.

Upvotes: 0

Views: 449

Answers (1)

RaphaelS
RaphaelS

Reputation: 869

Hm maybe try a different value for frequency. From the ts help page:

"The value of argument frequency is used when the series is sampled an integral number of times in each unit time interval. For example, one could use a value of 7 for frequency when the data are sampled daily, and the natural time period is a week, or 12 when the data are sampled monthly and the natural time period is a year. Values of 4 and 12 are assumed in (e.g.) print methods to imply a quarterly and monthly series respectively."

So for your data the following might work

decompose(ts(x,frequency = 4))

Or

decompose(ts(x,frequency = 52))

as weekly

Upvotes: 1

Related Questions