Dear all, I want to do the following process as a loop ( to run automatically with dimension of X, here 50). How can I do that? Your cooments will be highly appreciable. Alex *# Code:* library(lars) library(chemometrics) X<-matrix(rnorm(2500),ncol=50) dim(X) # [1] 50 50 X1<-X[,2:dim(X)[2]] # I have taken out first column dim(X1) #[1] 50 49 X2<-X1[2:dim(X1)[1],] # new X2 is constructed dim(X2) #[1] 49 49 y<-as.matrix(X1[1,]) # Now first row of the X1 acts a response vector dim(y) # [1] 49 1 # application of LASSO regression where y is response and X2 is a design matrix data1<-data.frame(y,X2=I(X2)) lasso_res=lassoCV(y~X2,data=data1,K=10,fraction=seq(0.1,1,by=0.1),use.Gram=FALSE) # to get optimum value of Cross Validation lasso_coef=lassocoef(y~X2,data=data1,sopt=lasso_res$sopt,use.Gram=FALSE) # to get the coefficients [[alternative HTML version deleted]]
Hi Jiim, Thanks . I want to do the following: 1. each time I need to drop one column, say first column 1 from matrix X. 2 then take out row 1 of the remainning matrix and that row becomes response (y) 3. do lasso regression on remaining X to y. 4. store the coefficients Similarly, in next run 1. I need to drop 2nd column, from matrix X. 2 then take out row 2 of the remainning matrix and that row becomes response (y) 3. do lasso regression on remaining X ( in example: X2to y.) 4. store the coefficients repeat On Sat, Jun 13, 2009 at 7:19 PM, jim holtman <jholtman@gmail.com> wrote:> It is not exactly clear what you want to iterate on. What is going to be > changing each time through the loop? > > On Sat, Jun 13, 2009 at 10:09 AM, Alex Roy <alexroy2008@gmail.com>wrote: > >> Dear all, >> I want to do the following process as a loop ( to run >> automatically with dimension of X, here 50). How can I do that? Your >> cooments will be highly appreciable. >> >> Alex >> >> >> *# Code:* >> >> library(lars) >> library(chemometrics) >> >> X<-matrix(rnorm(2500),ncol=50) >> dim(X) >> # [1] 50 50 >> X1<-X[,2:dim(X)[2]] # I have taken out first column >> dim(X1) >> #[1] 50 49 >> X2<-X1[2:dim(X1)[1],] # new X2 is constructed >> dim(X2) >> #[1] 49 49 >> y<-as.matrix(X1[1,]) # Now first row of the X1 acts a response vector >> dim(y) >> # [1] 49 1 >> >> # application of LASSO regression where y is response and X2 is a design >> matrix >> >> data1<-data.frame(y,X2=I(X2)) >> >> lasso_res=lassoCV(y~X2,data=data1,K=10,fraction=seq(0.1,1,by=0.1),use.Gram=FALSE) >> # to get optimum value of Cross Validation >> lasso_coef=lassocoef(y~X2,data=data1,sopt=lasso_res$sopt,use.Gram=FALSE) >> # >> to get the coefficients >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? >[[alternative HTML version deleted]]
Alex Roy <alexroy2008 at gmail.com> [Sun, Jun 14, 2009 at 06:43:52AM CEST]:> Hi Jiim, Thanks . I want to do the following: > > 1. each time I need to drop one column, say first column 1 from matrix X. > 2 then take out row 1 of the remainning matrix and that row becomes > response (y) > 3. do lasso regression on remaining X to y. > 4. store the coefficients > > Similarly, in next run > > 1. I need to drop 2nd column, from matrix X. > 2 then take out row 2 of the remainning matrix and that row becomes > response (y) > 3. do lasso regression on remaining X ( in example: X2to y.) > 4. store the coefficientsYou may have reasons you want to do this, to me it looks a bit peculiar, but then I am not too much an expert on penalized regression. Care to share some thoughts on the theory behind what you are doing? The following may work (I did not bother to install chemometrics, so untested): library(lars) library(chemometrics) nr <- 50 X <- matrix(rnorm(nr**nr),ncol=nr) sapply(1:nr, function(i) { lasso_res=lassoCV(X[i] ~ X[-i],data=data1,K=10,fraction=seq(0.1,1,by=0.1),use.Gram=FALSE) # to get optimum value of Cross Validation lasso_coef=lassocoef(X[i] ~ X[-i],data=data1,sopt=lasso_res$sopt,use.Gram=FALSE)}) -- Johannes H?sing There is something fascinating about science. One gets such wholesale returns of conjecture mailto:johannes at huesing.name from such a trifling investment of fact. http://derwisch.wikidot.com (Mark Twain, "Life on the Mississippi")