Hello, I am wondering how I should set up the tsp attribute (available through attr(x, "tsp")) of a dataset x? Let's assume that x has 100 points, and I want to set the frequency to 4. I tried: > attr(x,"tsp")<-c(1,100,4) Error in attr(x, "tsp") <- c(1, 100, 4) : invalid time series parameters specified Is there any other way to set the frequency of the data? Thanks. -Samik
The first two components of the tsp vector are in time units as mentioned in ?tsp . Thus you would want:> x <- 1:100 > tsp(x) <- c(1, 25.75, 4)but normally you don't have to set it explicitly in the first place. Just use ts:> x <- 1:100 > x.ts <- ts(x, start = 1, frequency = 4) > tsp(x.ts)[1] 1.00 25.75 4.00> x.tsQtr1 Qtr2 Qtr3 Qtr4 1 1 2 3 4 2 5 6 7 8 3 9 10 11 12 4 13 14 15 16 5 17 18 19 20 6 21 22 23 24 7 25 26 27 28 8 29 30 31 32 9 33 34 35 36 10 37 38 39 40 11 41 42 43 44 12 45 46 47 48 13 49 50 51 52 14 53 54 55 56 15 57 58 59 60 16 61 62 63 64 17 65 66 67 68 18 69 70 71 72 19 73 74 75 76 20 77 78 79 80 21 81 82 83 84 22 85 86 87 88 23 89 90 91 92 24 93 94 95 96 25 97 98 99 100 On Fri, Nov 20, 2009 at 5:53 PM, Samik Raychaudhuri <samikr at gmail.com> wrote:> Hello, > I am wondering how I should set up the tsp attribute (available through > attr(x, "tsp")) of a dataset x? Let's assume that x has 100 points, and I > want to set the frequency to 4. > I tried: >> attr(x,"tsp")<-c(1,100,4) > Error in attr(x, "tsp") <- c(1, 100, 4) : > ?invalid time series parameters specified > Is there any other way to set the frequency of the data? > Thanks. > -Samik > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
tsp is supposed to be applied to a ts object. If you are not using ts objects then that is your problem. On Fri, Nov 20, 2009 at 6:43 PM, Samik Raychaudhuri <samikr at gmail.com> wrote:> Looks like there is a difference between when I use frequency(x) vs. when I > use frequency(x.ts). If I try to get the tsp attribute of x by using attr(x, > "tsp"), it still shows up as NULL. When I looked at the code of frequency() > function (in stats), it seems to be looking at attr(x, "tsp"), which is > NULL, so the function returns 1. > Let me relate the context a bit. I am trying to use the function > forecast:nsdiffs to perform a Canova-Hansen test for a given dataset. Even > though I specify the seasonality, in an internal function, frequency(x) is > used to find seasonality, and since that value is 1 (as I explained above: > it looks at attr(x, "tsp")), the calculations do not succeed. > > On 11/20/2009 4:25 PM, Gabor Grothendieck wrote: >> >> Not for me. >> >> >>> >>> x <- 1:100 >>> x.ts <- ts(x, start = 1, frequency = 4) >>> frequency(x.ts) >>> >> >> [1] 4 >> >> >> >> >