Description Usage Arguments Value Details Author(s) Examples
This function takes in inputs defined by the user and computes the optimum α and λ for an Elastic Net model. The function is very flexible and allows for many different settings such as, data splitting, repeated error curves and a definable α grid. This function also fully supports multiple-cores parallelisation. The main fitting process is cv.glmnet() from the package glmnet.
1 2 3 | prediction_ElasticNet(data = data, x.indices = x.indices,
response = response, err.curves = 0, splits = 0, step.size = 0.2,
type.lambda = "lambda.min", interactive = FALSE, parallel = FALSE)
|
data |
A well-cleaned |
x.indices |
The coordinates of the predictors that you would like to model with. Please provide a vecotr of locations e.g. seq(2,6). |
response |
The location of the response within the |
err.curves |
Due to the fact that the cross-validation process is random, the result can vary qutie a bit (if without a seed). In order to stabilise the CV process, the function fits a collection of Elastic Net models, each with a different α value, multiple times. Therefore, for EACH α, we will create multiple error curves over a range of λs and the optimum pair (α, λ) is the pair that has the lowest averaged error curves value (local optimum). We have an optimum pair (α, λ) for each α and the global optimum pair is the pair that has the overall lowest averaged error curved value. Note, with this setting, the process tends to be slow. Thus, we highly suggest multiple-cores parallelisation. Note you can set this argument to 0 if you do not wish to stabilise the process, in which case the seed (1234567) will be used for the CV process. A postive integer indicates the stabilisation process is desired. For more information about how this works, please see section details below. |
splits |
A element specifying the training proportion and test proportion will be set as 1- traing.proportion. Note if you set |
step.size |
The step size of the α grid. An adjustable value within [0,1]. Default is |
type.lambda |
Either "lambda.min" or "lambda.1se". Default is "lambda.min. Note when |
interactive |
If you are running this function, please ALWAYS keep this argument to FALSE, which is the default. |
parallel |
parallelisation supported,default is FALSE. |
a list with elements:
seed |
if |
number of err.curves |
if |
best alpha |
if |
best lambda |
A part of the global optimum pair (α, λ). For more information on how the α and λ get selected, please see the detail section. |
prediction error |
if |
prediction_lower |
The function will only compute this when |
prediction_upper |
The function will only compute this when |
rooted prediction error |
The function will only compute this when |
absolute prediction error |
The function will only compute this when |
This function further develops on the cv.glmnet() function from the glmnet package to allow for more flexibility. The function allows users the option to split the dataset into a traning set and a test set, which usually gives more realistic assessment of perdictive performance than using the whole dataset.
The function also offers an alternative to compute the global optimum (α, λ) pair by averaging across the error curves instead of using a fixed seed. More specifically, this stabilisation
takes a double looping structure where the outer layer contains the α grid and the inner loop builds multiple Elastic Net models for each α in the outer layer.
In this way, for each α, we create say, B, Elastic Net models and thus, this results in B error curves over a range of λs. For each α then, the
function finds the local optimum pair (α, λ) by averaging across these error curves and find the pair that has the lowest averaged cross validation errors. After the function finds
all the local optimum pairs, the golbal optimum pair is the pair that has the overall lowest averaged cross validation error. From experneice, for medium
size datasets, with err.curves
larger than 1500, the global optimum (α, λ) will usually converge to stable values that consistently
achieves the overall lowest averaged across error curves value.
When splits = 0
(no spliting), the 95 percent confidence interval for such a prediction error (overall lowest averaged error curves value) is generated as follows:
from the corresponding error curves for that optimum α, the cross-validation scores corresponding to the global optimum (α, λ) are extracted and the command quantile()
is then used to compute the 95 percent confidence interval. Do not worry, we will also provide you with a plot that contains these error curves from the output, so you can
see how it is that the optimum pair values got selected and with its 95 percent CI around it. When we are not stabilising the process e.g.
err.curve = 0
but still using the whole dataset splits = 0
, we compute the (α, λ) pair with the seed (1234567) and the associated CI is computed by using the standard error
provided by the glmnet package and assuming normality. When we do split the dataset however, we provide out of sample mean squared error(MSE), RMSE and MAE instead of 95 percent CI.
Mokyo Zhou
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 | library(glmnet)
data(QuickStartExample)
#please NOTE: you can access "QuickStartExample" by using data.frame(y,x)
#non-split, no error curves, step.size =0.2, using lambda.min
result <- prediction_ElasticNet(data = data.frame(y,x), x.indices = seq(2,21),
response = 1, err.curves = 0 ,splits = 0, step.size =0.2,
type.lambda="lambda.min")
#0.8 /0.2 split, no error curves, step.size=0.1, using lambda.1se
result <- prediction_ElasticNet(data = data.frame(y,x), x.indices = seq(2,21),
response = 1, err.curves = 0, splits = 0.8, step.size = 0.1,
type.lambda = "lambda.1se")
#non-split, but with 100 error curves, step.size=0.3, with parallel (2 cores)
#cl <- parallel::makeCluster(2)
#doParallel::registerDoParallel(cl)
result <- prediction_ElasticNet(data = data.frame(y,x), x.indices = seq(2,21),
response = 1, err.curves = 100, splits = 0, step.size = 0.3, parallel = TRUE)
#0.8 / 0.2 split, with 100 error curves, step.size=0.2, with parallel (2 cores)
#cl <- parallel::makeCluster(2)
#doParallel::registerDoParallel(cl)
result <- prediction_ElasticNet(data = data.frame(y,x), x.indices = seq(2,21),
response = 1, err.curves = 100, splits = 0.8, step.size = 0.2, parallel = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.