Hi I have a simple question. I want to run a "n times" a simple linear regession and save beta in a matrix but I´m not able. Imagine: Data.txt is a 10*5 file and want to run 4 different stimations always regressing first column on the rest. So I try this: First I run Data on memory This is my function mrp <- function(){ mr<-matrix(0,4,1) for(i in 1:4) r(i)=lm(dat(,i+1)~dat(,1) mr[i] <- coefficients(r(i))) } I execute mrp usin source file choose option but nothing happens.... Where I´m wrong? [[alternative HTML version deleted]]
Try this: mrp <- function(){ r <- list() mr<-matrix(0,4,1) for(i in 1:4){ r[[i]] <- lm(dat(,i+1)~dat(,1)) mr[i,1] <- coefficients(r[[i]]) } mr } Atenciosamente, Leandro Lins Marino Centro de Avalia??o Funda??o CESGRANRIO Rua Santa Alexandrina, 1011 - 2? andar Rio de Janeiro, RJ - CEP: 20261-903 R (21) 2103-9600 R.:236 (21) 8777-7907 ( leandro at cesgranrio.org.br "Aquele que suporta o peso da sociedade ? precisamente aquele que obt?m as menores vantagens". (SMITH, Adam) ? Antes de imprimir pense em sua responsabilidade e compromisso com o MEIO AMBIENTE Esta mensagem, incluindo seus anexos, pode conter informacoes privilegiadas e/ou de carater confidencial, nao podendo ser retransmitida sem autorizacao do remetente. Se voce nao e o destinatario ou pessoa autorizada a recebe-la, informamos que o seu uso, divulgacao, copia ou arquivamento sao proibidos. Portanto, se voc? recebeu esta mensagem por engano, por favor, nos informe respondendo imediatamente a este e-mail e em seguida apague-a. -----Mensagem original----- De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Em nome de Oscar Bayona Enviada em: segunda-feira, 15 de junho de 2009 18:42 Para: r-help at r-project.org Assunto: [R] Hi Hi I have a simple question. I want to run a "n times" a simple linear regession and save beta in a matrix but I4m not able. Imagine: Data.txt is a 10*5 file and want to run 4 different stimations always regressing first column on the rest. So I try this: First I run Data on memory This is my function mrp <- function(){ mr<-matrix(0,4,1) for(i in 1:4) r(i)=lm(dat(,i+1)~dat(,1) mr[i] <- coefficients(r(i))) } I execute mrp usin source file choose option but nothing happens.... Where I4m wrong? [[alternative HTML version deleted]]
On 6/15/2009 5:42 PM, Oscar Bayona wrote:> Hi I have a simple question. I want to run a "n times" a simple linear > regession and save beta in a matrix but I?m not able. > > Imagine: > > Data.txt is a 10*5 file and want to run 4 different stimations always > regressing first column on the rest. > > So I try this: > > First I run Data on memory > > This is my function > > mrp <- function(){ > mr<-matrix(0,4,1) > for(i in 1:4) > r(i)=lm(dat(,i+1)~dat(,1) > mr[i] <- coefficients(r(i))) > } > > I execute mrp usin source file choose option but nothing happens.... > > Where I?m wrong?It's hard to tell exactly what you want, but does this help? mr <- lm(as.matrix(cbind(dat[,2:ncol(dat)])) ~ dat[,1]) summary(mr)> [[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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
You can use (*)apply functions: sapply(paste("V1 ~", names(dat)[2:5]), function(f)coef(lm(as.formula(f), data = dat))) Where V1 is your first column and dat is your data.frame object. On Mon, Jun 15, 2009 at 6:42 PM, Oscar Bayona <osbacan@gmail.com> wrote:> Hi I have a simple question. I want to run a "n times" a simple linear > regession and save beta in a matrix but I´m not able. > > Imagine: > > Data.txt is a 10*5 file and want to run 4 different stimations always > regressing first column on the rest. > > So I try this: > > First I run Data on memory > > This is my function > > mrp <- function(){ > mr<-matrix(0,4,1) > for(i in 1:4) > r(i)=lm(dat(,i+1)~dat(,1) > mr[i] <- coefficients(r(i))) > } > > I execute mrp usin source file choose option but nothing happens.... > > Where I´m wrong? > > [[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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]