5

Based on the documentation, predict is a polymorphic function in R and a different function is actually called depending on what is passed as the first argument.

However, the documentation does not give any information about the names of the functions that predict actually invokes for any particular class.

Normally, one could type the name of a function to get its source, but this does not work with predict.

If I want to view the source code for the predict function when invoked on objects of the type glmnet, what is the easiest way?

0

2 Answers 2

9

You can use look for a function using getAnywhere

getAnywhere("predict.glmnet")
## A single object matching ‘predict.glmnet’ was found
## It was found in the following places
##   registered S3 method for predict from namespace glmnet
##   namespace:glmnet
## with value
## 
## function (object, newx, s = NULL, type = c("link", "response", 
##     "coefficients", "nonzero", "class"), exact = FALSE, offset, 
##     ...) 
## {
##     type = match.arg(type)
##     if (missing(newx)) {
##         if (!match(type, c("coefficients", "nonzero"), FALSE)) 
##             stop("You need to supply a value for 'newx'")
##     }
##     if (exact && (!is.null(s))) {
##         lambda = object$lambda
##         which = match(s, lambda, FALSE)
##         if (!all(which > 0)) {
##             lambda = unique(rev(sort(c(s, lambda))))
##             object = update(object, lambda = lambda)
##         }
##     }
##     a0 = t(as.matrix(object$a0))
##     rownames(a0) = "(Intercept)"
##     nbeta = rbind2(a0, object$beta)
##     if (!is.null(s)) {
##         vnames = dimnames(nbeta)[[1]]
##         dimnames(nbeta) = list(NULL, NULL)
##         lambda = object$lambda
##         lamlist = lambda.interp(lambda, s)
##         nbeta = nbeta[, lamlist$left, drop = FALSE] * lamlist$frac + 
##             nbeta[, lamlist$right, drop = FALSE] * (1 - lamlist$frac)
##         dimnames(nbeta) = list(vnames, paste(seq(along = s)))
##     }
##     if (type == "coefficients") 
##         return(nbeta)
##     if (type == "nonzero") 
##         return(nonzeroCoef(nbeta[-1, , drop = FALSE], bystep = TRUE))
##     if (inherits(newx, "sparseMatrix")) 
##         newx = as(newx, "dgCMatrix")
##     nfit = as.matrix(cbind2(1, newx) %*% nbeta)
##     if (object$offset) {
##         if (missing(offset)) 
##             stop("No offset provided for prediction, yet used in fit of glmnet", 
##                 call. = FALSE)
##         if (is.matrix(offset) && dim(offset)[[2]] == 2) 
##             offset = offset[, 2]
##         nfit = nfit + array(offset, dim = dim(nfit))
##     }
##     nfit
## }
## <environment: namespace:glmnet>
Sign up to request clarification or add additional context in comments.

Comments

2

Calling methods(predict) will show you all the methods that have been defined for particular classes, e.g.:

> methods(predict)
 [1] predict.ar*                predict.Arima*             predict.arima0*            predict.glm               
 [5] predict.HoltWinters*       predict.lm                 predict.loess*             predict.mlm               
 [9] predict.nls*               predict.poly               predict.ppr*               predict.prcomp*           
[13] predict.princomp*          predict.smooth.spline*     predict.smooth.spline.fit* predict.StructTS*

The predict function for a glmnet is more than likely just predict.glmnet.

2 Comments

There is no method by that name after I used library(glmnet). That's why I was curious. In general, is there a way to tell which of the predict methods is actually invoked, given a particular object?
Just had a quick look at the actual glmnet package, it seems like predict.glmnet isn't exported into the global namespace so it's a bit harder to get to, but you can access it using glmnet:::predict.glmnet, and see the methods that are defined using methods(class=glmnet)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.