Sys.setenv(LANGUAGE = "en")
library("ameld")

knitr::write_bib("glmnet", "rpackages.bib")

Authors: r packageDescription("ameld")[["Author"]]
Last modified: r file.info("ameld.Rmd")$mtime
Compiled: r date()

Introduction

The ameld R package extends glmnet::cv.glmnet [@R-glmnet; @glmnet2010]. It supports a repeated cross-validation (rcv.glmnet) and a repeated cross-validation to tune alpha and lambda simultaneously (arcv.glmnet).

Dataset

The example data set is taken from ?"glmnet::cv.glmnet":

set.seed(1010)
n <- 1000
p <- 100
nzc <- trunc(p/10)
x <- matrix(rnorm(n * p), n, p)
beta <- rnorm(nzc)
fx <- x[, seq(nzc)] %*% beta
eps <- rnorm(n) * 5
y <- drop(fx + eps)

Repeated Cross-Validation

rcv.glmnet is used to run a repeated cross-validation. The interface is the same as in cv.glmnet.

set.seed(1011)
# nrepcv should usually be higher but to keep the runtime of the example low
# we choose 2 here
rcvob <- rcv.glmnet(x, y, nrepcv = 2, nfolds = 3)
plot(rcvob)

plot(rcvob, what = "path")

Accessing coefficients or predicting works exactly as with cv.glmnet:

coef(rcvob)
predict(rcvob, newx = x[1:5, ], s = "lambda.min")

Simultaneously tune alpha and lambda

arcv.glmnet is used to run a repeated cross-validation and tune alpha and lambda.

set.seed(1011)
# nrepcv should usually be higher but to keep the runtime of the example low
# we choose 2 here
# in similar manner we just evaluate a few alpha values
arcvob <- arcv.glmnet(x, y, alpha = c(0, 0.5, 1), nrepcv = 2, nfolds = 3)

We could visualise the effect of different alphas across all lambdas in a plot.

plot(arcvob)

Alternatively we could just look at the "best" lambda.

plot(arcvob, what = "lambda.min")
plot(arcvob, what = "lambda.1se")

Next we selecting the best autotuned alpha:

i <- which.min.error(arcvob)
plot(arcvob$models[[i]])

Acknowledgment

This work is part of the AMPEL (Analysis and Reporting System for the Improvement of Patient Safety through Real-Time Integration of Laboratory Findings) project.

This measure is co-funded with tax revenues based on the budget adopted by the members of the Saxon State Parliament.

Session Information

sessionInfo()

References



ampel-leipzig/ameld documentation built on Aug. 23, 2024, 7:31 p.m.