Description Usage Arguments Details Value Author(s) References Examples
View source: R/utility_functions.R
Function to calculate the sequence of tuning parameters based on
the design matrix x
and the response variable y. This is used in
the shim_once
function to calculate the tuning parameters
applied to the main effects
1 2 | lambda_sequence(x, y, weights = NULL, lambda.factor = ifelse(nobs < nvars,
0.01, 1e-06), nlambda = 100, scale_x = F, center_y = F)
|
x |
Design matrix of dimension |
y |
response variable (matrix form) of dimension |
weights |
Separate penalty factors can be applied to each coefficient. This is a number that multiplies lambda to allow differential shrinkage, and can be used to apply adaptive LASSO. Can be 0 for some variables, which implies no shrinkage, and that variable is always included in the model. Default is 1 for all variables (and implicitly infinity for variables listed in exclude). Note: the penalty factors are internally rescaled to sum to nvars, and the lambda sequence will reflect this change. |
lambda.factor |
The factor for getting the minimal lambda in lambda
sequence, where |
nlambda |
the number of lambda values - default is 100. |
scale_x |
should the columns of x be scaled - default is FALSE |
center_y |
should y be mean centered - default is FALSE |
The maximum lambda is calculated using the following inequality:
(N*w_j)^-1 | ∑ x_ij y_i | ≤ λ_max
The minimum lambda is given by lambda.factor*lambda_max. The sequence of nlambda values are decreasing from lambda_max to lambda_min on the log scale.
The penalty factors are internally rescaled to sum to the number of predictor variables in glmnet. Therefore, to get the correct sequence of lambdas when there are weights, this function first rescales the weights and then calclated the sequence of lambdas.
This formula is taken from section 2.5 of the glmnet
paper in the
Journal of Statistical Software (see references for details)
numeric vector of length q
Sahir Bhatnagar
Maintainer: Sahir Bhatnagar sahir.bhatnagar@mail.mcgill.ca
Friedman, J., Hastie, T. and Tibshirani, R. (2008) Regularization Paths for Generalized Linear Models via Coordinate Descent, http://www.stanford.edu/~hastie/Papers/glmnet.pdf Journal of Statistical Software, Vol. 33(1), 1-22 Feb 2010 http://www.jstatsoft.org/v33/i01/
Yang, Y., & Zou, H. (2015). A fast unified algorithm for solving group-lasso penalize learning problems. Statistics and Computing, 25(6), 1129-1141. http://www.math.mcgill.ca/yyang/resources/papers/gglasso.pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | # number of observations
n <- 100
# number of predictors
p <- 5
# environment variable
e <- sample(c(0,1), n, replace = T)
# main effects
x <- cbind(matrix(rnorm(n*p), ncol = p), e)
# need to label columns
dimnames(x)[[2]] <- c(paste0("x",1:p), "e")
# design matrix without intercept
X <- model.matrix(~(x1+x2+x3+x4+x5)*e-1, data = as.data.frame(x))
# response
Y <- X %*% rbinom(ncol(X), 1, 0.2) + 3*rnorm(n)
lambda_sequence(X,Y)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.