### generate sample data
set.seed(2023)
n <- 50
p <- 30
X <- matrix(rnorm(n * p), n, p)
### Example 1, LASSO penalty
beta <- apply(matrix(rnorm(p, sd = 1), p, 1), 1, fdaSP::softhresh, 1.5)
y <- X %*% beta + rnorm(n, sd = sqrt(crossprod(X %*% beta)) / 20)
### set the hyper-parameters of the ADMM algorithm
maxit <- 1000
adaptation <- TRUE
rho <- 1
reltol <- 1e-5
abstol <- 1e-5
### run cross-validation
mod_cv <- lmSP_cv(X = X, y = y, penalty = "LASSO",
standardize.data = FALSE, intercept = FALSE,
cv.fold = 5, nlambda = 30,
control = list("adaptation" = adaptation,
"rho" = rho,
"maxit" = maxit, "reltol" = reltol,
"abstol" = abstol,
"print.out" = FALSE))
### graphical presentation
plot(log(mod_cv$lambda), mod_cv$mse, type = "l", col = "blue", lwd = 2, bty = "n",
xlab = latex2exp::TeX("$\\log(\\lambda)$"), ylab = "Prediction Error",
ylim = range(mod_cv$mse - mod_cv$mse.sd, mod_cv$mse + mod_cv$mse.sd),
main = "Cross-validated Prediction Error")
fdaSP::confband(xV = log(mod_cv$lambda), yVmin = mod_cv$mse - mod_cv$mse.sd,
yVmax = mod_cv$mse + mod_cv$mse.sd)
abline(v = log(mod_cv$lambda[which(mod_cv$lambda == mod_cv$lambda.min)]),
col = "red", lwd = 1.0)
### comparison with oracle error
mod <- lmSP(X = X, y = y, penalty = "LASSO",
standardize.data = FALSE,
intercept = FALSE,
nlambda = 30,
control = list("adaptation" = adaptation,
"rho" = rho,
"maxit" = maxit, "reltol" = reltol,
"abstol" = abstol,
"print.out" = FALSE))
err_mod <- apply(mod$sp.coef.path, 1, function(x) sum((x - beta)^2))
plot(log(mod$lambda), err_mod, type = "l", col = "blue", lwd = 2,
xlab = latex2exp::TeX("$\\log(\\lambda)$"),
ylab = "Estimation Error", main = "True Estimation Error", bty = "n")
abline(v = log(mod$lambda[which(err_mod == min(err_mod))]), col = "red", lwd = 1.0)
abline(v = log(mod_cv$lambda[which(mod_cv$lambda == mod_cv$lambda.min)]),
col = "red", lwd = 1.0, lty = 2)
### Example 2, sparse group-LASSO penalty
beta <- c(rep(4, 12), rep(0, p - 13), -2)
y <- X %*% beta + rnorm(n, sd = sqrt(crossprod(X %*% beta)) / 20)
### define groups of dimension 3 each
group1 <- rep(1:10, each = 3)
### set regularization parameter grid
lam <- 10^seq(1, -2, length.out = 30)
### set the alpha parameter
alpha <- 0.5
### set the hyper-parameters of the ADMM algorithm
maxit <- 1000
adaptation <- TRUE
rho <- 1
reltol <- 1e-5
abstol <- 1e-5
### run cross-validation
mod_cv <- lmSP_cv(X = X, y = y, penalty = "spGLASSO",
groups = group1, cv.fold = 5,
standardize.data = FALSE, intercept = FALSE,
lambda = lam, alpha = 0.5,
control = list("adaptation" = adaptation,
"rho" = rho,
"maxit" = maxit, "reltol" = reltol,
"abstol" = abstol,
"print.out" = FALSE))
### graphical presentation
plot(log(mod_cv$lambda), mod_cv$mse, type = "l", col = "blue", lwd = 2, bty = "n",
xlab = latex2exp::TeX("$\\log(\\lambda)$"), ylab = "Prediction Error",
ylim = range(mod_cv$mse - mod_cv$mse.sd, mod_cv$mse + mod_cv$mse.sd),
main = "Cross-validated Prediction Error")
fdaSP::confband(xV = log(mod_cv$lambda), yVmin = mod_cv$mse - mod_cv$mse.sd,
yVmax = mod_cv$mse + mod_cv$mse.sd)
abline(v = log(mod_cv$lambda[which(mod_cv$lambda == mod_cv$lambda.min)]),
col = "red", lwd = 1.0)
### comparison with oracle error
mod <- lmSP(X = X, y = y,
penalty = "spGLASSO",
groups = group1,
standardize.data = FALSE,
intercept = FALSE,
lambda = lam,
alpha = 0.5,
control = list("adaptation" = adaptation, "rho" = rho,
"maxit" = maxit, "reltol" = reltol, "abstol" = abstol,
"print.out" = FALSE))
err_mod <- apply(mod$sp.coef.path, 1, function(x) sum((x - beta)^2))
plot(log(mod$lambda), err_mod, type = "l", col = "blue", lwd = 2,
xlab = latex2exp::TeX("$\\log(\\lambda)$"),
ylab = "Estimation Error", main = "True Estimation Error", bty = "n")
abline(v = log(mod$lambda[which(err_mod == min(err_mod))]), col = "red", lwd = 1.0)
abline(v = log(mod_cv$lambda[which(mod_cv$lambda == mod_cv$lambda.min)]),
col = "red", lwd = 1.0, lty = 2)
Run the code above in your browser using DataLab