| Title: | Group Elastic Net Regularized GLMs and GAMs |
|---|---|
| Description: | Efficient algorithms for fitting generalized linear and additive models with group elastic net penalties as described in Helwig (2025) <doi:10.1080/10618600.2024.2362232>. Implements group LASSO, group MCP, and group SCAD with an optional group ridge penalty. Computes the regularization path for linear regression (gaussian), multivariate regression (multigaussian), smoothed support vector machines (svm1), squared support vector machines (svm2), logistic regression (binomial), proportional odds logistic regression (ordinal), multinomial logistic regression (multinomial), log-linear count regression (poisson and negative.binomial), and log-linear continuous regression (gamma and inverse gaussian). Supports default and formula methods for model specification, k-fold cross-validation for tuning the regularization parameters, and nonparametric regression via tensor product reproducing kernel (smoothing spline) basis function expansion. |
| Authors: | Nathaniel E. Helwig [aut, cre] |
| Maintainer: | Nathaniel E. Helwig <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.2 |
| Built: | 2026-05-29 08:54:24 UTC |
| Source: | https://github.com/cran/grpnet |
Miles per gallon and other characteristics of vehicles from the 1970s-1980s. A version of this dataset was used as the 1983 American Statistical Association Exposition dataset.
data("auto")data("auto")
A data frame with 392 observations on the following 9 variables.
mpgmiles per gallon (numeric vector)
cylindersnumber of cylinders: 3,4,5,6,8 (ordered factor)
displacementengine displacement in cubic inches (numeric vector)
horsepowerengine horsepower (integer vector)
weightvehicle weight in of lbs. (integer vector)
acceleration0-60 mph time in sec. (numeric vector)
model.yearranging from 1970 to 1982 (integer vector)
originregion of origin: American, European, Japanese (factor vector)
This is a modified version of the "Auto MPG Data Set" on the UCI Machine Learning Repository, which is a modified version of the "cars" dataset on StatLib.
Compared to the version of the dataset in UCI's MLR, this version of the dataset has removed (i) the 6 rows with missing horsepower scores, and (ii) the last column giving the name of each vehicle (car.name).
The dataset was originally collected by Ernesto Ramos and David Donoho.
StatLib—Datasets Archive at Carnegie Mellon University http://lib.stat.cmu.edu/datasets/cars.data
Machine Learning Repository at University of California Irvine https://archive.ics.uci.edu/ml/datasets/Auto+MPG
# load data data(auto) # display structure str(auto) # display header head(auto) # see 'cv.grpnet' for cross-validation examples ?cv.grpnet # see 'grpnet' for fitting examples ?grpnet# load data data(auto) # display structure str(auto) # display header head(auto) # see 'cv.grpnet' for cross-validation examples ?cv.grpnet # see 'grpnet' for fitting examples ?grpnet
Obtain coefficients from a cross-validated group elastic net regularized GLM (cv.grpnet) or a group elastic net regularized GLM (grpnet) object.
## S3 method for class 'cv.grpnet' coef(object, s = c("lambda.1se", "lambda.min"), ...) ## S3 method for class 'grpnet' coef(object, s = NULL, ...)## S3 method for class 'cv.grpnet' coef(object, s = c("lambda.1se", "lambda.min"), ...) ## S3 method for class 'grpnet' coef(object, s = NULL, ...)
object |
Object of class "cv.grpnet" or "grpnet" |
s |
Lambda value(s) at which predictions should be obtained. For "cv.grpnet" objects, default uses the 1se solution. For "grpnet" objects, default uses |
... |
Additional arguments (ignored) |
coef.cv.grpnet:
Returns the coefficients that are used by the predict.cv.grpnet function to form predictions from a fit cv.grpnet object.
coef.grpnet:
Returns the coefficients that are used by the predict.grpnet function to form predictions from a fit grpnet object.
For multigaussian and multinomial response variables, returns a list of length length(object$ylev), where the j-th element is a matrix of dimension c(ncoef, length(s)) giving the coefficients for object$ylev[j].
For other response variables, returns a matrix of dimension c(ncoef, length(s)), where the i-th column gives the coefficients for s[i].
The syntax of these functions closely mimics that of the coef.cv.glmnet and coef.glmnet functions in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
print.coef.grpnet for printing coef.grpnet objects
predict.cv.grpnet for predicting from cv.grpnet objects
predict.grpnet for predicting from grpnet objects
######***###### grpnet ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # extract coefs for regularization path (output = 12 x 100 matrix) coef(mod) # extract coefs at 3 particular points (output = 12 x 3 matrix) coef(mod, s = c(1.5, 1, 0.5)) ######***###### cv.grpnet ######***###### # load data data(auto) # 5-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1) # extract coefs for "min" solution (output = 12 x 1 matrix) coef(mod) # extract coefs for "1se" solution (output = 12 x 1 matrix) coef(mod, s = "lambda.1se") # extract coefs at 3 particular points (output = 12 x 3 matrix) coef(mod, s = c(1.5, 1, 0.5))######***###### grpnet ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # extract coefs for regularization path (output = 12 x 100 matrix) coef(mod) # extract coefs at 3 particular points (output = 12 x 3 matrix) coef(mod, s = c(1.5, 1, 0.5)) ######***###### cv.grpnet ######***###### # load data data(auto) # 5-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1) # extract coefs for "min" solution (output = 12 x 1 matrix) coef(mod) # extract coefs for "1se" solution (output = 12 x 1 matrix) coef(mod, s = "lambda.1se") # extract coefs at 3 particular points (output = 12 x 3 matrix) coef(mod, s = c(1.5, 1, 0.5))
Creates a plot (default) or returns a data frame (otherwise) that compares the cross-validation error for multiple cv.grpnet fits.
cv.compare(x, s = c("lambda.1se", "lambda.min"), plot = TRUE, at = 1:length(x), nse = 1, point.col = "red", line.col = "gray", lwd = 2, bwd = 0.02, labels = NULL, xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL, ...)cv.compare(x, s = c("lambda.1se", "lambda.min"), plot = TRUE, at = 1:length(x), nse = 1, point.col = "red", line.col = "gray", lwd = 2, bwd = 0.02, labels = NULL, xlim = NULL, ylim = NULL, xlab = NULL, ylab = NULL, ...)
x |
|
s |
the tuning parameter value at which to plot results (if |
plot |
switch controlling whether a plot is produced (default) versus data frame. |
at |
x-axis coordinates for plotting the cv error for each solution. |
nse |
number of standard errors to use for error bars in plot. |
point.col |
color for point used to plot the average of the cv error. |
line.col |
color for lines used to plot the standard error for the cv error. |
lwd |
width of lines used to plot the standard error for the cv error. |
bwd |
width of standard error bars in terms of proportion of |
labels |
labels for x-axis tick marks. Defaults to |
xlim |
axis limits for abscissa (x-axis) |
ylim |
axis limits for ordinate (y-axis) |
xlab |
axis label for abscissa (x-axis) |
ylab |
axis label for ordinate (y-axis) |
... |
additional arguments passed to plotting functions. |
Default behavior creates a plot that displays the mean cv error +/- 1 se for each of the requested solutions.
If the input x is a single cv.grpnet object, then the function plots the lambda.min and lambda.1se solutions.
If the input x is a list of cv.grpnet objects, then the function plots either the lambda.min or the lambda.1se solution (controlled by s argument) for all of the input models.
When plot = TRUE, there is no return value (it produces a plot)
When plot = FALSE, a data.frame is returned with the mean cv error (and se) for each solution
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
plot.cv.grpnet for plotting cv error path (for all lambdas)
plot.grpnet for plotting regularization path (for single lambda)
# load data data(auto) # LASSO penalty set.seed(1) mod1 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1) # MCP penalty set.seed(1) mod2 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1, penaly = "MCP") # SCAD penalty set.seed(1) mod3 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1, penaly = "SCAD") # compare lambda.min and lambda.1se for mod1 cv.compare(mod1) # compare lambda.1se for mod1, mod2, mod3 cv.compare(x = list(mod1, mod2, mod3), labels = c("LASSO", "MCP", "SCAD"))# load data data(auto) # LASSO penalty set.seed(1) mod1 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1) # MCP penalty set.seed(1) mod2 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1, penaly = "MCP") # SCAD penalty set.seed(1) mod3 <- cv.grpnet(mpg ~ ., data = auto, nfolds = 5, alpha = 1, penaly = "SCAD") # compare lambda.min and lambda.1se for mod1 cv.compare(mod1) # compare lambda.1se for mod1, mod2, mod3 cv.compare(x = list(mod1, mod2, mod3), labels = c("LASSO", "MCP", "SCAD"))
Implements k-fold cross-validation for grpnet to find the regularization parameters that minimize the prediction error (deviance, mean squared error, mean absolute error, or misclassification rate).
cv.grpnet(x, ...) ## Default S3 method: cv.grpnet(x, y, group, weights = NULL, offset = NULL, alpha = c(0.01, 0.25, 0.5, 0.75, 1), gamma = c(3, 4, 5), type.measure = NULL, nfolds = 10, foldid = NULL, same.lambda = FALSE, parallel = FALSE, cluster = NULL, verbose = interactive(), adaptive = FALSE, power = 1, ...) ## S3 method for class 'formula' cv.grpnet(formula, data, use.rk = TRUE, weights = NULL, offset = NULL, alpha = c(0.01, 0.25, 0.5, 0.75, 1), gamma = c(3, 4, 5), type.measure = NULL, nfolds = 10, foldid = NULL, same.lambda = FALSE, parallel = FALSE, cluster = NULL, verbose = interactive(), adaptive = FALSE, power = 1, ...)cv.grpnet(x, ...) ## Default S3 method: cv.grpnet(x, y, group, weights = NULL, offset = NULL, alpha = c(0.01, 0.25, 0.5, 0.75, 1), gamma = c(3, 4, 5), type.measure = NULL, nfolds = 10, foldid = NULL, same.lambda = FALSE, parallel = FALSE, cluster = NULL, verbose = interactive(), adaptive = FALSE, power = 1, ...) ## S3 method for class 'formula' cv.grpnet(formula, data, use.rk = TRUE, weights = NULL, offset = NULL, alpha = c(0.01, 0.25, 0.5, 0.75, 1), gamma = c(3, 4, 5), type.measure = NULL, nfolds = 10, foldid = NULL, same.lambda = FALSE, parallel = FALSE, cluster = NULL, verbose = interactive(), adaptive = FALSE, power = 1, ...)
x |
Model (design) matrix of dimension |
y |
Response vector of length |
group |
Group label vector (factor, character, or integer) of length |
formula |
Model formula: a symbolic description of the model to be fitted. Uses the same syntax as |
data |
Optional data frame containing the variables referenced in |
use.rk |
If |
weights |
Optional vector of length |
offset |
Optional vector of length |
alpha |
Scalar or vector specifying the elastic net tuning parameter |
gamma |
Scalar or vector specifying the penalty hyperparameter |
type.measure |
Loss function for cross-validation. Options include: |
nfolds |
Number of folds for cross-validation. |
foldid |
Optional vector of length |
same.lambda |
Logical specfying if the same |
parallel |
Logical specifying if sequential computing (default) or parallel computing should be used. If |
cluster |
Optional cluster to use for parallel computing. If |
verbose |
Logical indicating if the fitting progress should be printed. Defaults to |
adaptive |
Logical indicating if the adaptive group elastic net should be used (see Note). |
power |
If |
... |
Optional additional arguments for |
This function calls the grpnet function nfolds+1 times: once on the full dataset to obtain the lambda sequence, and once holding out each fold's data to evaluate the prediction error. The syntax of (the default S3 method for) this function closely mimics that of the cv.glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Let denote the -th fold's data, let denote the full dataset excluding the -th fold's data, and let denote the coefficient estimates obtained from fitting the model to using the regularization parameter .
The cross-validation error for the -th fold is defined as
where denotes the cross-validation loss function that is specified by type.measure. For example, the "mse" loss function is defined as
where denotes the L2 norm.
The mean cross-validation error cvm is defined as
where is the total number of folds. The standard error cvsd is defined as
which is the classic definition of the standard error of the mean.
lambda |
regularization parameter sequence for the full data |
cvm |
mean cross-validation error for each |
cvsd |
estimated standard error of |
cvup |
upper curve: |
cvlo |
lower curve: |
nzero |
number of non-zero groups for each |
grpnet.fit |
fitted grpnet object for the full data |
lambda.min |
value of |
lambda.1se |
largest |
index |
two-element vector giving the indices of |
type.measure |
loss function for cross-validation (used for plot label) |
call |
matched call |
time |
runtime in seconds to perform k-fold CV tuning |
tune |
data frame containing the tuning results, i.e., min(cvm) for each combination of |
When adaptive = TRUE, the adaptive group elastic net is used:
(1) an initial fit with alpha = 0 estimates the penalty.factor
(2) a second fit using estimated penalty.factor is returned
lambda.1se is defined as follows: minid <- which.min(cvm) min1se <- cvm[minid] + cvsd[minid] se1id <- which(cvm <= min1se)[1] lambda.1se <- lambda[se1id]
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
summary.cv.grpnet for calculating fitted values and importances
plot.cv.grpnet for plotting the cross-validation error curve
predict.cv.grpnet for predicting from cv.grpnet objects
grpnet for fitting group elastic net regularization paths
######***###### family = "gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto) # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "multigaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) set.seed(1) mod <- cv.grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm1") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm2") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "logit") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "binomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "multinomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin with 3 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "multinomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "ordinal" ######***###### # load data data(auto) # 10-fold cv (formula method, response = cylinders with 5 levels) set.seed(1) mod <- cv.grpnet(cylinders ~ ., data = auto, family = "ordinal") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "poisson" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "poisson") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "Gamma" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "Gamma") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod)######***###### family = "gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto) # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "multigaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) set.seed(1) mod <- cv.grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm1") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm2") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "logit") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "binomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "multinomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin with 3 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "multinomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "ordinal" ######***###### # load data data(auto) # 10-fold cv (formula method, response = cylinders with 5 levels) set.seed(1) mod <- cv.grpnet(cylinders ~ ., data = auto, family = "ordinal") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "poisson" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "poisson") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "Gamma" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "Gamma") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # print min and 1se solution info mod # plot cv error curve plot(mod) # summarize model summary(mod)
Takes in the family argument from grpnet and returns a list containing the information needed for fitting and/or tuning the model.
family.grpnet(object, theta = 1)family.grpnet(object, theta = 1)
object |
two options: (1) an object of class "grpnet" or "cv.grpnet"; or (2) a character specifying the exponential family: |
theta |
positive scalar that serves as an additional hyperparameter for various loss functions. svm1: additional parameter that controls the smoothing rate for the hinge loss function (see Note below). negative.binomial: size parameter such that the variance function is defined as |
There is only one available link function for each family:
* gaussian (identity):
* multigaussian (identity):
* svm1/svm2 (identity):
* binomial/logit (logit):
* multinomial (symmetric):
* ordinal (logit):
* poisson (log):
* negative.binomial (log):
* Gamma (log):
* inverse.gaussian (log):
List with components:
family |
same as input object, i.e., character specifying the family |
linkinv |
function for computing inverse of link function |
dev.resids |
function for computing deviance residuals |
For gaussian family, this returns the full output produced by gaussian.
For svm1 family, the quadratically smoothed hinge loss is defined as
where with denoting the response and denoting the linear predictor. Note that the svm1 loss function approaches the support vector machine (i.e., hinge) loss function as .
For svm2 family, the squared hinge loss is defined as
where with denoting the response and denoting the linear predictor. Note that the svm1 loss function approaches the support vector machine (i.e., hinge) loss function as .
For ordinal family, a cumulative link model is used, i.e.,
where is the probabilty that the response exceeds the -th ordered response category for .
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
visualize.loss for plotting loss functions
grpnet for fitting group elastic net regularization paths
cv.grpnet for k-fold cross-validation of lambda
family.grpnet("gaussian") family.grpnet("multigaussian") family.grpnet("svm1", theta = 0.1) family.grpnet("svm2") family.grpnet("logit") family.grpnet("binomial") family.grpnet("multinomial") family.grpnet("ordinal") family.grpnet("poisson") family.grpnet("negative.binomial", theta = 10) family.grpnet("Gamma") family.grpnet("inverse.gaussian")family.grpnet("gaussian") family.grpnet("multigaussian") family.grpnet("svm1", theta = 0.1) family.grpnet("svm2") family.grpnet("logit") family.grpnet("binomial") family.grpnet("multinomial") family.grpnet("ordinal") family.grpnet("poisson") family.grpnet("negative.binomial", theta = 10) family.grpnet("Gamma") family.grpnet("inverse.gaussian")
Fits generalized linear/additive models with a group elastic net penalty using an adaptively bounded gradient descent (ABGD) algorithm (Helwig, 2025). Predictor groups can be manually input (default S3 method) or inferred from the model (S3 "formula" method). The regularization path is computed at a data-generated (default) or user-provided sequence of lambda values.
grpnet(x, ...) ## Default S3 method: grpnet(x, y, group, family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "ordinal", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.05, 0.0001), lambda = NULL, penalty.factor = NULL, penalty = c("LASSO", "MCP", "SCAD"), gamma = 4, theta = 1, standardized = !orthogonalized, orthogonalized = TRUE, intercept = TRUE, thresh = 1e-04, maxit = 1e05, proglang = c("Fortran", "R"), standardize.response = FALSE, keep.data = TRUE, ...) ## S3 method for class 'formula' grpnet(formula, data, use.rk = TRUE, family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "ordinal", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.05, 0.0001), lambda = NULL, penalty.factor = NULL, penalty = c("LASSO", "MCP", "SCAD"), gamma = 4, theta = 1, standardized = !orthogonalized, orthogonalized = TRUE, thresh = 1e-04, maxit = 1e05, proglang = c("Fortran", "R"), standardize.response = FALSE, keep.data = TRUE, ...)grpnet(x, ...) ## Default S3 method: grpnet(x, y, group, family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "ordinal", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.05, 0.0001), lambda = NULL, penalty.factor = NULL, penalty = c("LASSO", "MCP", "SCAD"), gamma = 4, theta = 1, standardized = !orthogonalized, orthogonalized = TRUE, intercept = TRUE, thresh = 1e-04, maxit = 1e05, proglang = c("Fortran", "R"), standardize.response = FALSE, keep.data = TRUE, ...) ## S3 method for class 'formula' grpnet(formula, data, use.rk = TRUE, family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "ordinal", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), weights = NULL, offset = NULL, alpha = 1, nlambda = 100, lambda.min.ratio = ifelse(nobs < nvars, 0.05, 0.0001), lambda = NULL, penalty.factor = NULL, penalty = c("LASSO", "MCP", "SCAD"), gamma = 4, theta = 1, standardized = !orthogonalized, orthogonalized = TRUE, thresh = 1e-04, maxit = 1e05, proglang = c("Fortran", "R"), standardize.response = FALSE, keep.data = TRUE, ...)
x |
Model (design) matrix of dimension |
y |
Response vector of length |
group |
Group label vector (factor, character, or integer) of length |
formula |
Model formula: a symbolic description of the model to be fitted. Uses the same syntax as |
data |
Optional data frame containing the variables referenced in |
use.rk |
If |
family |
Character specifying the assumed distribution for the response variable. Partial matching is allowed. Options include |
weights |
Optional vector of length |
offset |
Optional vector of length |
alpha |
Regularization hyperparameter satisfying |
nlambda |
Number of |
lambda.min.ratio |
The proportion |
lambda |
Optional vector of user-supplied regularization parameter values. |
penalty.factor |
Default S3 method: vector of length S3 "formula" method: named list giving the non-negative penalty weight for terms specified in the formula. Incomplete lists are allowed. Any term that is specified in |
penalty |
Character specifying which (group) penalty to use: LASSO , MCP, or SCAD. |
gamma |
Penalty hyperparameter that satisfies |
theta |
For SVM1: additional ("smoothing") parameter, that controls the smoothing rate of the hinge loss function.
For negative binomial: additional ("size") parameter, where the variance function is defined as |
standardized |
Logical indicating whether the predictors should be groupwise standardized. If |
orthogonalized |
Logical indicating whether the predictors should be groupwise orthogonalized. If |
intercept |
Logical indicating whether an intercept term should be included in the model. Note that the intercept is always unpenalized. |
thresh |
Convergence threshold (tolerance). The algorithm is determined to have converged once the maximum relative change in the coefficients is below this threshold. See "Convergence" section. |
maxit |
Maximum number of iterations to allow. |
proglang |
Which programming language should be used to implement the ABGD algorithm? Options include |
standardize.response |
Should columns of response be standardized to have unit variance before fitting the model? Only applicable when |
keep.data |
Should the training data be saved? Note this is needed for the |
... |
Additional arguments used by the default or formula method. |
Consider a generalized linear model of the form
where is the conditional expectation of the response given the predictor vector , the function is a user-specified (invertible) link function, and are the unknown regression coefficients. Furthermore, suppose that the predictors are grouped, such as
where is the grouped predictor vector, and is the grouped coefficient vector.
Given observations, this function finds the that minimizes
where is the loss function with denoting the observed data, is the group elastic net penalty, and is the regularization parameter.
The loss function has the form
where are the user-supplied weights, and is the -th observation's contribution to the loss function. Note that denotes the negative log-likelihood function for the given family.
The group elastic net penalty function has the form
where is the user-specified alpha value,
is the group lasso penalty with denoting the -th group's penalty.factor, and
is the group ridge penalty. Note that denotes the squared Euclidean norm. When penalty %in% c("MCP", "SCAD"), the group L1 penalty is replaced by the group MCP or group SCAD penalty.
An object of class "grpnet" with the following elements:
call |
matched call |
a0 |
intercept sequence of length |
beta |
coefficient matrix of dimension |
alpha |
balance between the group L1 (lasso) and group L2 (ridge) penalty |
lambda |
sequence of regularization parameter values |
family |
exponential family defining the loss function |
dev.ratio |
proportion of (null) deviance explained for each |
nulldev |
null deviance for each |
df |
effective degrees of freedom for each |
nzgrp |
number of non-zero groups for each |
nzcoef |
number of non-zero coefficients for each |
xsd |
standard deviation of x for each group |
ylev |
levels of response variable (only for binomial and multinomial families) |
nobs |
number of observations |
group |
group label vector |
ngroups |
number of groups |
npasses |
number of iterations for each |
time |
runtime in seconds to compute regularization path |
offset |
logical indicating if an offset was included |
args |
list of input argument values |
formula |
input formula (possibly after expansion) |
term.labels |
terms that appear in formula (if applicable) |
rk.args |
arguments for rk.model.matrix function (if applicable) |
Important: When using the S3 "formula" method, the S3 "predict" method forms the model matrix for the predictions by applying the model formula to the new data. As a result, to ensure that the corresponding S3 "predict" method works correctly, some formulaic features should be avoided.
Polynomials: When including polynomial terms, the poly function should be used with option raw = TRUE. Default use of the poly function (with raw = FALSE) will work for fitting the model, but will result in invalid predictions for new data. Polynomials can also be included via the I function, but this isn't recommended because the polynomials terms wouldn't be grouped together, i.e., the terms x and I(x^2) would be treated as two separate groups of size one instead of a single group of size two.
Splines: B-splines (and other spline bases) can be included via the S3 "formula" method. However, to ensure reasonable predictions for new data, it is necessary to specify the knots directly. For example, if x is a vector with entries between zero and one, the code bs(x, df = 5) will *not* produce valid predictions for new data, but the code bs(x, knots = c(0.25, 0.5, 0.75), Boundary.knots = c(0, 1)) will work as intended. Instead of attempting to integrate a call to bs() or rk() into the model formula, it is recommended that splines be included via the use.rk = TRUE argument.
Unlike the glm function, the family argument of the grpnet function
* should be a character vector (not a family object)
* does not allow for specification of a link function
Currently, there is only one available link function for each family:
* gaussian (identity):
* multigaussian (identity):
* svm1/svm2 (identity):
* binomial/logit (logit):
* multinomial (symmetric):
* poisson (log):
* negative.binomial (log):
* Gamma (log):
* inverse.gaussian (log):
For "svm1", "svm2", and "logit" responses, three different possibilities exist for the input response:
1. vector coercible into a factor with two levels
2. matrix with two columns (# successes, # failures)
3. numeric vector with entries between -1 and 1
In this case, the weights argument should be used specify the total number of trials.
For "binomial" responses, three different possibilities exist for the input response:
1. vector coercible into a factor with two levels
2. matrix with two columns (# successes, # failures)
3. numeric vector with entries between 0 and 1
In this case, the weights argument should be used specify the total number of trials.
For "multinomial" responses, two different possibilities exist for the input response:
1. vector coercible into a factor with more than two levels
2. matrix of integers (counts) for each category level
The algorithm is determined to have converged once
where and is the thresh argument.
The syntax of (the default S3 method for) this function closely mimics that of the glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
summary.grpnet for calculating fitted values and importances
plot.grpnet for plotting the regularization path
predict.grpnet for predicting from grpnet objects
cv.grpnet for k-fold cross-validation of lambda
######***###### family = "gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "multigaussian" ######***###### # load data data(auto) # fit model (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) mod <- grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm1") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm2") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "logit") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "binomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "multinomial" ######***###### # load data data(auto) # fit model (formula method, response = origin with 3 levels) mod <- grpnet(origin ~ ., data = auto, family = "multinomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "ordinal" ######***###### # load data data(auto) # fit model (formula method, response = cylinders with 5 levels) mod <- grpnet(cylinders ~ ., data = auto, family = "ordinal") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "poisson" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "poisson") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "Gamma" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "Gamma") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod)######***###### family = "gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "multigaussian" ######***###### # load data data(auto) # fit model (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) mod <- grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm1") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm2") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "logit") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "binomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "multinomial" ######***###### # load data data(auto) # fit model (formula method, response = origin with 3 levels) mod <- grpnet(origin ~ ., data = auto, family = "multinomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "ordinal" ######***###### # load data data(auto) # fit model (formula method, response = cylinders with 5 levels) mod <- grpnet(cylinders ~ ., data = auto, family = "ordinal") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "poisson" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "poisson") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "Gamma" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "Gamma") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # print regularization path info mod # plot proportion of null deviance explained plot(mod) # summarize model summary(mod)
Plots the mean cross-validation error, along with lower and upper standard deviation curves, as a function of log(lambda).
## S3 method for class 'cv.grpnet' plot(x, cv = TRUE, sign.lambda = 1, nzero = TRUE, ...)## S3 method for class 'cv.grpnet' plot(x, cv = TRUE, sign.lambda = 1, nzero = TRUE, ...)
x |
Object of class "cv.grpnet" |
cv |
If |
sign.lambda |
Default plots |
nzero |
Should the number of non-zero groups be printed on the top of the x-axis? |
... |
Additional arguments passed to the |
Produces cross-validation plot only (i.e., nothing is returned).
No return value (produces a plot)
Syntax and functionality were modeled after the plot.cv.glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
cv.grpnet for k-fold cross-validation of lambda
plot.grpnet for plotting the regularization path
# see 'cv.grpnet' for plotting examples ?cv.grpnet# see 'cv.grpnet' for plotting examples ?cv.grpnet
Creates a profile plot of the reguarlization paths for a fit group elastic net regularized GLM (grpnet) object.
## S3 method for class 'grpnet' plot(x, type = c("dev.ratio", "coef", "imp", "norm", "znorm"), newx, newdata, intercept = FALSE, color.by.group = TRUE, col = NULL, ...)## S3 method for class 'grpnet' plot(x, type = c("dev.ratio", "coef", "imp", "norm", "znorm"), newx, newdata, intercept = FALSE, color.by.group = TRUE, col = NULL, ...)
x |
Object of class "grpnet" |
type |
What to plot on the Y-axis: "dev.ratio" for explained deviance, "coef" for coefficient values, "imp" for importance of each group's contribution, "norm" for L2 norm of coefficients for each group, or "znorm" for L2 norm of standardized coefficients for each group. |
newx |
Matrix of new |
newdata |
Data frame of new |
intercept |
Should the intercept be included in the plot? |
color.by.group |
If |
col |
If |
... |
Additional arguments passed to the |
Syntax and functionality were modeled after the plot.glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Produces a profile plot showing the requested type (y-axis) as a function of log(lambda) (x-axis).
If x is a multigaussian or multinomial model, the coefficients for each response dimension/class are plotted in a separate plot.
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
grpnet for fitting grpnet regularization paths
plot.cv.grpnet for plotting cv.grpnet objects
# see 'grpnet' for plotting examples ?grpnet# see 'grpnet' for plotting examples ?grpnet
Obtain predictions from a cross-validated group elastic net (cv.grpnet) object.
## S3 method for class 'cv.grpnet' predict(object, newx, newdata, s = c("lambda.1se", "lambda.min"), type = c("link", "response", "class", "terms", "importance", "coefficients", "nonzero", "groups", "ncoefs", "ngroups", "norm", "znorm"), ...)## S3 method for class 'cv.grpnet' predict(object, newx, newdata, s = c("lambda.1se", "lambda.min"), type = c("link", "response", "class", "terms", "importance", "coefficients", "nonzero", "groups", "ncoefs", "ngroups", "norm", "znorm"), ...)
object |
Object of class "cv.grpnet" |
newx |
Matrix of new |
newdata |
Data frame of new |
s |
Lambda value(s) at which predictions should be obtained. Can input a character ("lambda.min" or "lambda.1se") or a numeric vector. Default of "lambda.min" uses the |
type |
Type of prediction to return. "link" gives predictions on the link scale ( |
... |
Additional arguments (ignored) |
Predictions are calculated from the grpnet object fit to the full sample of data, which is stored as object$grpnet.fit
See predict.grpnet for further details on the calculation of the different types of predictions.
Depends on three factors...
1. the exponential family distribution
2. the length of the input s
3. the type of prediction requested
See predict.grpnet for details
Syntax is inspired by the predict.cv.glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
cv.grpnet for k-fold cross-validation of lambda
predict.grpnet for predicting from grpnet objects
######***###### family = "gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto) # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto) # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min)) ######***###### family = "multigaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) set.seed(1) mod <- cv.grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto) # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, s = "lambda.min") # compare mean absolute error for two solutions mean(abs(y - fit.1se)) mean(abs(y - fit.min)) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm1") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm2") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "logit") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "binomial") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "multinomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin with 3 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "multinomial") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "ordinal" ######***###### # load data data(auto) # 10-fold cv (formula method, response = cylinders with 5 levels) set.seed(1) mod <- cv.grpnet(cylinders ~ ., data = auto, family = "ordinal") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$cylinders == fit.1se) 1 - mean(auto$cylinders == fit.min) ######***###### family = "poisson" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "poisson") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$horsepower - fit.1se)) mean(abs(auto$horsepower - fit.min)) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$horsepower - fit.1se)) mean(abs(auto$horsepower - fit.min)) ######***###### family = "Gamma" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "Gamma") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min)) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min))######***###### family = "gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = mpg) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto) # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto) # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min)) ######***###### family = "multigaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) set.seed(1) mod <- cv.grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto) # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, s = "lambda.min") # compare mean absolute error for two solutions mean(abs(y - fit.1se)) mean(abs(y - fit.min)) ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm1") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "svm2") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "logit") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # 10-fold cv (default method, response = origin with 2 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "binomial") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "multinomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin with 3 levels) set.seed(1) mod <- cv.grpnet(origin ~ ., data = auto, family = "multinomial") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$origin == fit.1se) 1 - mean(auto$origin == fit.min) ######***###### family = "ordinal" ######***###### # load data data(auto) # 10-fold cv (formula method, response = cylinders with 5 levels) set.seed(1) mod <- cv.grpnet(cylinders ~ ., data = auto, family = "ordinal") # get predicted classes at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "class") # get predicted classes at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "class", s = "lambda.min") # compare misclassification rate for two solutions 1 - mean(auto$cylinders == fit.1se) 1 - mean(auto$cylinders == fit.min) ######***###### family = "poisson" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "poisson") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$horsepower - fit.1se)) mean(abs(auto$horsepower - fit.min)) ######***###### family = "negative.binomial" ######***###### # load data data(auto) # 10-fold cv (formula method, response = horsepower) set.seed(1) mod <- cv.grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$horsepower - fit.1se)) mean(abs(auto$horsepower - fit.min)) ######***###### family = "Gamma" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "Gamma") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min)) ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # 10-fold cv (formula method, response = origin) set.seed(1) mod <- cv.grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # get fitted values at "lambda.1se" fit.1se <- predict(mod, newdata = auto, type = "response") # get fitted values at "lambda.min" fit.min <- predict(mod, newdata = auto, type = "response", s = "lambda.min") # compare mean absolute error for two solutions mean(abs(auto$mpg - fit.1se)) mean(abs(auto$mpg - fit.min))
Obtain predictions from a fit group elastic net (grpnet) object.
## S3 method for class 'grpnet' predict(object, newx, newdata, s = NULL, type = c("link", "response", "class", "terms", "importance", "coefficients", "nonzero", "groups", "ncoefs", "ngroups", "norm", "znorm"), ...)## S3 method for class 'grpnet' predict(object, newx, newdata, s = NULL, type = c("link", "response", "class", "terms", "importance", "coefficients", "nonzero", "groups", "ncoefs", "ngroups", "norm", "znorm"), ...)
object |
Object of class "grpnet" |
newx |
Matrix of new |
newdata |
Data frame of new |
s |
Lambda value(s) at which predictions should be obtained. Default uses |
type |
Type of prediction to return. "link" gives predictions on the link scale ( |
... |
Additional arguments (ignored) |
When type == "link", the predictions for each have the form
where is the argument newx (or the design matrix created from newdata by applying object$formula) and is the coefficient vector corresponding to .
When type == "response", the predictions for each have the form
where is the inverse link function stored in object$family$linkinv.
When type == "class", the predictions for each have the form
where gives the predicted probability that each observation belongs to the -th category (for ) using the regularization parameter .
When type == "terms", the groupwise predictions for each have the form
where is the portion of the argument newx (or the design matrix created from newdata by applying object$formula) that corresponds to the -th term/group, and are the corresponding coefficients.
When type == "importance", the variable importance indices are defined as
where denotes a centering matrix, and . Note that , but some could be negative. When they are positive, gives the approximate proportion of model (explained) variation that is attributed to the -th term.
Depends on three factors...
1. the exponential family distribution
2. the length of the input s
3. the type of prediction requested
For most response variables, the typical output will be...
* |
a matrix of dimension |
* |
a vector of length |
For multigaussian and multinomial response variables, the typical output will be...
* |
an array of dimension |
* |
a matrix of dimension |
Note: if type == "class", then the output will be the same class as object$ylev. Otherwise, the output will be real-valued (or integer for the counts).
If type == "terms" and !(family %in% c("multigaussian","multinomial")), the output will be...
* |
an array of dimension |
* |
a matrix of dimension |
If type == "terms" and family %in% c("multigaussian","multinomial"), the output will be a list of length length(object$ylev) where each element gives the terms for the corresponding response dimension/class.
If type == "importance" and family != "multinomial", the output will be...
* |
a matrix of dimension |
* |
a vector of length |
If type == "importance" and family == "multinomial", the output will be a list of length length(object$ylev) where each element gives the importance for the corresponding response class. If length(s) == 1, the output will be simplified to matrix.
If type == "coefficients", the output will be the same as that produced by coef.grpnet.
If type == "nonzero", the output will be a list of length length(s) where each element is a vector of integers (indices).
If type == "groups", the output will be a list of length length(s) where each element is a vector of characters (term.labels).
If type %in% c("ncoefs", "ngroups"), the output will be a vector of length length(s) where each element is an integer.
If type == "norm", the output will be a matrix of dimension c(K, length(s)), where each cell gives the L2 norm for the corresponding group and smoothing parameter. Note that K denotes the number of groups.
Some internal code (e.g., used for the interpolation) is borrowed from the predict.glmnet function in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
grpnet for fitting grpnet regularization paths
predict.cv.grpnet for predicting from cv.grpnet objects
######***###### family = "gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto) # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "multigaussian" ######***###### # load data data(auto) # fit model (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) mod <- grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # get fitted values for regularization path (output = 392 x 2 x 100 array) fit.path <- predict(mod, newdata = auto) # get fitted values at 3 particular points (output = 392 x 2 x 3 array) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, s = lam) # compare rmse for solutions (mpg) rmse1.path <- sqrt(colMeans((y[,1] - fit.path[,1,])^2)) rmse1.some <- sqrt(colMeans((y[,1] - fit.some[,1,])^2)) plot(log(mod$lambda), rmse1.path, cex = 0.5) points(log(lam), rmse1.some, pch = 0, col = "red") # compare rmse for solutions (displacement) rmse2.path <- sqrt(colMeans((y[,2] - fit.path[,2,])^2)) rmse2.some <- sqrt(colMeans((y[,2] - fit.some[,2,])^2)) plot(log(mod$lambda), rmse2.path, cex = 0.5) points(log(lam), rmse2.some, pch = 0, col = "red") ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm1") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm2") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "logit") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "binomial") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "multinomial" ######***###### # load data data(auto) # fit model (formula method, response = origin with 3 levels) mod <- grpnet(origin ~ ., data = auto, family = "multinomial") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "ordinal" ######***###### # load data data(auto) # fit model (formula method, response = cylinders with 5 levels) mod <- grpnet(cylinders ~ ., data = auto, family = "ordinal") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$cylinders == fit.path) miss.some <- 1 - colMeans(auto$cylinders == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "poisson" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "poisson") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$horsepower - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$horsepower - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "negative.binomial" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$horsepower - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$horsepower - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "Gamma" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "Gamma") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red")######***###### family = "gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto) # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto) # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "multigaussian" ######***###### # load data data(auto) # fit model (formula method, response = (mpg, displacement)) y <- as.matrix(auto[,c(1,3)]) mod <- grpnet(y ~ ., data = auto[,-c(1,3)], family = "multigaussian", standardize.response = TRUE) # get fitted values for regularization path (output = 392 x 2 x 100 array) fit.path <- predict(mod, newdata = auto) # get fitted values at 3 particular points (output = 392 x 2 x 3 array) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, s = lam) # compare rmse for solutions (mpg) rmse1.path <- sqrt(colMeans((y[,1] - fit.path[,1,])^2)) rmse1.some <- sqrt(colMeans((y[,1] - fit.some[,1,])^2)) plot(log(mod$lambda), rmse1.path, cex = 0.5) points(log(lam), rmse1.some, pch = 0, col = "red") # compare rmse for solutions (displacement) rmse2.path <- sqrt(colMeans((y[,2] - fit.path[,2,])^2)) rmse2.some <- sqrt(colMeans((y[,2] - fit.some[,2,])^2)) plot(log(mod$lambda), rmse2.path, cex = 0.5) points(log(lam), rmse2.some, pch = 0, col = "red") ######***###### family = "svm1" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm1") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "svm2" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "svm2") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "logit" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "logit") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "binomial" ######***###### # load data data(auto) # redefine origin (Domestic vs Foreign) auto$origin <- ifelse(auto$origin == "American", "Domestic", "Foreign") # fit model (formula method, response = origin with 2 levels) mod <- grpnet(origin ~ ., data = auto, family = "binomial") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "multinomial" ######***###### # load data data(auto) # fit model (formula method, response = origin with 3 levels) mod <- grpnet(origin ~ ., data = auto, family = "multinomial") # get predicted classes for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get predicted classes at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$origin == fit.path) miss.some <- 1 - colMeans(auto$origin == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "ordinal" ######***###### # load data data(auto) # fit model (formula method, response = cylinders with 5 levels) mod <- grpnet(cylinders ~ ., data = auto, family = "ordinal") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "class") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "class", s = lam) # compare misclassification rate for solutions miss.path <- 1 - colMeans(auto$cylinders == fit.path) miss.some <- 1 - colMeans(auto$cylinders == fit.some) plot(log(mod$lambda), miss.path, cex = 0.5) points(log(lam), miss.some, pch = 0, col = "red") ######***###### family = "poisson" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "poisson") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$horsepower - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$horsepower - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "negative.binomial" ######***###### # load data data(auto) # fit model (formula method, response = horsepower) mod <- grpnet(horsepower ~ ., data = auto, family = "negative.binomial") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$horsepower - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$horsepower - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "Gamma" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "Gamma") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red") ######***###### family = "inverse.gaussian" ######***###### # load data data(auto) # fit model (formula method, response = mpg) mod <- grpnet(mpg ~ ., data = auto, family = "inverse.gaussian") # get fitted values for regularization path (output = 392 x 100 matrix) fit.path <- predict(mod, newdata = auto, type = "response") # get fitted values at 3 particular points (output = 392 x 3 matrix) lam <- quantile(mod$lambda, probs = c(0.25, 0.5, 0.75)) fit.some <- predict(mod, newdata = auto, type = "response", s = lam) # compare rmse for solutions rmse.path <- sqrt(colMeans((auto$mpg - fit.path)^2)) rmse.some <- sqrt(colMeans((auto$mpg - fit.some)^2)) plot(log(mod$lambda), rmse.path, cex = 0.5) points(log(lam), rmse.some, pch = 0, col = "red")
Prints some basic information about the coefficients (for coef.grpnet objects), observed cross-validation error (for cv.grpnet objects), or the computed regularization path (for grpnet objects).
## S3 method for class 'coef.grpnet' print(x, ...) ## S3 method for class 'cv.grpnet' print(x, digits = max(3, getOption("digits") - 3), ...) ## S3 method for class 'grpnet' print(x, ...)## S3 method for class 'coef.grpnet' print(x, ...) ## S3 method for class 'cv.grpnet' print(x, digits = max(3, getOption("digits") - 3), ...) ## S3 method for class 'grpnet' print(x, ...)
x |
an object of class |
digits |
the number of digits to print (must be a positive integer) |
... |
additional arguments for |
For coef.grpnet objects, prints the non-zero coefficients and uses "." for coefficients shrunk to zero.
For cv.grpnet objects, prints the function call, the cross-validation type.measure, and a two-row table with information about the min and 1se solutions.
For grpnet objects, prints a data frame with columns
* nGrp: number of non-zero groups for each lambda
* Df: effective degrees of freedom for each lambda
* %Dev: percentage of null deviance explained for each lambda
* Lambda: the values of lambda
No return value (produces a printout)
Some syntax and functionality were modeled after the print functions in the glmnet package (Friedman, Hastie, & Tibshirani, 2010).
Nathaniel E. Helwig <[email protected]>
Friedman, J., Hastie, T., & Tibshirani, R. (2010). Regularization paths for generalized linear models via coordinate descent. Journal of Statistical Software, 33(1), 1-22. doi:10.18637/jss.v033.i01
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
coef.grpnet for extracting coefficients
cv.grpnet for k-fold cross-validation of lambda
grpnet for fitting grpnet regularization paths
# see 'coef.grpnet' for coefficient printing examples ?coef.grpnet # see 'cv.grpnet' for cross-validation error printing examples ?cv.grpnet # see 'grpnet' for regularization path printing examples ?grpnet# see 'coef.grpnet' for coefficient printing examples ?coef.grpnet # see 'cv.grpnet' for cross-validation error printing examples ?cv.grpnet # see 'grpnet' for regularization path printing examples ?grpnet
Generate a reproducing kernel basis matrix for a nominal, ordinal, or polynomial smoothing spline.
rk(x, df = NULL, knots = NULL, m = NULL, intercept = FALSE, Boundary.knots = NULL, warn.outside = TRUE, periodic = FALSE, xlev = levels(x))rk(x, df = NULL, knots = NULL, m = NULL, intercept = FALSE, Boundary.knots = NULL, warn.outside = TRUE, periodic = FALSE, xlev = levels(x))
x |
the predictor vector of length |
df |
the degrees of freedom, i.e., number of knots to place at quantiles of |
knots |
the breakpoints (knots) defining the spline. If |
m |
the derivative penalty order: 0 = ordinal spline, 1 = linear spline, 2 = cubic spline, 3 = quintic spline |
intercept |
should an intercept be included in the basis? |
Boundary.knots |
the boundary points for spline basis. Defaults to |
warn.outside |
if |
periodic |
should the spline basis functions be constrained to be periodic with respect to the |
xlev |
levels of |
Given a vector of function realizations , suppose that , where is the (unregularized) spline basis and is the coefficient vector. Let denote the postive semi-definite penalty matrix, such that defines the roughness penalty for the spline. See Helwig (2017) for the form of and for the various types of splines.
Consider the spectral parameterization of the form where
is the regularized spline basis (that is returned by this function), and are the reparameterized coefficients. Note that and , so the spectral parameterization absorbs the penalty into the coefficients (see Helwig, 2021, 2024).
Syntax of this function is designed to mimic the syntax of the bs function.
Returns a basis function matrix of dimension n by df (plus 1 if an intercept is included) with the following attributes:
df |
degrees of freedom |
knots |
knots for spline basis |
m |
derivative penalty order |
intercept |
was an intercept included? |
Boundary.knots |
boundary points of |
periodic |
is the basis periodic? |
xlev |
factor levels (if applicable) |
The (default) type of spline basis depends on the class of the input x object:
* If x is an unordered factor, then a nominal spline basis is used
* If x is an ordered factor (and m = NULL), then an ordinal spline basis is used
* If x is an integer or numeric (and m = NULL), then a cubic spline basis is used
Note that you can override the default behavior by specifying the m argument.
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2017). Regression with ordered predictors via ordinal smoothing splines. Frontiers in Applied Mathematics and Statistics, 3(15), 1-13. doi:10.3389/fams.2017.00015
Helwig, N. E. (2021). Spectrally sparse nonparametric regression via elastic net regularized smoothers. Journal of Computational and Graphical Statistics, 30(1), 182-191. doi:10.1080/10618600.2020.1806855
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
######***###### NOMINAL SPLINE BASIS ######***###### x <- as.factor(LETTERS[1:5]) basis <- rk(x) plot(1:5, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(1:5, basis[,j], col = j) } ######***###### ORDINAL SPLINE BASIS ######***###### x <- as.ordered(LETTERS[1:5]) basis <- rk(x) plot(1:5, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(1:5, basis[,j], col = j) } ######***###### LINEAR SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x, m = 1) plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) } ######***###### CUBIC SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x) basis <- scale(basis) # for visualization only! plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) } ######***###### QUINTIC SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x, m = 3) basis <- scale(basis) # for visualization only! plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) }######***###### NOMINAL SPLINE BASIS ######***###### x <- as.factor(LETTERS[1:5]) basis <- rk(x) plot(1:5, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(1:5, basis[,j], col = j) } ######***###### ORDINAL SPLINE BASIS ######***###### x <- as.ordered(LETTERS[1:5]) basis <- rk(x) plot(1:5, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(1:5, basis[,j], col = j) } ######***###### LINEAR SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x, m = 1) plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) } ######***###### CUBIC SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x) basis <- scale(basis) # for visualization only! plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) } ######***###### QUINTIC SPLINE BASIS ######***###### x <- seq(0, 1, length.out = 101) basis <- rk(x, m = 3) basis <- scale(basis) # for visualization only! plot(x, basis[,1], t = "l", ylim = extendrange(basis)) for(j in 2:ncol(basis)){ lines(x, basis[,j], col = j) }
Creates a design (or model) matrix using the rk function to expand variables via a reproducing kernel basis.
rk.model.matrix(object, data = environment(object), ...)rk.model.matrix(object, data = environment(object), ...)
object |
|
data |
a data frame containing the variables referenced in |
... |
additional arguments passed to the |
Designed to be a more flexible alternative to the model.matrix function. The rk function is used to construct a marginal basis for each variable that appears in the input object. Tensor product interactions are formed by taking a row.kronecker product of marginal basis matrices. Interactions of any order are supported using standard formulaic conventions, see Note.
The design matrix corresponding to the input formula and data, which has the following attributes:
assign |
an integer vector with an entry for each column in the matrix giving the term in the formula which gave rise to the column |
term.labels |
a character vector containing the labels for each of the terms in the model |
knots |
a named list giving the knots used for each variable in the formula |
m |
a named list giving the penalty order used for each variable in the formula |
periodic |
a named list giving the periodicity used for each variable in the formula |
xlev |
a named list giving the factor levels used for each variable in the formula |
For formulas of the form y ~ x + z, the constructed model matrix has the form cbind(rk(x), rk(z)), which simply concatenates the two marginal basis matrices. For formulas of the form y ~ x : z, the constructed model matrix has the form row.kronecker(rk(x), rk(z)), where row.kronecker denotes the row-wise kronecker product. The formula y ~ x * z is a shorthand for y ~ x + z + x : z, which concatenates the two previous results. Unless it is suppressed (using 0+), the first column of the basis will be a column of ones named (Intercept).
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2017). Regression with ordered predictors via ordinal smoothing splines. Frontiers in Applied Mathematics and Statistics, 3(15), 1-13. doi:10.3389/fams.2017.00015
Helwig, N. E. (2021). Spectrally sparse nonparametric regression via elastic net regularized smoothers. Journal of Computational and Graphical Statistics, 30(1), 182-191. doi:10.1080/10618600.2020.1806855
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
See rk for details on the reproducing kernel basis
# load auto data data(auto) # additive effects x <- rk.model.matrix(mpg ~ ., data = auto) dim(x) # check dimensions attr(x, "assign") # check group assignments attr(x, "term.labels") # check term labels # two-way interactions x <- rk.model.matrix(mpg ~ . * ., data = auto) dim(x) # check dimensions attr(x, "assign") # check group assignments attr(x, "term.labels") # check term labels # specify df for horsepower, weight, and acceleration # note: default df = 5 is used for displacement and model.year df <- list(horsepower = 6, weight = 7, acceleration = 8) x <- rk.model.matrix(mpg ~ ., data = auto, df = df) sapply(attr(x, "knots"), length) # check df # specify knots for model.year # note: default knots are selected for other variables knots <- list(model.year = c(1970, 1974, 1978, 1982)) x <- rk.model.matrix(mpg ~ ., data = auto, knots = knots) sapply(attr(x, "knots"), length) # check df# load auto data data(auto) # additive effects x <- rk.model.matrix(mpg ~ ., data = auto) dim(x) # check dimensions attr(x, "assign") # check group assignments attr(x, "term.labels") # check term labels # two-way interactions x <- rk.model.matrix(mpg ~ . * ., data = auto) dim(x) # check dimensions attr(x, "assign") # check group assignments attr(x, "term.labels") # check term labels # specify df for horsepower, weight, and acceleration # note: default df = 5 is used for displacement and model.year df <- list(horsepower = 6, weight = 7, acceleration = 8) x <- rk.model.matrix(mpg ~ ., data = auto, df = df) sapply(attr(x, "knots"), length) # check df # specify knots for model.year # note: default knots are selected for other variables knots <- list(model.year = c(1970, 1974, 1978, 1982)) x <- rk.model.matrix(mpg ~ ., data = auto, knots = knots) sapply(attr(x, "knots"), length) # check df
Calculates the row-wise Kronecker product between two matrices with the same number of rows.
row.kronecker(X, Y)row.kronecker(X, Y)
X |
matrix of dimension |
Y |
matrix of dimension |
Given X of dimension c(n, p) and Y of dimension c(n, q), this function returns
cbind(x[,1] * Y, x[,2] * Y, ..., x[,p] * Y)
which is a matrix of dimension c(n, p*q)
Matrix of dimension where each row contains the Kronecker product between the corresponding rows of X and Y.
Nathaniel E. Helwig <[email protected]>
Used by the rk.model.matrix to construct basis functions for interaction terms
See kronecker for the regular kronecker product
X <- matrix(c(1, 1, 2, 2), nrow = 2, ncol = 2) Y <- matrix(1:6, nrow = 2, ncol = 3) row.kronecker(X, Y)X <- matrix(c(1, 1, 2, 2), nrow = 2, ncol = 2) Y <- matrix(1:6, nrow = 2, ncol = 3) row.kronecker(X, Y)
Prints the startup message when grpnet is loaded. Not intended to be called by the user.
The ‘grpnet’ ascii start-up message was created using the taag software.
https://patorjk.com/software/taag/
Obtain fitted values, variable activations, variable importances from a fit group elastic net (grpnet) object.
## S3 method for class 'cv.grpnet' summary(object, ...)## S3 method for class 'cv.grpnet' summary(object, ...)
object |
Object of class "cv.grpnet" |
... |
Additional arguments passed to |
Passes the training data to the predict.cv.grpnet function four times: two predictions types (fit and imp) by two lambda values (lambda.1se and lambda.min).
family |
Name of exponential family/loss function. |
penalty |
Name of L1 group penalty. |
nobs |
Number of observations. |
ngroups |
Number of coefficient groups. |
lambda |
Vector of lambda values (1se and min). |
dev.ratio |
Vector of proportions of null deviance explained (1se and min). |
fit |
Fitted values obtained from |
act |
Variable activations obtained from |
imp |
Variable importances obtained from |
The variables activations are defined as: act <- abs(imp) > 0.0
For most response families, the returned components fit, act, and imp are matrices of dimension K by 2.
For families with multivariate responses (i.e., multigaussian and multinomial), these components are arrays of dimension K by L by 2 where L is the dimension of the response.
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
cv.grpnet for k-fold cross-validation of lambda
# see 'cv.grpnet' for summary examples ?cv.grpnet# see 'cv.grpnet' for summary examples ?cv.grpnet
Obtain fitted values, variable activations, variable importances from a fit group elastic net (grpnet) object.
## S3 method for class 'grpnet' summary(object, ...)## S3 method for class 'grpnet' summary(object, ...)
object |
Object of class "grpnet" |
... |
Additional arguments passed to |
Passes the training data to the predict.grpnet function twice: (1) to calculate fitted values, and (2) to calculate variable importances.
family |
Name of exponential family/loss function. |
penalty |
Name of L1 group penalty. |
nobs |
Number of observations. |
ngroups |
Number of coefficient groups. |
lambda |
Vector of lambda values. |
dev.ratio |
Vector of proportions of null deviance explained. |
fit |
Fitted values obtained from |
act |
Variable activations obtained from |
imp |
Variable importances obtained from |
The variables activations are defined as: act <- abs(imp) > 0.0
For most response families, the returned components fit, act, and imp are matrices of dimension K by nlambda.
For families with multivariate responses (i.e., multigaussian and multinomial), these components are arrays of dimension K by L by nlambda where L is the dimension of the response.
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
grpnet for fitting grpnet regularization paths
# see 'grpnet' for summary examples ?grpnet# see 'grpnet' for summary examples ?grpnet
Makes a plot or returns a data frame containing the specified loss function evaluated at a sequence of input values.
visualize.loss(x = seq(-3, 3, length.out = 1001), family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), theta = 1, type = c("link", "response"), y = NULL, plot = TRUE, add = FALSE, ...)visualize.loss(x = seq(-3, 3, length.out = 1001), family = c("gaussian", "multigaussian", "svm1", "svm2", "logit", "binomial", "multinomial", "poisson", "negative.binomial", "Gamma", "inverse.gaussian"), theta = 1, type = c("link", "response"), y = NULL, plot = TRUE, add = FALSE, ...)
x |
sequence of linear predictor values at which to evaluate the loss. |
family |
Character specifying the assumed distribution for the response variable. Partial matching is allowed. See options below. |
theta |
For SVM1: additional ("smoothing") parameter, that controls the smoothing rate of the hinge loss function.
For negative binomial: additional ("size") parameter, where the variance function is defined as |
type |
Default of |
y |
Response value used to compute loss. Note that the loss function is interpreted as a function of |
plot |
if |
add |
if |
... |
additional arguments passed to |
grpnet implements the following loss functions:
L =
where
is a constant with respect to
If plot = TRUE, a plot is produced and nothing is returned.
If plot = FALSE, a data frame is returned with columns:
eta |
linear predictor, i.e., fitted values on link scale (same as input |
mu |
expected value, i.e., fitted values on response scale ( |
loss |
loss function evaluation for given |
Nathaniel E. Helwig <[email protected]>
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
visualize.penalty for plotting penalty function
visualize.shrink for plotting shrinkage operator
visualize.loss(family = "gaussian") visualize.loss(family = "svm1", theta = 0.1) visualize.loss(family = "svm2") visualize.loss(family = "logit") visualize.loss(family = "binomial") visualize.loss(family = "poisson") visualize.loss(family = "negative.binomial", theta = 10) visualize.loss(family = "Gamma") visualize.loss(family = "inverse.gaussian")visualize.loss(family = "gaussian") visualize.loss(family = "svm1", theta = 0.1) visualize.loss(family = "svm2") visualize.loss(family = "logit") visualize.loss(family = "binomial") visualize.loss(family = "poisson") visualize.loss(family = "negative.binomial", theta = 10) visualize.loss(family = "Gamma") visualize.loss(family = "inverse.gaussian")
Makes a plot or returns a data frame containing the group elastic net penalty (or its derivative) evaluated at a sequence of input values.
visualize.penalty(x = seq(-5, 5, length.out = 1001), penalty = c("LASSO", "MCP", "SCAD"), alpha = 1, lambda = 1, gamma = 4, derivative = FALSE, plot = TRUE, subtitle = TRUE, legend = TRUE, location = ifelse(derivative, "bottom", "top"), ...)visualize.penalty(x = seq(-5, 5, length.out = 1001), penalty = c("LASSO", "MCP", "SCAD"), alpha = 1, lambda = 1, gamma = 4, derivative = FALSE, plot = TRUE, subtitle = TRUE, legend = TRUE, location = ifelse(derivative, "bottom", "top"), ...)
x |
sequence of values at which to evaluate the penalty. |
penalty |
which penalty or penalties should be plotted? |
alpha |
elastic net tuning parameter (between 0 and 1). |
lambda |
overall tuning parameter (non-negative). |
gamma |
additional hyperparameter for MCP (>1) or SCAD (>2). |
derivative |
if |
plot |
if |
subtitle |
if |
legend |
if |
location |
the legend's location; ignored if |
... |
additional arguments passed to |
The group elastic net penalty is defined as
where denotes the L1 penalty (LASSO, MCP, or SCAD), denotes the Euclidean norm, is the L1 tuning parameter, and is the L2 tuning parameter. Note that and denote the lambda and alpha arguments.
If plot = TRUE, then produces a plot.
If plot = FALSE, then returns a data frame.
Nathaniel E. Helwig <[email protected]>
Fan, J., & Li, R. (2001). Variable selection via nonconcave penalized likelihood and its oracle properties. Journal of the American Statistical Association, 96(456), 1348-1360. doi:10.1198/016214501753382273
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
Tibshirani, R. (1996). Regression and shrinkage via the Lasso. Journal of the Royal Statistical Society, Series B, 58, 267-288. doi:10.1111/j.2517-6161.1996.tb02080.x
Zhang, C. H. (2010). Nearly unbiased variable selection under minimax concave penalty. The Annals of Statistics, 38(2), 894-942. doi:10.1214/09-AOS729
visualize.loss for plotting loss functions
visualize.shrink for plotting shrinkage operator
# plot penalty functions visualize.penalty() # plot penalty derivatives visualize.penalty(derivative = TRUE)# plot penalty functions visualize.penalty() # plot penalty derivatives visualize.penalty(derivative = TRUE)
Makes a plot or returns a data frame containing the group elastic net shrinkage operator (or its estimator) evaluated at a sequence of input values.
visualize.shrink(x = seq(-5, 5, length.out = 1001), penalty = c("LASSO", "MCP", "SCAD"), alpha = 1, lambda = 1, gamma = 4, fitted = FALSE, plot = TRUE, subtitle = TRUE, legend = TRUE, location = "top", ...)visualize.shrink(x = seq(-5, 5, length.out = 1001), penalty = c("LASSO", "MCP", "SCAD"), alpha = 1, lambda = 1, gamma = 4, fitted = FALSE, plot = TRUE, subtitle = TRUE, legend = TRUE, location = "top", ...)
x |
sequence of values at which to evaluate the penalty. |
penalty |
which penalty or penalties should be plotted? |
alpha |
elastic net tuning parameter (between 0 and 1). |
lambda |
overall tuning parameter (non-negative). |
gamma |
additional hyperparameter for MCP (>1) or SCAD (>2). |
fitted |
if |
plot |
if |
subtitle |
if |
legend |
if |
location |
the legend's location; ignored if |
... |
additional arguments passed to |
The updates for the group elastic net estimator have the form
where is a shrinkage and selection operator, and
is the unpenalized update with denoting the current gradient.
Note that is the L1 tuning parameter, is the L2 tuning parameter, is an upper-bound on the weights appearing in the Fisher information matrix, and is the largest eigenvalue of the Gramm matrix .
If plot = TRUE, then produces a plot.
If plot = FALSE, then returns a data frame.
Nathaniel E. Helwig <[email protected]>
Fan, J., & Li, R. (2001). Variable selection via nonconcave penalized likelihood and its oracle properties. Journal of the American Statistical Association, 96(456), 1348-1360. doi:10.1198/016214501753382273
Helwig, N. E. (2025). Versatile descent algorithms for group regularization and variable selection in generalized linear models. Journal of Computational and Graphical Statistics, 34(1), 239-252. doi:10.1080/10618600.2024.2362232
Tibshirani, R. (1996). Regression and shrinkage via the Lasso. Journal of the Royal Statistical Society, Series B, 58, 267-288. doi:10.1111/j.2517-6161.1996.tb02080.x
Zhang, C. H. (2010). Nearly unbiased variable selection under minimax concave penalty. The Annals of Statistics, 38(2), 894-942. doi:10.1214/09-AOS729
visualize.loss for plotting loss functions
visualize.penalty for plotting penalty function
# plot shrinkage operator visualize.shrink() # plot shrunken estimator visualize.shrink(fitted = TRUE)# plot shrinkage operator visualize.shrink() # plot shrunken estimator visualize.shrink(fitted = TRUE)