--- Begin Message ---
Ivan, try this
d1 <- matrix(rnorm(10),50,50)
d2 <- matrix(rnorm(10),50,50)
regs <- lapply(1:ncol(d1), function(i) lm(d1[,i] ~ d2[,i])
HTH, Filipe
-----Mensagem original-----
De: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
Em nome de ivan
Enviada em: segunda-feira, 20 de junho de 2011 16:24
Para: r-help@r-project.org
Assunto: [R] for loop and linear models
Hi,
I have two datasets, x and y. Simplified x and y denote:
X
Y
A B C A B C . . . . . . . . . . . . . . . . . .
I want to implement all possible models such as lm(X$A~Y$A),
lm(X$B~Y$B),
lm(X$C~Y$C)... I have tried the following:
fun<- function(x,y){
for(i in 1:length(colnames(x))){
for(j in 1:length(colnames(y))){
if(colnames(x)[i]==colnames(y)[j]){
models=list(lm(ts(x[i])~ts(y[j])))
}
else{}
}
}
return(models)
}
The problem is that this returns only one of the three models, namely
the
last one. What am I doing wrong? Thank you very much in advance.
Regards
[[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
and provide commented, minimal, self-contained, reproducible code.
--- End Message ---
"This message and its attachments may contain confidential and/or privileged
information. If you are not the addressee, please, advise the sender
immediately by replying to the e-mail and delete this message."
"Este mensaje y sus anexos pueden contener información confidencial o
privilegiada. Si ha recibido este e-mail por error por favor bórrelo y envíe un
mensaje al remitente."
"Esta mensagem e seus anexos podem conter informação confidencial ou
privilegiada. Caso não seja o destinatário, solicitamos a imediata notificação
ao remetente e exclusão da mensagem."
______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.