glmnet_measure | R Documentation |
Extract Measures of Model Fit from GLMNET Fit
Description
Extracts measures of model fitness from a glmnet
object.
Usage
glmnet_measure(
cv.model,
x = NULL,
y = NULL,
lambda.criteria = "lambda.min",
type.measure = c("default", "mse", "deviance", "class", "auc", "mae", "C"),
family = "gaussian",
alpha = 1
)
Arguments
cv.model |
A model fit by glmnet function cv.glmnet() .
|
x |
input matrix, of dimension nobs x nvars; each row is an observation
vector. Can be in sparse matrix format (inherit from class
"sparseMatrix" as in package Matrix ).
Requirement: nvars >1 ; in other words, x should have 2 or more columns.
|
y |
response variable. Quantitative for family="gaussian" , or
family="poisson" (non-negative counts). For family="binomial"
should be either a factor with two levels, or a two-column matrix of counts
or proportions (the second column is treated as the target class; for a
factor, the last level in alphabetical order is the target class). For
family="multinomial" , can be a nc>=2 level factor, or a matrix
with nc columns of counts or proportions. For either
"binomial" or "multinomial" , if y is presented as a
vector, it will be coerced into a factor. For family="cox" , preferably
a Surv object from the survival package: see Details section for
more information. For family="mgaussian" , y is a matrix
of quantitative responses.
|
lambda.criteria |
Determines the model selection criteria. When
"lambda.min" the final model is selected based on the penalty that
minimizes the measure given in type.measure . When "lambda.1se"
the final model is selected based on the smallest value of lambda that is
within one standard error of the minimal measure given in
type.measure .
|
type.measure |
loss to use for cross-validation. Currently five
options, not all available for all models. The default is
type.measure="deviance" , which uses squared-error for gaussian models
(a.k.a type.measure="mse" there), deviance for logistic and poisson
regression, and partial-likelihood for the Cox model.
type.measure="class" applies to binomial and multinomial logistic
regression only, and gives misclassification error.
type.measure="auc" is for two-class logistic regression only, and
gives area under the ROC curve. type.measure="mse" or
type.measure="mae" (mean absolute error) can be used by all models
except the "cox" ; they measure the deviation from the fitted mean to
the response.
type.measure="C" is Harrel's concordance measure, only available for cox models.
|
family |
Either a character string representing
one of the built-in families, or else a glm() family object. For more
information, see Details section below or the documentation for response
type (above).
|
alpha |
The elasticnet mixing parameter, with 0\le\alpha\le 1 .
The penalty is defined as
(1-\alpha)/2||\beta||_2^2+\alpha||\beta||_1.
alpha=1 is the
lasso penalty, and alpha=0 the ridge penalty.
|
Value
A data frame containing measures of model fitness.
Note
This function is primarily used within
compare_ssnet
, but perhaps could be useful elsewhere.
Examples
## generate data (no intercept)
set.seed(4799623)
cn <- c()
for (i in 1:100) cn[i] <- paste0("x", i)
tb <- rbinom(100, 1, 0.05)
tx <- matrix(rnorm(10000), nrow = 100, ncol = 100,
dimnames = list(1:100, cn))
ty <- tx %*% tb + rnorm(100)
## fit model and export model fit stats
cv1 <- cv.glmnet(x = tx, y = ty, family = "gaussian", alpha = 1)
glmnet_measure(cv1, x = tx, y = ty)