[R] Overriding predict based on newdata...

Jonathan Greenberg jgrn at illinois.edu
Tue Mar 18 15:28:18 CET 2014


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 <dwinsemius at 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: 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
>



-- 
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




More information about the R-help mailing list