Learn R Programming

cernn (version 0.1)

select_lambda: Selection of penalty parameter based on cross-validation

Description

select_lambda selects the best regularization parameter from a grid of values based on minimal predictive negative log-likelihood.

Usage

select_lambda(X, lambda, fold = min(nrow(X), 10))

Arguments

X
n-by-p data matrix
lambda
vector of penalties for cross-validation
fold
number of folds for cross-validation

Examples

Run this code
n <- 30
p <- 30
set.seed(12345)
X <- matrix(rnorm(n*p),n,p)
alpha <- get_alpha(X)
lambda_max <- get_lambda_max(svd(X)$d**2,alpha,n)
lambda <- 10**(seq(-1,log10(lambda_max),length.out=100))
sol_path <- cernn(X,lambda,alpha)
df <- t(sol_path$e)

## Plot regularization paths of eigenvalues
matplot(x=log10(lambda),y=df,type='l',ylab='shrunken eigenvalue')
grand_mean <- (norm(scale(X,center=TRUE,scale=FALSE),'f')**2)/(n*p)
abline(h=grand_mean)

## Plot selected lambda
abline(v=log10(select_lambda(X,lambda)$lambda))

Run the code above in your browser using DataLab