[R] Ask if an object will respond to a function or method

David Winsemius dwinsemius at comcast.net
Fri Apr 1 01:26:29 CEST 2016


> On Mar 31, 2016, at 1:00 PM, Paul Johnson <pauljohn32 at gmail.com> wrote:
> 
> In the rockchalk package, I want to provide functions for regression
> objects that are "well behaved." If an object responds to the methods
> that lm or glm objects can handle, like coef(), nobs(), and summary(),
> I want to be able to handle the same thing.
> 
> It is more difficult than expected to ask a given fitted model object
> "do you respond to these functions: coef(), nobs(), summary()." How
> would you do it?
> 
> I tried this with the methods() function but learned that all methods
> that a class can perform are not listed.  I'll demonstrate with a
> regression "zz" that is created by the example in the plm package.
> The coef() function succeeds on the zz object, but coef is not listed
> in the list of methods that the function can carry out.
> 
>> library(plm)
>> example(plm)
> 
>> class(zz)
> [1] "plm"        "panelmodel"
>> methods(class = "plm")
> [1] ercomp          fixef           has.intercept   model.matrix
> [5] pFtest          plmtest         plot            pmodel.response
> [9] pooltest        predict         residuals       summary
> [13] vcovBK          vcovDC          vcovG           vcovHC
> [17] vcovNW          vcovSCC
> see '?methods' for accessing help and source code
>> methods(class = "panelmodel")
> [1] deviance      df.residual   fitted        has.intercept index
> [6] nobs          pbgtest       pbsytest      pcdtest       pdim
> [11] pdwtest       phtest        print         pwartest      pwfdtest
> [16] pwtest        residuals     terms         update        vcov
> see '?methods' for accessing help and source code
>> coef(zz)
>   log(pcap)      log(pc)     log(emp)        unemp
> -0.026149654  0.292006925  0.768159473 -0.005297741
> 
> I don't understand why coef(zz) succeeds but coef is not listed as a method.

There is a coef.default method. (Also an S$  coef,ANY-method )

> 
> Right now, I'm contemplating this:
> 
> zz1 < - try(coef(zz))
> if (inherits(zz1, "try-error")) stop("Your model has no coef method")
> 
> This seems like a bad workaround because I have to actually run the
> function in order to find out if the function exists. That might be
> time consuming for some summary() methods.
> 
> pj
> 
> -- 
> Paul E. Johnson
> Professor, Political Science        Director
> 1541 Lilac Lane, Room 504      Center for Research Methods
> University of Kansas                 University of Kansas
> http://pj.freefaculty.org              http://crmda.ku.edu
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list