On Fri, May 17, 2013 at 2:56 AM, Seth Myers <sjmyers3...@gmail.com> wrote:

> Let's say I would like to look inside the function corBrownian in library
> (ape).  When I type in the function name I get the following, which is not
> nearly the detail that goes into this function.  I am wondering how to
> begin cracking this function open (and others) so I can learn more about it
> and perhaps code my own corClass one day.  Thanks.
>
> > corBrownian
> function (value = 1, phy, form = ~1)
> {
>     if (!inherits(phy, "phylo"))
>         stop("object \"phy\" is not of class \"phylo\"")
>     attr(value, "formula") <- form
>     attr(value, "fixed") <- TRUE
>     attr(value, "tree") <- phy
>     class(value) <- c("corBrownian", "corPhyl", "corStruct")
>     value
> }
> <environment: namespace:ape>
>
>
Your premise is wrong! This is exactly all that goes into the corBrownian
function. All that it does is create a thing with some values and most
importantly, a set of class attributes.

 It's via that class attribute that your corBrownian objects get their
behaviour via _method_ functions.  If you download the source code for ape
from CRAN and have a look in PGLS.R you will see a bunch of functions such
as "Initialize.corPhyl" and "corMatrix.corBrownian" which are methods for
Initialize and corMatrix for corPhyl classes and corBrownian classes
respectively.

 So when you come to use one of these correlation structures, the calling
code doesn't care how the correlation structure computes its correlation
matrix, it just calls the corMatrix function and the object-oriented magic
calls the specific one for that specific class of model.

 So in ape, all the phylogenetic correlation classes are initialised with
Initialize.corPhyl and the Brownian one has corMatrix.corBrownian as the
method for computing its correlation matrix. You can see the source code
for these methods by typing their names on the R command line. Sometimes
methods aren't exported and aren't so easily visible on the command line,
leading to frustration. Download the source package for complete
satisfaction.

 The help for ?corClasses in the nlme package has a bit more help, but you
might do well to read the source code of how these methods are created in
the ape package, especially if you are going to be creating a variation on
these phylo-classes.

 Good luck. I tried and failed to write a new corClass for nlme a few years
back.

corBlimey.

Barry

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to