loop problem for extract coefficients
Hi Alex,
On Sun, April 5, 2009 16:49, Alex Roy wrote:
Dear R users,
I have problem with extracting coefficients from a
object. Here, X (predictor)and Y (response) are two matrix , I am
regressing
X ( dimensions 10 x 20) on each of columns of Y[,1] (10 x 1) and want to
store the coefficient values. I have performed a Elastic Net regression
and
I want to store the coeffcients in each iteration. I got an error message
.
I do not know where is the problem???? Please help me.
Thanks
*Code:*
-------------------------------------------------------
library(elasticnet)
X<-matrix(rnorm(200),ncol=20)
Y<-matrix(rnorm(200),ncol=20)
loop <- 20
size <- 20
enres<-matrix(nrow = size, ncol = loop)
fit<-matrix(nrow = size, ncol = loop)
store<-matrix(nrow = size, ncol = loop)
for(j in 1: 10)
print (paste(j,"/200",sep=""))
{
enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
fit<-predict.enet(enres, X, type="coefficients")
store[,j]<-fit$coefficients
}
----------------------------------------------------
The problem is that only the print statement is inside the for loop. I
would suggest:
for(j in 1: 10)
{ # here the opening bracket
print (paste(j,"/200",sep=""))
# and not here!
enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE)
fit<-predict.enet(enres, X, type="coefficients")
store[,j]<-fit$coefficients
}
Hope this helps,
Arien
library(elasticnet)
Loading required package: lars
X<-matrix(rnorm(200),ncol=20) Y<-matrix(rnorm(200),ncol=20) loop <- 20 size <- 20 enres<-matrix(nrow = size, ncol = loop) fit<-matrix(nrow = size, ncol = loop) store<-matrix(nrow = size, ncol = loop) for(j in 1: 10)
+ print (paste(j,"/200",sep="")) [1] "1/200" [1] "2/200" [1] "3/200" [1] "4/200" [1] "5/200" [1] "6/200" [1] "7/200" [1] "8/200" [1] "9/200" [1] "10/200"
{
+ enres<-enet(x=X,y=Y[,j],lambda=1,normalize=TRUE,intercept=TRUE) + fit<-predict.enet(enres, X, type="coefficients") + store[,j]<-fit$coefficients + } *Error in store[, j] <- fit$coefficients : number of items to replace is not a multiple of replacement length
*
[[alternative HTML version deleted]]
______________________________________________ R-help at 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 and provide commented, minimal, self-contained, reproducible code.
drs. H.A. (Arien) Lam (Ph.D. student) Department of Physical Geography Faculty of Geosciences Utrecht University, The Netherlands