s-news
[Top] [All Lists]

Re: class and inheritance

To: "Tim Hesterberg" <timh@insightful.com>, "Russell Ivory" <Russell.Ivory@MerrickBank.com>
Subject: Re: class and inheritance
From: "Michael Camilleri" <MichaelCamilleri@branz.co.nz>
Date: Wed, 7 Feb 2007 08:38:50 +1300
Cc: <s-news@lists.biostat.wustl.edu>
In-reply-to: <SEWINEXCH00xzmWecm9000001bb@sewinexch00.insightful.com>
References: <C8D9459E0E12A04D9CD897043F52F74903F461AE@MBCEXCHANGE.MBC.com> <SEWINEXCH00xzmWecm9000001bb@sewinexch00.insightful.com>
Thread-index: AcdKJm0AQHT5u0IIQ5yE1jynRHvrsQ==
Thread-topic: class and inheritance
If all you are doing is trying to handle missing values in analysis
functions then you might be best creating your own missing value
functions for use in model calls, and just write your own version of
cor() or other functions that don't use the na.action parameter. 

Otherwise, depending on what you are trying to do the S4 or S3 classes
may work best.

S+ has a pframe object that is derived from the data frame object which
might make for a quick and easy work around. A pframe object is a data
frame with additional user defined attributes, that is automatically
coerced to a data frame in most S+ function calls, so most generic
functions work on it as is, and you can define class specific generic
function calls easily.

IMO, if all you want to do is store some extra parameters with your
objects, and override some of the generic functions the S3 way using
pframe objects is easier. The S4 way is better if you are creating
complex objects, for example a suite of custom modelling objects with
new types of generic functions. There is a big learning curve into the
S4 classes. You will need to read the manual and tutorials, and maybe
get a book like "Programming with Data" by Chambers.

You can create your own pframe class like this so that S+ will look for
generic functions for yourClassName:

        setOldClass(c("yourClassName", 'pframe','data.frame'))

then define any class specific generic function like this

        summary.yourClassname <- function(x).....

Then make object x of class yourClassName

        oldClass(x) <- c("yourClassName", oldClass(x))

You will also need to define extraction operators like

"[.yourClassname" = function(x, ..., drop = T)
{
#Store the parameters
        pp <- parameters(x)
# Extract data as a data frame
        x <- NextMethod("[")
        if(is.data.frame(x)) {
#Restore the parameters and convert back to a pframe object
                parameters(x) <- pp
        }
        x
}

Michael

<Prev in Thread] Current Thread [Next in Thread>