Could generic functions check different S3 methods for an object when one of them produces an error?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Could generic functions check different S3 methods for an object when one of them produces an error?

IAGO GINÉ VÁZQUEZ
Hi,


Let's say one has an object with multiple classes, and a generic function to apply to it has associated S3 methods for more than one of those classes. Further, the method it chooses (I do not know how; some order in the class vector?) is not the suitable one and it produces an error. Would there be some way to make the generic function to choice the correct method, or in case that for any method taken it produces an error, to try another one.


For example (commented in detail here<https://github.com/tidymodels/broom/issues/714>):


# object with multiple classes: the output of function `geepack::geeglm`. The output of `class(object)`:

```

[1] "geeglm" "gee" "glm" "lm"

```

The generic function: `stats::confint`.

The S3 method chosen: `confint.glm`. It produces an error. The correct method in this case would be `broom:::confint.geeglm`.


Thank you!

Iago

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Could generic functions check different S3 methods for an object when one of them produces an error?

Fox, John
Dear Iago,

The R S3 object system works as expected here, using the first available method processing the class vector from left to right. The problem is that the broom package doesn't export the confint.geeglm() method but rather reserves it for internal use. I can't think why the package authors chose to do that but you could ask them. The following therefore works (following on with your example):

> confint.geeglm <- broom:::confint.geeglm
> confint(geefit)
                    lwr         upr
(Intercept) 3607.595981 5204.790222
Frost         -2.723317    6.097207
Murder       -84.166856   38.838155

I hope this helps,
 John

  -------------------------------------------------
  John Fox, Professor Emeritus
  McMaster University
  Hamilton, Ontario, Canada
  Web: http::/socserv.mcmaster.ca/jfox

> On Jun 17, 2019, at 3:56 AM, IAGO GINÉ VÁZQUEZ <[hidden email]> wrote:
>
> Hi,
>
>
> Let's say one has an object with multiple classes, and a generic function to apply to it has associated S3 methods for more than one of those classes. Further, the method it chooses (I do not know how; some order in the class vector?) is not the suitable one and it produces an error. Would there be some way to make the generic function to choice the correct method, or in case that for any method taken it produces an error, to try another one.
>
>
> For example (commented in detail here<https://github.com/tidymodels/broom/issues/714>):
>
>
> # object with multiple classes: the output of function `geepack::geeglm`. The output of `class(object)`:
>
> ```
>
> [1] "geeglm" "gee" "glm" "lm"
>
> ```
>
> The generic function: `stats::confint`.
>
> The S3 method chosen: `confint.glm`. It produces an error. The correct method in this case would be `broom:::confint.geeglm`.
>
>
> Thank you!
>
> Iago
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Could generic functions check different S3 methods for an object when one of them produces an error?

Duncan Murdoch-2
In reply to this post by IAGO GINÉ VÁZQUEZ
On 17/06/2019 3:56 a.m., IAGO GINÉ VÁZQUEZ wrote:

> Hi,
>
>
> Let's say one has an object with multiple classes, and a generic function to apply to it has associated S3 methods for more than one of those classes. Further, the method it chooses (I do not know how; some order in the class vector?) is not the suitable one and it produces an error. Would there be some way to make the generic function to choice the correct method, or in case that for any method taken it produces an error, to try another one.
>
>
> For example (commented in detail here<https://github.com/tidymodels/broom/issues/714>):
>
>
> # object with multiple classes: the output of function `geepack::geeglm`. The output of `class(object)`:
>
> ```
>
> [1] "geeglm" "gee" "glm" "lm"
>
> ```
>
> The generic function: `stats::confint`.
>
> The S3 method chosen: `confint.glm`. It produces an error. The correct method in this case would be `broom:::confint.geeglm`.

There is something wrong here.  If R knows about confint.geeglm (because
broom is loaded, for example), then confint(x) should call it before it
calls confint.glm.  It will look for methods confint.geeglm, then
confint.gee, then confint.glm, then confint.lm.

However, when I run your code and load broom, I see

 > methods("confint")
[1] confint.default  confint.glm*     confint.lm       confint.lme*
confint.lmList*  confint.nls*     confint.nlsList*
see '?methods' for accessing help and source code


(with no confint.geeglm shown).  So something is wrong, probably in
broom.  As far as I can see, it never declares confint.geeglm as an
S3method in its NAMESPACE file.  (I'm looking at the devel version in
https://github.com/tidymodels/broom/blob/master/NAMESPACE).

Get that fixed, and your problem should go away.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel