[R] loop problem for extract coefficients
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Mon Apr 6 01:21:17 CEST 2009
Perhaps your loop should be more than just a print statement. That works fine! You need to place the print statement after the '{', not before it.
fit$coefficients is a 21 x 20 array (the rows are lablelled 0 to 20) and you are trying to put it in the jth *column* of a 20 x 20 matrix. Not surprisingly it does not fit.
Perhaps you need to know more about just what it is this software is doing.
Bill Venables
http://www.cmis.csiro.au/bill.venables/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alex Roy
Sent: Monday, 6 April 2009 12:49 AM
To: r-help at r-project.org
Subject: [R] loop problem for extract coefficients
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
}
----------------------------------------------------
> 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.
More information about the R-help
mailing list