# cv.glmnet

From glmnet v1.1-5
by Trevor Hastie

##### Cross-validation for glmnet

Does k-fold cross-validation for glmnet, produces a plot,
and returns a value for `lambda`

- Keywords
- models, regression

##### Usage

`cv.glmnet(x, y, ..., nfolds, foldid, type)`

##### Arguments

- x
`x`

matrix as in`glmnet`

.- y
- response
`y`

as in`glmnet`

. - ...
- Other arguments that can be passed to
`glmnet`

. - nfolds
- number of folds - default is 10.
- foldid
- an optional vector of values between 1 and
`nfold`

identifying whhat fold each observation is in. If supplied,`nfold`

can be missing. - type
- loss to use for cross-validation. Currently two
options. The default is
`type="response"`

, which uses squared-error for gaussian models, and deviance for logistic regression.`type="class"`

applies to logistic regression

##### Details

The function runs `glmnet`

`nfolds`

+1 times; the
first to get the `lambda`

sequence, and then the remainder to
compute the fit with each of the folds omitted. The error is
accumulated, and the average error and standard deviation over the
folds is computed. This function is a preliminary version, since it
does not allow the full range of data-types for `glmnet`

yet.

##### Value

- an object of class
`"cv.glmnet"`

is returned, which is a list with the ingredients of the cross-validation fit. lambda the values of `lambda`

used in the fits.cvm The mean cross-validated error - a vector of length `length(lambda)`

.cvsd estimate of standard error of `svm`

.cvup upper curve = `cvm+cvsd`

.cvlo lower curve = `cvm-cvsd`

.nzero number of non-zero coefficients at each `lambda`

.name a text string indicating type of measure (for plotting purposes). lambda.min value of `lambda`

that gives minimum`cvm`

.lambda.1se largest value of `lambda`

such that error is within 1 standard error of the minimum.

##### References

Friedman, J., Hastie, T. and Tibshirani, R. (2008)
*Regularization Paths for Generalized Linear Models via Coordinate
Descent*

##### See Also

`glmnet`

and `plot`

method for `"cv.glmnet"`

object.

##### Examples

```
set.seed(1010)
n=1000;p=100
nzc=trunc(p/10)
x=matrix(rnorm(n*p),n,p)
beta=rnorm(nzc)
fx= (x[,seq(nzc)] %*% beta)
eps=rnorm(n)*5
y=drop(fx+eps)
px=exp(fx)
px=px/(1+px)
ly=rbinom(n=length(px),prob=px,size=1)
cvob1=cv.glmnet(x,y)
plot(cvob1)
title("Gaussian Family",line=2.5)
frame()
set.seed(1011)
par(mfrow=c(2,2),mar=c(4.5,4.5,4,1))
cvob2=cv.glmnet(x,ly,family="binomial")
plot(cvob2)
title("Binomial Family",line=2.5)
set.seed(1011)
cvob3=cv.glmnet(x,ly,family="binomial",type="class")
plot(cvob3)
title("Binomial Family",line=2.5)
```

*Documentation reproduced from package glmnet, version 1.1-5, License: GPL-2*

### Community examples

Looks like there are no examples yet.