[R] Nine questions about methods and generics

Michael Dewey info at aghmed.fsnet.co.uk
Thu Dec 6 13:17:58 CET 2007


I have a series of question about methods and generics.
The questions are interspersed in some text which explains
what I want to do, how it works now and why I do not
understand well why it works. Questions are written
Q1 and so on up to Q9 and always start a new line.

The concrete problem is this: it has become customary
after a meta-analysis to quote various statistics
to give a picture of the degree of heterogeneity.
Initially I wrote a function which calculated these given
the heterogeneity chi-squared Q and the number of studies k.
It then occurred to me that it would be convenient to
have functions which take the objects returned by
the various functions in the package rmeta (available
from CRAN) and perform the calculation on them.
So after plagiarising some ideas from rmeta
I wrote

hetero <- function(x, ...) {
    UseMethod("hetero")
}

Q1 - have I created a generic called hetero? If so
what is the correct idiom: created, declared, ...?
Q2 - if I go class(hetero) it returns "function" rather
than "generic". Is that what I should expect?

I then called my original function hetero.default

hetero.default <- function(Q = NULL, k = NULL, conflevel = 0.95) {
# some code left out here which computes values in the list res
    class(res) <- "meta.hetero"
    res
}

Q3 was it a good idea to have it return an object of class
"meta.hetero" or would it have been better to call it "hetero"

now if I go
hetero(14.4, 24)
I get what I expected

I then provide a new function

hetero.meta.summaries <- function(obj, ...) {
# some code to calculate Q and k
    res <- hetero.default(Q, k, ...)
    res
}
which also seems to do what I wanted. There are also
similar functions hetero.meta.MH and hetero.meta.DSL
to operate on the other classes of object returned in rmeta.

The problem is that I do not really understand why it all works
and suspect it could easily stop working.

At this point I decided that if all else fails I should read the
documentation. The manual pages (the things you get
with ?UseMethod) describe what the functions do but
do not give me a series of steps to salvation.
I found S Programming (V&R) much clearer now I knew where to
look but I think V&R underestimate the depths of my
ignorance.

Q4 V&R seems to suggest that I should have called the first
argument to hetero, hetero.default and hetero.meta.summaries
the same (possibly x or object) yet it seems to work.
Q5 Does it matter?
If it does matter which should I choose?

I also decide to look at R-extensions which seems
to suggest that I should be using NextMethod and
also has dire warnings about having different argument
lists to the various methods.
I found R-extensions much the least helpful
of the three sources I tried.

Q6 should I be using NextMethod?
Q7 if what I have created are methods which are
displayed by methods("hetero") why is it UseMethod not
usemethod? (I note that ?Methods tells me about something
different from ?methods.)
Q8 I have used a ... argument for hetero.meta.summaries
but not for hetero.default. Was that wise? R-extensions
has some stern things to say here which I do not really
understand.
Q9 is there somewhere else I could have looked
bearing in mind my comments above about the
manual pages, V&R and R-extensions? Or shall
I just keep reading V&R until the penny drops?

For the record:

S Programming (V&R) means
@BOOK{venables00,
   author = {Venables, W N and Ripley, B D},
   year = 2000,
   title = {S programming},
   publisher = {Springer-Verlag},
   address = {New York},
   keywords = {statistics general; software}
}
The heterogeneity statistics are explained in
@ARTICLE{higgins02,
   author = {Higgins, J P T and Thompson, S G},
   year = 2002,
   title = {Quantifying heterogeneity in a meta--analysis},
   journal = {Statistics in Medicine},
   volume = 21,
   pages = {1539--1558},
   keywords = {meta-analysis, heterogeneity}
}

I am using 2.6.1 under XP Professional.
My knowledge of computer science is frozen at about 1975.



Michael Dewey
http://www.aghmed.fsnet.co.uk



More information about the R-help mailing list