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
l
. But it might be more illustrative to make one of the coefficients smaller than the others so that it is consistently dropped. $\endgroup$y
by summing overx
. $\endgroup$