[R] Overriding predict based on newdata...

David Winsemius dwinsemius at comcast.net
Mon Mar 17 10:08:19 CET 2014


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: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007
> 
> ______________________________________________
> R-help at 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




More information about the R-help mailing list