Skip to content

question about trailing arguments in an S4 method

5 messages · Brian Ripley, Seth Falcon, Byron Ellis +1 more

#
I'm trying to add arguments to the AIC method
for some classes -- things like
  weights=TRUE to calculate AIC weights
  corr=TRUE, nobs to calculate AICc
  delta=TRUE  to put a delta-AIC column in the output.

 The problem is that AIC is defined as
AIC(object, ..., k=2)  where k is the constant associated
with the penalty term and ... is a list of objects
that will have their AICs calculated and listed.
Thus I'm not allowed (I think) to extend the definition to

AIC(object, ..., nobs, corr=FALSE, delta=FALSE, weights=FALSE, k=2)

  An ugly solution to this is to go through the ... list and
pull out the values of any of the other arguments I want, and
then set them to NULL, leaving the list composed only of
additional objects to calculate the AIC for.  But it seems
really ugly ...

   Any ideas for a better way to do this would be
appreciated.  (Please be gentle if possible, I'm a bit out
of my depth with S4 ...)

  cheers
    Ben Bolker


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 254 bytes
Desc: OpenPGP digital signature
Url : https://stat.ethz.ch/pipermail/r-devel/attachments/20061216/4cd5425d/attachment.bin
#
I don't see how this can work, in S3 or S4.  Callers of AIC are entitied 
to expect it to behave as described on the help page, and so to be able to 
pass objects called (e.g.) 'delta' and get back exactly the value 
mentioned.

For what you seem to want to do, I think you need your own generic.
On Sat, 16 Dec 2006, Ben Bolker wrote:

            

  
    
#
Ben Bolker <bolker at zoo.ufl.edu> writes:
I think you can add args before the '...'.  However, you will need to
provide values for them in all calls or else they will steal a fit
from the '...' args.

I don't understand really what you are trying to achieve, so this may
not be helpful.  Nevertheless, here is a dummy example that you may
find useful.


setClass("Foo",
         representation=representation(
           fit="lm"))

setMethod("AIC", "Foo",
          function(object, weights=TRUE, delta=TRUE, ..., k=2) {
              ans <- AIC(object=object at fit, k=k, ...)
              args1 <- list(weights=weights, delta=delta, k=k)
              cat("args\n")
              print(args1)
              cat("answer:\n")
              print(ans)
          })

example("AIC")
foo <- new("Foo", fit=lm1)
AIC(foo)
## AIC(foo, lm1)   # this doesn't work, lm1 gets picked up as the weights arg
AIC(foo, weights=FALSE, delta=TRUE, lm1, lm1, lm1)



+ seth
#
He wants to specify arguments to AIC that act like the "k" argument
and is thinking of faking it by, essentially, doing another round of
argument matching on ...

I think that if you define an S4 generic that only dispatches on the
first argument you may be able to do it for a specific method. On the
bright side, the new documentation for AIC would take precedence over
the stats documentation so the users couldn't actually complain about
the method not matching the documentation.

For flowCore we wanted to use filter, which unfortunately assumes that
the only thing I would ever want to filter is a time series and isn't
even an S3 generic, and had to do something similar by specifying
"useAsDefault=FALSE" in the generic. It seems to have broken
dispatching to the original filter function though, which is something
we'll have to fix.
On 12/16/06, Seth Falcon <sfalcon at fhcrc.org> wrote:

  
    
2 days later
#
Byron Ellis <byron.ellis <at> gmail.com> writes:
I think I'll bite the bullet and define a separate
"AICtab" function that takes a list of objects and
produces an AIC table.

  thanks, all

   Ben  Bolker