# Cross-Validation for relaxnet Models

### Description

Both of these functions will perform v-fold cross-validation to select tuning parameters for relaxnet models. `cv.relaxnet`

will cross-validate on the value of lambda for both the main model and for the relaxed models, a two dimensional cross-validation. `cv.alpha.relaxnet`

will in addition cross-validate on the value of alpha. For each value of alpha, relaxnet is run once on whole data set, then it is run again v times on subsets of the rows of the data.

### Usage

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | ```
cv.relaxnet(x, y, family = c("gaussian", "binomial"),
nlambda = 100,
alpha = 1,
relax = TRUE,
relax.nlambda = 100,
relax.max.vars = min(nrow(x), ncol(x)) * 0.8,
lambda = NULL,
relax.lambda.index = NULL,
relax.lambda.list = NULL,
nfolds = 10,
foldid,
multicore = FALSE,
mc.cores,
mc.seed = 123,
...)
cv.alpha.relaxnet(x, y, family = c("gaussian", "binomial"),
nlambda = 100,
alpha = c(.1, .3, .5, .7, .9),
relax = TRUE,
relax.nlambda = 100,
relax.max.vars = min(nrow(x), ncol(x)) * 0.8,
lambda = NULL,
relax.lambda.index = NULL,
relax.lambda.list = NULL,
nfolds = 10,
foldid,
multicore = FALSE,
mc.cores,
mc.seed = 123,
...)
``` |

### Arguments

`x` |
Input matrix, of dimension nobs x nvars; each row is an observation vector. Can be in sparse matrix format (inherit from class |

`y` |
response variable. Quantitative for |

`family` |
Response type (see above). |

`nlambda` |
The number of |

`alpha` |
Elastic net mixing parameter (see |

`relax` |
Should the model be relaxed. If FALSE, only the main glmnet model is run and no relaxed models are. |

`relax.nlambda` |
Like nlambda but for secondary (relaxed) models. |

`relax.max.vars` |
Maximum number of variables for relaxed models. No relaxation will be done for subsets along the regularization path with number of variables greater than relax.max.vars. If |

`lambda` |
See (see |

`relax.lambda.index` |
Vector which indexes the lambda argument and specifyies the values at which a relaxed model should be fit. Optional: default is to let |

`relax.lambda.list` |
List of lambda values to use for the relaxed models. Optional: default is to let |

`nfolds` |
Number of folds - default is 10. Although |

`foldid` |
An optional vector of values between 1 and |

`multicore` |
Should execution be parallelized over cv folds (for |

`mc.cores` |
Number of cores/cpus to be used for multicore processing. Processing will be most efficient if nfolds (for |

`mc.seed` |
Integer value with which to seed the RNG when using parallel processing (internally, |

`...` |
Further aruments passed to glmnet. Use with caution as this has not yet been tested. For example, setting |

### Details

cv.glmnet's `type.measure`

argument has not yet been implemented. For type = gaussian models, mean squared error is used, and for type = binomial, binomial deviance is used.

### Value

For `cv.relaxnet`

– an object of class `"cv.relaxnet"`

containing the following slots:

`call` |
A copy of the call which produced this object |

`relax` |
The value of the relax argument. If this is |

`lambda` |
lambda sequence used for this fit |

`cvm` |
The mean cross-validated error - a vector of length |

`cvsd` |
estimate of standard error of |

`cvup` |
upper curve = |

`cvlo` |
lower curve = |

`nzero` |
number of non-zero coefficients at each |

`name` |
a text string indicating type of measure. |

`relaxnet.fit` |
Fitted relaxnet object for the full data. |

`relax.cvstuff.list` |
List containing cvm and cvsd for each of the relaxed models. |

`relax.lambda.list.trunc` |
List containing the values of lambda used for cross-validation for each relaxed model |

`which.model.min` |
This will have value |

`overall.lambda.min` |
The value of lambda with overall min cvm (i.e. from the submodel specified by |

`min.cvm` |
The overall minimum value of cvm |

`main.lambda.min` |
lambda.min, restricted to main model only. |

`main.lambda.1se` |
lambda.1se, restricted to main model only (see |

`main.min.cvm` |
Minimum of cvm, restricted to main model only. |

`total.time` |
Time in seconds to fit this object (full data fit plus all cross-validation) |

`full.data.fit.time` |
Time in seconds to fit the relaxnet.fit. |

`cv.fit.times` |
Time in seconds to fit the models for each set of cv folds. |

For `cv.alpha.relaxnet`

– an object of class `"cv.alpha.relaxnet"`

containing the following slots:

`call` |
A copy of the call which produced this object |

`relax` |
The value of the relax argument. If this is |

`alpha` |
The alpha values used. |

`cv.relaxnet.results` |
List of cv.relaxnet objects, one for each alpha value. |

`which.alpha.min` |
The alpha value which "won" the cross-validation. |

`total.time` |
Time in seconds to fit this object. |

### Note

This is a preliminary release and several additional features are planned for later versions.

### Author(s)

Stephan Ritter, with design contributions from Alan Hubbard.

Much of the code (and some help file content) is adapted from the glmnet package, whose authors are Jerome Friedman, Trevor Hastie and Rob Tibshirani.

### References

Stephan Ritter and Alan Hubbard, Tech report (forthcoming).

Jerome Friedman, Trevor Hastie, Rob Tibshirani (2010) “Regularization Paths for Generalized Linear Models via Coordinate Descent.” *Journal of Statistical Software* **33**(1)

Nicolai Meinshausen (2007) “Relaxed Lasso” *Computational Statistics and Data Analysis* **52**(1), 374-393

### See Also

`relaxnet`

, `predict.cv.relaxnet`

### Examples

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | ```
## generate predictor matrix
nobs <- 100
nvars <- 200
set.seed(23)
x <- matrix(rnorm(nobs * nvars), nobs, nvars)
## make sure it has unique colnames
colnames(x) <- paste("x", 1:ncol(x), sep = "")
## let y depend on first 5 columns plus noise
y <- rowSums(x[, 1:5]) + rnorm(nrow(x))
## run cv.relaxnet
cv.result <- cv.relaxnet(x, y)
predict(cv.result, type = "nonzero")
## very few false positives compared to glmnet alone
## glmnet min rule
predict(cv.result$relaxnet.fit$main.glmnet.fit,
type = "nonzero",
s = cv.result$main.lambda.min)
## glmnet 1se rule
predict(cv.result$relaxnet.fit$main.glmnet.fit,
type = "nonzero",
s = cv.result$main.lambda.1se)
## get values of the coefs for cv.relaxnet's chosen fit
coefs <- drop(predict(cv.result, type = "coef"))
coefs[coefs != 0]
``` |

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker. Vote for new features on Trello.