Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

r - Set frequency in xts object

I want to create an xts object in R, which I then want to decompose to seasonal and trend.

> require(xts)
> require(lubridate) 
> chicos$date <- ymd(chicos$date)
> ctr.ts <- xts(chicos[, 7], order.by = chicos[, 8], frequency = 365)
> plot(ctr.ts, main="Meaningful title")

enter image description here

When I try to decompose it, I get this error message..

> plot(decompose(ctr.ts))
Error in decompose(ctr.ts) : time series has no or less than 2 periods

My observations are daily, and span from 2014-12-01 to 2015-02-25. Did I set the wrong frequency?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For the frequency of the time series of type xts: By default xts has a daily frequency, So you don't need to include any frequency if it is daily:

 ctr.xts <- xts(chicos[, 7], order.by = chicos[, 8])

The R function decompose() works only with objects of type ts. So, you may like to convert the xts object to ts by issuing the following lines:

attr(ctr.xts, 'frequency') <- 7  # Set the frequency of the xts object to weekly
periodicity(ctr.xts)             # check periodicity: weekly 
plot(decompose(as.ts(ctr.xts)))  # Decompose after conversion to ts

Also, you may like to try different frequencies:

  • monthly: 12
  • yearly: 365

Hope this may help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...