如何查看特定“预测”函数的源代码?

5
根据文档,predict是一个多态函数,在R中实际调用的函数取决于作为第一个参数传递的内容。
然而,文档没有提供关于predict实际调用哪个特定类的函数名称的任何信息。
通常,可以键入函数名称以获取其源代码,但对于predict则不适用。
如果我想查看在类型为glmnet的对象上调用predict函数的源代码,最简单的方法是什么?
2个回答

9
您可以使用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>

2

调用methods(predict)将显示定义在特定类中的所有方法,例如:

> 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*
glmnet的预测函数很可能只是predict.glmnet

在我使用library(glmnet)之后,没有这个名称的方法。这就是我感到好奇的原因。 一般来说,是否有一种方法可以确定给定对象调用哪个预测方法? - merlin2011
刚刚快速查看了实际的glmnet包,似乎predict.glmnet没有导出到全局命名空间中,所以有点难以访问,但是您可以使用glmnet:::predict.glmnet来访问它,并使用methods(class=glmnet)查看定义的方法。 - Marius

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接