0

I want to apply decompose function in R on weekly collected data.For instance I am giving the example on that I have worked along with the error I am getting:

>v<-c(50,45,23,21,32,24,42)
>vtimeseries<-ts(v)
>vtimeseries
Time Series:
Start = 1 
End = 30 
Frequency = 1 
>decompose(vtimeseries)
Error in decompose(vts) : time series has no or less than 2 periods

Can anyone please help me out of this problem.And also give some light on how to set the frequency value in the ts(). I want to decompose the time series data as

Monday      Tuesday      Wednesday     Thursday      Friday       Saturday     Sunday
...          ....          ....            ...            ...           ...     ...

My main purpose of doing so is to find the seasonal and trend component for the time series data.

Saurabh
  • 867
  • 3
  • 13
  • 28
  • your question is unclear? Are you trying to split your data into 7 columns - one for each weekday? – CHP Feb 13 '13 at 06:07
  • Not exactly....I wanted to implement ts() function and decompose function for that I have taken some random set of data.My question is that how to use ts() function to make it time series data like weekly(in my case) and when we apply decompose function on it then the above error will not come. Hope I am clear now – Saurabh Feb 13 '13 at 06:13
  • have you tried `ts(v, frequency=7)`. It's still unclear what you are trying to achieve without proper data. Perhaps giving actual data sample might be helpful to prospective helpers – CHP Feb 13 '13 at 06:31
  • @geektrader.... I have tried that.But I want the output of ts() as whatever I have given above Like Monday Tuesday .... along with their values. – Saurabh Feb 13 '13 at 07:01
  • 2
    You can name the values: `ts(c(Monday=50,Tuesday=45,Wednesday=23,Thursday=21,Friday=32,Saturday=24,Sunday=42), frequency=7)`. For `decompose`, you need the `frequency` to be larger than 1 (the default), and you need more than one period (more than one week of data). – Vincent Zoonekynd Feb 13 '13 at 10:28

2 Answers2

1

The example given by Pramit Choudhary above seems not to work anymore due to an NA column in the GOOG xts which causes to.monthly(GOOG)to throw up an error Borrowing code from Removing NA columns in xts

getSymbols("GOOG", src="google", from = from.dat, to = to.dat)
GOOG <- GOOG[,-which(apply(is.na(GOOG), 2, all))]
mGoog <- to.monthly(GOOG)

seems to sort the problem out.

Community
  • 1
  • 1
mercergeoinfo
  • 379
  • 2
  • 3
  • 14
0

If you are looking for an example to wrap your head around relevance of frequency and decompose, try the following example. Try changing the 'frequency' value. It will help you realize what exactly is happening.

    library(quantmod)
    from.dat <- as.Date("01/01/08", format="%m/%d/%y")
    to.dat <- as.Date("12/31/13", format="%m/%d/%y")
    getSymbols("GOOG", src="google", from = from.dat, to = to.dat)
    mGoog <- to.monthly(GOOG)
    googOpen <- Op(mGoog)
    ts1 <- ts(googOpen,frequency=12)
    plot(ts1,xlab="Years+1", ylab="GOOG")
    plot(decompose(ts1),xlab="Years+1")
Pramit
  • 1,373
  • 1
  • 18
  • 27