# How can I implement lasso in R using optim function

As you know lasso is a popular variable selection method of the form of

$(y-x\beta)'(y-X\beta)+\lambda \sum_i|\beta_i|$

the first is that it is possible to use optim() function in R to minimize this problem? a sample code can be like

x=matrix(rnorm(100),ncol=20)
y=rowSums(x)
f<-function(x,y,l,beta){
beta=as.matrix(beta)
sum((y-x%*% beta)^2) +l*sum(abs(beta))
}
optim(rep(0,ncol(x)),f,method='CG',x=x,y=y,l=1)


Other questions are, 2) is the code above true? 3) how can I force the coefficients to be exactly zero?

PLEASE NOTICE THAT I DONT WANT TO USE PACKAGES LIKE LARS, GLMNET or ... just optim or nlm functions. Thanks

• is this self study? Oct 23, 2014 at 19:00
• By construction, your coefficients will be estimated near 1. To drop the smallest coefficient(s), you will need to experiment with increasing the value of l. But it might be more illustrative to make one of the coefficients smaller than the others so that it is consistently dropped.
– Sycorax
Oct 23, 2014 at 19:28
• @user777 Thank you. why 1? can you explain that more? Oct 23, 2014 at 20:06
• You created y by summing over x.
– Sycorax
Oct 23, 2014 at 20:07
• Sorry, Yes it is. But if generate a sparse matrix and execute the code above you will see that the zero coefficients are not zero. My main challenge is tackling with this coefficients. Maybe I need to change the input data above to sparse data. Oct 23, 2014 at 20:11