Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I am trying to use a function from the forecast package, seasonaldummy(), which requires a time series object as input, but I have an xts object, call it x. So first, is it correct that xts objects can not be directly passed to functions that require a ts object?

I then tried to coerce it to a ts object. I tried this by as.ts(x). The function still produces an error.

I looked at the xts vignette, and in particular the reclass function, but all I could understand from that was how to coerce non xts objects to xts. It appears I have to do the opposite, and that as.ts(x) does not work.

I have put a MWE below, where the last two lines generate an error.

> a = rnorm(20)    
> dt = seq(as.POSIXct("2010-03-24"),by="days",len=20)
> library(xts)   
> a = xts(a,dt)   
> library(forecast)   
> seasonaldummy(a)

Error in seasonaldummy(a) : Not a time series

> seasonaldummy(as.ts(a))

Error in seasonaldummy(as.ts(a)) : subscript out of bounds
share|improve this question

1 Answer

To use seasonal dummy variables, the period of seasonality must be an integer. When you convert a to a time series using as.ts(a), the period is set to 1/86400 (seconds in a day). So if you want to use seasonal dummy variables you will have to specify what the most appropriate period is first. For example, if you want to use a day-of-week seasonality, use

seasonaldummy(ts(a,f=7))
share|improve this answer
How to convert from xts to ts if my original xts doesn't have exactly the same number of data per cycle? For instance if I have daily data every weekday => every year there is not the same number of daily data... is there a best practice around that? – RockScience Feb 14 at 11:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.