Ahmed Salhin Ahmed Salhin - 2 months ago 9
R Question

Zooreg frequency warning

Suppose I have the following set of data:

date <- structure(c(1986, 1986.08333333333, 1986.16666666667), class = "yearmon")

return <- structure(c(0.000827577426231287, 0.00386371801344005, 0.00382634819565989
), .Dim = 3L, .Dimnames = list(c("1986-01", "1986-02", "1986-03"
)))


I used the following to transform the return
array
into a
zoo/zooreg
object:

zooreg(return, order.by = date)


It provides the correct output with a warning:

Jan 1986 Feb 1986 Mar 1986
0.0008275774 0.0038637180 0.0038263482



Warning message:
In zoo(data, order.by, frequency) :
“order.by” and “frequency” do not match: “frequency” ignored


The series is strictly regular and the
order.by
and
frequency
should match but I still couldn't figure out why there is a warning.

Answer

According to the documentation (?yearmon):

The "yearmon" class is used to represent monthly data. Internally it holds the data as year plus 0 for January, 1/12 for February, 2/12 for March and so on in order that its internal representation is the same as ts class with frequency = 12.

Calling:

zooreg(return, order.by = date)

is equivalent to calling

zoo(return, order.by = date, frequency = 1)

According to the documentation to zoo under Arguments::frequency :

If specified, it is checked whether order.by and frequency comply.

Hence the warning. To get rid of the warning, use

z <- zooreg(return, order.by = date, frequency = 12)

or

z <- zoo(return, order.by = date, frequency = 12)

Both of these will return an object of class zooreg:

str(z)
‘zooreg’ series from Jan 1986 to Mar 1986
  Data: Named num [1:3] 0.000828 0.003864 0.003826
 - attr(*, "names")= chr [1:3] "1986-01" "1986-02" "1986-03"
  Index: Class 'yearmon'  num [1:3] 1986 1986 1986
  Frequency: 12 

which according to the documentation (?zoo),

This is a subclass of "zoo" which relies on having a "zoo" series with an additional "frequency" attribute (which has to comply with the index of that series)

I believe this is what you want.

Note that calling with mismatched "order.by" and "frequency" using

z <- zooreg(return, order.by = date)

you get only a zoo object:

str(z)
‘zoo’ series from Jan 1986 to Mar 1986
  Data: Named num [1:3] 0.000828 0.003864 0.003826
 - attr(*, "names")= chr [1:3] "1986-01" "1986-02" "1986-03"
  Index: Class 'yearmon'  num [1:3] 1986 1986 1986
Comments