"glmnet"
object.
"predict"(object, newx, s = NULL,
type=c("link","response","coefficients","nonzero","class"), exact = FALSE, offset, ...)
"coef"(object,s=NULL, exact=FALSE, ...)
"glmnet"
model object.x
at which predictions are
to be made. Must be a matrix; can be sparse as in Matrix
package. This argument is not used for type=c("coefficients","nonzero")
lambda
at which
predictions are required. Default is the entire sequence used to
create the model. "link"
gives the
linear predictors for "binomial"
, "multinomial"
,
"poisson"
or "cox"
models; for "gaussian"
models it gives the fitted
values. Type "response"
gives the fitted probabilities for
"binomial"
or "multinomial"
, fitted mean for
"poisson"
and the fitted relative-risk for "cox"
; for "gaussian"
type "response"
is equivalent to type "link"
. Type
"coefficients"
computes the coefficients at the requested
values for s
. Note that for
"binomial"
models, results are returned only for the class
corresponding to the second level of the factor response.
Type "class"
applies only to
"binomial"
or "multinomial"
models, and produces the
class label corresponding to the maximum probability. Type
"nonzero"
returns a list of the indices of the nonzero
coefficients for each value of s
.s
(lambda) different from those used in the fitting
of the original model. If exact=FALSE
(default), then the predict function uses linear interpolation
to make predictions for values of s
(lambda) that do not coincide with
those used in the fitting algorithm. While this is often a good
approximation, it can sometimes be a bit coarse.
With exact=TRUE
, these different values of s
are merged
(and sorted) with
object$lambda
, and the model is refit before predictions are
made. In this case, it is strongly advisable to supply the original
data x=
and y=
as additional
named arguments to predict()
or coef()
.
The workhorse predict.glmnet()
needs to update
the model,
and expects the data used to create it to be around.
Although it will often work fine without supplying these additional arguments, it will likely break when
used inside a nested sequence of function calls.
If additional arguments such as weights
and
offset
were used, these should be included by name as well.type="coefficients"
or type="nonzero"
)x=
when exact=TRUE
; seeexact
argument."multinomial"
objects. This function actually calls
NextMethod()
,
and the appropriate predict method is invoked for each of the three
model types. coef(...)
is equivalent to predict(type="coefficients",...)
glmnet
, and print
, and coef
methods, and cv.glmnet
.x=matrix(rnorm(100*20),100,20)
y=rnorm(100)
g2=sample(1:2,100,replace=TRUE)
g4=sample(1:4,100,replace=TRUE)
fit1=glmnet(x,y)
predict(fit1,newx=x[1:5,],s=c(0.01,0.005))
predict(fit1,type="coef")
fit2=glmnet(x,g2,family="binomial")
predict(fit2,type="response",newx=x[2:5,])
predict(fit2,type="nonzero")
fit3=glmnet(x,g4,family="multinomial")
predict(fit3,newx=x[1:3,],type="response",s=0.01)
Run the code above in your browser using DataLab