Jonathan:

As David said, this is a key aspect  of S4 (there are others, of
course). But it can be "simulated" in S3, I think, albeit inelegantly.
You merely have to extend the class of "object", the fitted object you
dispatch on, and then write an appropriate method for this extended
class. e.g.

## wholly untested!

class(obj)<- c("mySpecial",class(obj)) ## extend class of fitted object
predict.mySpecial<- function(object,newdata,...){
  if(inherits(newdata,"weirdClasss")){
    ... ## do something
  }
  else NextMethod() ## pass through to standard predict method
}

## Now this will work as desired:
predict(obj)

If this is obviously stupid -- or even not so obviously -- I would
appreciate someone pointing it out.

Best,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Tue, Mar 18, 2014 at 7:28 AM, Jonathan Greenberg <j...@illinois.edu> wrote:
> David:
>
> Thanks!  Is it generally frowned upon (if I'm Incorporating this into
> a package) to "override" a generic function like "predict", even if I
> plan on making it a pass-through function (same parameters, and if the
> data type doesn't match my "weird" data type, it will simply pass the
> parameters through to the generic S3 "predict")?
>
> --j
>
> On Mon, Mar 17, 2014 at 4:08 AM, David Winsemius <dwinsem...@comcast.net> 
> wrote:
>> S3 classes only dispatch on the basis of the first parameter class. That was 
>> one of the reasons for the development of S4-classed objects. You say you 
>> have the expectation that the object is of a class that has an ordinary 
>> `predict` method presumably S3 in character,  so you probably need to write 
>> a function that will mask the existing method. You would rewrite the 
>> existing test for the existence of 'newdata' and the the definition of the 
>> new function would persist through the rest of the session and could be 
>> source()-ed in further sessions.
>>
>> --
>> David.
>>
>>
>> On Mar 16, 2014, at 2:09 PM, Jonathan Greenberg wrote:
>>
>>> R-helpers:
>>>
>>> I'm having some trouble with this one -- I figure because I'm a bit of
>>> a noob with S3 classes...  Here's my challenge: I want to write a
>>> custom predict statement that is triggered based on the presence and
>>> class of a *newdata* parameter (not the "object" parameter).  The
>>> reason is I am trying to write a custom function based on an oddly
>>> formatted dataset that has been assigned an R class.  If the predict
>>> function "detects" it (class(newdata) == "myweirdformat") it does a
>>> conversion of the newdata to what most predict statements expect (e.g.
>>> a dataframe) and then passes the converted dataset along to the
>>> generic predict statement.  If newdata is missing or is not of the odd
>>> class it should just pass everything along to the generic predict as
>>> usual.
>>>
>>> What would be the best way to approach this problem?  Since (my
>>> understanding) is that predict is dispatched based on the object
>>> parameter, this is causing me confusion -- my object should still
>>> remain the model, I'm just allowing a new data type to be fed into the
>>> predict model(s).
>>>
>>> Cheers!
>>>
>>> --j
>>>
>>> --
>>> Jonathan A. Greenberg, PhD
>>> Assistant Professor
>>> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
>>> Department of Geography and Geographic Information Science
>>> University of Illinois at Urbana-Champaign
>>> 259 Computing Applications Building, MC-150
>>> 605 East Springfield Avenue
>>> Champaign, IL  61820-6371
>>> Phone: 217-300-1924
>>> http://www.geog.illinois.edu/~jgrn/
>>> AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>>>
>>> ______________________________________________
>>> 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.
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>
>
>
> --
> Jonathan A. Greenberg, PhD
> Assistant Professor
> Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
> Department of Geography and Geographic Information Science
> University of Illinois at Urbana-Champaign
> 259 Computing Applications Building, MC-150
> 605 East Springfield Avenue
> Champaign, IL  61820-6371
> Phone: 217-300-1924
> http://www.geog.illinois.edu/~jgrn/
> AIM: jgrn307, MSN: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007
>
> ______________________________________________
> 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.

______________________________________________
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