0

I am fitting a logistic regression for a binary classification problem. Now I would like to find the source code of predict function and see how "predict" works.

> methods(predict)
 [1] predict.ar*                predict.Arima*             predict.arima0*           
 [4] predict.glm                predict.HoltWinters*       predict.lm                
 [7] predict.loess*             predict.mlm*               predict.nls*              
 [10] predict.poly*              predict.ppr*               predict.prcomp*           
 [13] predict.princomp*          predict.smooth.spline*    predict.smooth.spline.fit*
 [16] predict.StructTS*         
 see '?methods' for accessing help and source code

The predict is a generic function that will invoke the specific predict function based on the first input argument.

Within glmnet package https://github.com/cran/glmnet/tree/master/R, there are two main predict functions -- predict.glmnet and predict.lognet. Predict.glm can be found at https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/predict.glm.R. So what are the relations among predict.glm, predict.glmnet and predict.lognet?

And in predict.lognet.R, there is one line:

nfit=NextMethod("predict")

It is explained in R Documentation that "NextMethod invokes the next method (determined by the class vector, either of the object supplied to the generic, or of the first argument to the function containing NextMethod if a method was invoked directly). Normally NextMethod is used with only one argument, generic, but if further arguments are supplied these modify the call to the next method."

What is "predict" in NextMethod("predict")? Is it calling predict.glm?

How the prediction can be made based on a saved file .RDS model?

Thank you.

  • What does `class(your_model)` give? – Gregor Thomas Nov 14 '17 at 15:18
  • As far as basing a prediction on a saved file, you would first load the saved file `my_model = readRDS("my_rds_file.rds")`, then you would call `predict` on `my_model` as usual. – Gregor Thomas Nov 14 '17 at 15:20
  • > class(glmmod) [1] "lognet" "glmnet" – Heidi Xiao Nov 14 '17 at 15:23
  • So, as you say, `predict.lognet` will be called first, it says `NextMethod`, `glmnet` is the next in the class vector, so `predict.glmnet` is called next. [Here's the code in the github repo you linked](https://github.com/cran/glmnet/blob/master/R/predict.glmnet.R). – Gregor Thomas Nov 14 '17 at 15:26
  • Thank you. I know how to apply readRDS and do the prediction in R. But I was asked to figure out that if the system are going to implement in Java and call the model saved from R, how is it going to be connected. – Heidi Xiao Nov 14 '17 at 15:27
  • That seems unrelated to your question about prediction routines. But the answer is "probably not". Maybe you can find a java package for reading R objects saved in RDS format? But it seems unlikely. – Gregor Thomas Nov 14 '17 at 15:29
  • Thank you so much. I will keep searching for it. – Heidi Xiao Nov 14 '17 at 15:35
  • I think your looking for [this function](https://github.com/cran/glmnet/blob/master/R/predict.glmnet.R#L32) – Jthorpe Dec 04 '17 at 19:18

0 Answers0