Learn R Programming

hqreg (version 1.2)

cv.hqreg: Cross-validation for hqreg

Description

Perform k-fold cross validation for elastic-net penalized Huber loss regression and quantile regression over a sequence of lambda values and find an optimal lambda.

Usage

cv.hqreg(X, y, ..., nfolds=10, fold.id, type.measure = c("deviance", "mse", "mae"), 
         seed, trace=FALSE)

Arguments

X
The input matrix, as in hqreg.
y
The response vector, as in hqreg.
...
Additional arguments to hqreg.
nfolds
The number of cross-validation folds. Default is 10.
fold.id
(Optional) a vector of values between 1 and nfold indicating which fold each observation belongs to. If supplied, nfolds can be missing. By default the observations are randomly assigned by cv.hqreg.
type.measure
The default is "deviance", which uses the chosen loss function of the model. Other options include "mse" for mean squared error and "mae" for mean absolute error.
seed
Seed for the random number generator in order to obtain reproducible results.
trace
If set to TRUE, cv.hqreg will inform the user of its progress by announcing the beginning of each CV fold. Default is FALSE.

Value

  • The function returns an object of S3 class "cv.hqreg", which is a list containing:
  • cveThe error for each value of lambda, averaged across the cross-validation folds.
  • cvseThe estimated standard error associated with each value of cve.
  • lambdaThe values of lambda used in the cross-validation fits.
  • fitThe fitted hqreg object for the whole data.
  • lambda.1seThe largest lambda such that the error is within 1 standard error of the minimum.
  • lambda.minThe value of lambda with the minimum cross-validation error.

Details

The function randomly partitions the data in nfolds. It calls hqreg nfolds+1 times, the first to obtain the lambda sequence, and the remainder to fit with each of the folds left out once for validation. The cross-validation error is the average of validation errors for the nfolds fits.

Note that cv.hqreg does not search for values of alpha, gamma or tau. Specific values should be supplied, otherwise the default ones for hqreg are used. If users would like to cross-validate alpha, gamma or tau as well, they should call cv.hqreg for each combination of these parameters and use the same "seed" in these calls so that the partitioning remains the same.

See Also

hqreg, plot.cv.hqreg

Examples

Run this code
X = matrix(rnorm(1000*100), 1000, 100)
beta = rnorm(10)
eps = 4*rnorm(1000)
y = drop(X[,1:10] %*% beta + eps)
cv = cv.hqreg(X, y, seed = 123)
plot(cv)
predict(cv, X[1:5,])

Run the code above in your browser using DataLab