Sacha Epskamp - 1 year ago 89

R Question

The

`glmnet`

`LASSO`

`lambda`

`lambda_max`

`glmnet`

`lambda_max`

`set.seed(1)`

library("glmnet")

x <- matrix(rnorm(100*20),100,20)

y <- rnorm(100)

fitGLM <- glmnet(x,y)

max(fitGLM$lambda)

# 0.1975946

The package vignette (http://www.jstatsoft.org/v33/i01/paper) describes in section 2.5 that it computes this value as follows:

`sx <- as.matrix(scale(x))`

sy <- as.vector(scale(y))

max(abs(colSums(sx*sy)))/100

# 0.1865232

Which clearly is close but not the same value. So, what causes this difference? And in a related question, how could I compute

`lambda_max`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

To get the same result you need to standardize the variables using a standard deviation with `n`

instead of `n-1`

denominator.

```
mysd <- function(y) sqrt(sum((y-mean(y))^2)/length(y))
sx <- scale(x,scale=apply(x,2,mysd))
sx <- as.matrix(sx,ncol=20,nrow=100)
sy <- as.vector(scale(y,scale=mysd))
max(abs(colSums(sx*sy)))/100
## [1] 0.1758808
fitGLM <- glmnet(sx,sy)
max(fitGLM$lambda)
## [1] 0.1758808
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**