# [R] window?

R. Michael Weylandt michael.weylandt at gmail.com
Wed Nov 9 04:40:13 CET 2011

```Like Denis said, you are asking ts to do things that don't make sense;
in particular, some of your statements suggest you don't fully
understand what window does or what its frequency argument does.
Specifically, when you set frequency = 1 in window, that doesn't mean
take a window and treat it as if it has frequency 1; rather take the
subseries corresponding to yearly observations. Since 53 is prime,
there is no regular subseries you can extract with window() other than
the original series and the yearly series.  ts objects are required to
have a frequency so statements like "now that I am taking a subset
there is no frequency" don't really make sense.

Take a look at these examples:

## Create some working data
ds.53 <- ts(rnorm(53*2), frequency=53, start=c(2000,10))
ds.48 <- ts(rnorm(48*2), frequency = 48, start = c(2000,10))

## These all work
window(ds.53, frequency = 1) # Returns elements 1 & 54 of ds.53
window(ds.53, frequency = 53) # Returns every element of ds.53

window(ds.48, frequency = 1) # Returns elements 1 & 54 of ds.53
window(ds.48, frequency = 12) # Returns elements seq(1, 48, by = 4) of ds.48
window(ds.48, frequency = 48) # Returns every element of ds.48

## These don't
window(ds.53, frequency = 7)
window(ds.48, frequency = 9)

Here's how you could do the same with xts.

library(xts)
library(forecast)
x = xts(rnorm(53*2), Sys.Date() + 365*seq(0, 2, by = 1/53))
ets(x) # Auto-conversion to ts

Michael

On Tue, Nov 8, 2011 at 8:19 PM, Kevin Burton <rkevinburton at charter.net> wrote:
> The problem is when I use the window function an try to extract a subset of
> the time series an specify the frequency as 1 (not only will ets not take a
> time series with a frequency greater than 24, now that I am taking a subset
> there is no frequency so I would like to set it to 1 (which is one of the
> arguments to the window function) but it does not produce what I expect.
> That is the problem.  I fail to see the relationship of the discussion of
> what frequency is and how to use the forecast package with this problem.
>
> -----Original Message-----
> From: Dennis Murphy [mailto:djmuser at gmail.com]
> Sent: Tuesday, November 08, 2011 6:20 PM
> To: Kevin Burton
> Cc: R. Michael Weylandt; r-help at r-project.org
> Subject: Re: [R] window?
>
> The ets() function in the forecast package requires either a numeric vector
> or a Time-Series object (produced from ts()). The frequency argument in ts()
> refers to the time duration between observations; e.g., frequency = 7 means
> that the data are weekly; frequency = 12 means that the data are monthly;
> frequency = 4 means that the data are quarterly. You can see this from the
> examples on the help page of ts:
> ?ts at the R prompt.
>
> The example associated with the forecast::ets() function uses the
> USAccDeaths data:
>
> data(USAccDeaths)
> USAccDeaths   ## monthly data for six years
> # Simulate the same structure with ts:
> u <- ts(rnorm(72), start = c(1973, 1), frequency = 12) u
>
> # Evidently you want to produce a multivariate series; # here's one way with
> monthly frequency:
> v <- ts(matrix(rnorm(106), ncol = 2), start = c(2001, 1), frequency = 12) v
>
> Is that more or less what you were after?
>
> Dennis
>
> On Tue, Nov 8, 2011 at 2:04 PM, Kevin Burton <rkevinburton at charter.net>
> wrote:
>> I expect the frequency to be set to what I set it at and the window to
>> return all of the data in the window from the original time series.
>> The error is not because it is prime. I can generate a time series
>> with just 52 values (or 10) and it still occurs. I am building these
>> objects for use with the 'forecast' packages and one of the methods
>> 'ets' cannot handle a frequency above 24 so I set it (or try to) to 1.
>> Will 'window' take z zoo or xts object? Can I convert from zoo or xts to
> ts?
>>
>> -----Original Message-----
>> From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
>> Sent: Tuesday, November 08, 2011 2:28 PM
>> To: Kevin Burton
>> Cc: r-help at r-project.org
>> Subject: Re: [R] window?
>>
>> I'm not entirely sure that your request makes sense: what do you
>> expect the frequency to be? It makes sense to me as is...Might your
>> troubles be because
>> 53 is prime?
>>
>> More generally, most people don't like working with the raw ts class
>> and prefer the zoo or xts packages because they are much more pleasant
>> for most time series work. You might want to take a look into those.
>>
>> Michael
>>
>> On Tue, Nov 8, 2011 at 3:18 PM, Kevin Burton
>> <rkevinburton at charter.net>
>> wrote:
>>> This doesn't seem to work:
>>>
>>>
>>>
>>>> d <- rnorm(2*53)
>>>
>>>> ds <- ts(d, frequency=53, start=c(2000,10))
>>>
>>>> dswin <- window(ds, start=c(2001,1), end=c(2001,10), frequency=1)
>>>
>>>> dswin
>>>
>>> Time Series:
>>>
>>> Start = 2001
>>>
>>> End = 2001
>>>
>>> Frequency = 1
>>>
>>> [1] 1.779409
>>>
>>>
>>>
>>>> dswin <- window(ds, start=c(2001,1), end=c(2001,10))
>>>
>>>> dswin
>>>
>>> Time Series:
>>>
>>> Start = c(2001, 1)
>>>
>>> End = c(2001, 10)
>>>
>>> Frequency = 53
>>>
>>>  [1]  1.7794090  0.6916779 -0.6641813 -0.7426889 -0.5584049
>>> -0.2312959
>>>
>>> [7] -0.0183454 -1.0026301  0.4534920  0.6058198
>>>
>>>>
>>>
>>>
>>>
>>> The problem is that when the frequency is specified only one value
>>> shows up in the window. When no frequency is specified I get all 10
>>> values but now the time series has a frequency that I don't want.
>>>
>>>
>>>
>>>
>>>
>>>
>>> Kevin
>>>
>>>
>>>
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help