2

I am new to R and statistics. So this question might be a little stupid, but I was wondering if there is any difference between predict() and predict.lm() in R? I think they are the same, but then if they were, why two different functions?

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50

2 Answers2

9

If the first argument to predict has class "lm" then there will be no difference. R generic functions such as predict are dispatched to class-specific versions based on the name of the class. You can see what version of predict exist in the loaded namespaces by executing:

methods(predict)

I have a large number of packages loaded at the moment so the list is fairly long:

> methods(predict)
 [1] predict.ar*                predict.areg              
 [3] predict.areg.boot          predict.Arima*            
 [5] predict.arima0*            predict.bj*               
 [7] predict.bs*                predict.bSpline*          
 [9] predict.coxph*             predict.coxph.penal*      
[11] predict.cph*               predict.dataRep           
[13] predict.glm                predict.Glm*              
[15] predict.glmmPQL*           predict.glmtree*          
[17] predict.gls*               predict.Gls*              
[19] predict.gnls*              predict.goodfit*          
[21] predict.HoltWinters*       predict.lda*              
[23] predict.lm                 predict.lme*              
[25] predict.lmList*            predict.lmtree*           
[27] predict.loess*             predict.lqs*              
[29] predict.lrm*               predict.mca*              
[31] predict.mlm                predict.modelparty        
[33] predict.nbSpline*          predict.nlme*             
[35] predict.nls*               predict.npolySpline*      
[37] predict.ns*                predict.ols*              
[39] predict.orm*               predict.party*            
[41] predict.pbSpline*          predict.plm*              
[43] predict.polr*              predict.poly              
[45] predict.polySpline*        predict.ppolySpline*      
[47] predict.ppr*               predict.prcomp*           
[49] predict.princomp*          predict.psm*              
[51] predict.pspline*           predict.qda*              
[53] predict.rlm*               predict.Rq*               
[55] predict.smooth.spline*     predict.smooth.spline.fit*
[57] predict.StructTS*          predict.survreg*          
[59] predict.survreg.penal*     predict.transcan  
Thomas
  • 43,637
  • 12
  • 109
  • 140
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • If the answer is simple, can I ask what a "non-visible" function is? I get a message at the bottom `Non-visible functions are asterisked`. Is that a `.foo` type hidden function? – Rich Scriven Apr 22 '14 at 05:31
  • Some functions are "exported" and others are not. If you want to see the code of one that is not exported from its namespace (assuming that the package with the function has been loaded), then use either `pkg_name:::fn_name` or `getAnywhere(fn_name)`. (Do not put the asterisk in the 'fn_name'.) I think the second one is slightly more powerful than the triple-colon operator but I don't know the the precise limits of `:::`. – IRTFM Apr 22 '14 at 05:33
  • Useful!! `getAnywhere` is nice. – Rich Scriven Apr 22 '14 at 05:42
0

As pointed out for lm it's the same because predict dispatch immediately to predict.lm, being

> predict
function (object, ...) 
UseMethod("predict")

However generally speaking it's best practice to use generic functions (like predict) instead of direct methods (predict.lm in this case), because the appropriate method could be called via UseMethod after a bit of manipulation/checking inside the generic function. An example is as.data.frame:

> as.data.frame
function (x, row.names = NULL, optional = FALSE, ...) 
{
    if (is.null(x)) 
        return(as.data.frame(list()))
    UseMethod("as.data.frame")
}
Luca Braglia
  • 3,133
  • 1
  • 16
  • 21