Formula interface for elastic net cross-validation with cv.glmnet
cv.glmnet(x, ...)# S3 method for default
cv.glmnet(x, ...)
# S3 method for formula
cv.glmnet(formula, data, alpha = 1, nfolds = 10, ...,
  weights = NULL, offset = NULL, subset = NULL,
  na.action = getOption("na.action"), drop.unused.levels = FALSE,
  xlev = NULL, sparse = FALSE, use.model.frame = FALSE)
# S3 method for cv.glmnet.formula
predict(object, newdata, na.action = na.pass, ...)
# S3 method for cv.glmnet.formula
coef(object, ...)
# S3 method for cv.glmnet.formula
print(x, ...)
For the default method, a matrix of predictor variables.
For cv.glmnet.formula and cv.glmnet.default, other arguments to be passed to glmnet::cv.glmnet; for the predict and coef methods, arguments to be passed to their counterparts in package glmnet.
A model formula; interaction terms are allowed and will be expanded per the usual rules for linear models.
A data frame or matrix containing the variables in the formula.
The elastic net mixing parameter. See glmnet::glmnet for more details.
The number of crossvalidation folds to use. See glmnet::cv.glmnet for more details.
An optional vector of case weights to be used in the fitting process. If missing, defaults to an unweighted fit.
An optional vector of offsets, an a priori known component to be included in the linear predictor.
An optional vector specifying the subset of observations to be used to fit the model.
A function which indicates what should happen when the data contains missing values. For the predict method, na.action = na.pass will predict missing values with NA; na.omit or na.exclude will drop them.
Should factors have unused levels dropped? Defaults to FALSE.
A named list of character vectors giving the full set of levels to be assumed for each factor.
Should the model matrix be in sparse format? This can save memory when dealing with many factor variables, each with many levels (but see the warning below).
Should the base model.frame function be used when constructing the model matrix? This is the standard method that most R modelling functions use, but has some disadvantages. The default is to avoid model.frame and construct the model matrix term-by-term; see discussion.
For the predict and coef methods, an object of class cv.glmnet.formula.
For the predict method, a data frame containing the observations for which to calculate predictions.
For cv.glmnet.formula, an object of class cv.glmnet.formula. This is basically the same object created by glmnet::cv.glmnet, but with extra components to allow formula usage.
The cv.glmnet function in this package is an S3 generic with a formula and a default method. The former calls the latter, and the latter is simply a direct call to the cv.glmnet function in package glmnet. All the arguments to glmnet::cv.glmnet are (or should be) supported.
The code works in a similar manner to lm, glm and other modelling functions. The arguments are used to generate a model frame, which is a data frame augmented with information about the roles the columns play in fitting the model. This is then turned into a model matrix and a response vector, which are passed to glmnet::glmnet along with any arguments in .... If sparse is TRUE, then Matrix::sparse.model.matrix is used instead of stats::model.matrix to create the model matrix.
The predict and coef methods are wrappers for the corresponding methods in the glmnet package. The former constructs a predictor model matrix from its newdata argument and passes that as the newx argument to glmnet:::predict.cv.glmnet.
glmnet::cv.glmnet, glmnet:::predict.cv.glmnet, glmnet:::coef.cv.glmnet, model.frame, model.matrix
# NOT RUN {
cv.glmnet(mpg ~ ., data=mtcars)
cv.glmnet(Species ~ ., data=iris, family="multinomial")
# }
# NOT RUN {
# Leukemia example dataset from Trevor Hastie's website
download.file("http://web.stanford.edu/~hastie/glmnet/glmnetData/Leukemia.RData",
              "Leukemia.RData")
load("Leukemia.Rdata")
leuk <- do.call(data.frame, Leukemia)
cv.glmnet(y ~ ., leuk, family="binomial")
# }
Run the code above in your browser using DataLab