Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view the source code for a particular `predict` function? [duplicate]

Tags:

r

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?

like image 463
merlin2011 Avatar asked Nov 06 '13 03:11

merlin2011


2 Answers

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>
like image 119
CHP Avatar answered Nov 15 '22 03:11

CHP


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.

like image 40
Marius Avatar answered Nov 15 '22 03:11

Marius