[R] ENC: Using lm() combined with sapply

Filipe Leme Botelho filipe.botelho at vpar.com.br
Tue May 31 19:30:24 CEST 2011


Hi Caio,

I just replied to a fellow who needed to run a series of regressions changing the dependent variables for the same period. I believe your solution might be similar to that one, just making the window dynamic instead. I used a lapply to store regressions and sapplys to extract stats from the lm()s. Please check the attachment and hope it´s useful.

Filipe Botelho

-----Mensagem original-----
De: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Em nome de Caio Ramos Casimiro Enviada em: terça-feira, 31 de maio de 2011 12:23
Para: r-help at r-project.org
Assunto: [R] Using lm() combined with sapply

Dear list,

I spent a lot of time looking for the solution for this problem, but with no success. It's the first time I came to the list to ask for help, so I'm sorry if I break some protocol.

Well, I'm trying to make a serie of regressions in different periods, actually, in growing periods. In other words, I start by make a regression in a period from 1 to t, then I make another regression from 1 to t+1, and so on until t+1 < N.

A loop version for this is:
a <- rep(1,100)+rnorm(100)
b <- rep(2,100)+rnorm(100)
c <- rep(1.5,100)+rnorm(100)
n.start <- 50
coefs <- array(NA,c(51,4))
for (i in (n.start:100)) coefs[i-n.start+1,] <- coef(lm(a ~ b + c,
subset=1:i))

I want to avoid loops, so I was trying to make something like:

regride <- function(formula,dat,sub,w) {
    window <- c(1:sub)
    w[1:sub-1] <- w[1:sub-1]*beta^sub
    coefs(lm(formula=formula,data=dat,subset=window,weights=w))
}

coefs <- sapply(n_start:dim(ind)[1],regride,formula,dat,x)

But it's not working.

Best regards,
Caio Casimiro

--
Caio Ramos Casimiro
Software Developer
+55 11 9773 2809

	[[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.

"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."
-------------- next part --------------
An embedded message was scrubbed...
From: "Filipe Leme Botelho" <filipe.botelho at vpar.com.br>
Subject: RES: [R] Linear regression - several response variables vs few indvariables
Date: Tue, 31 May 2011 13:03:06 -0300
Size: 4840
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110531/510f82f3/attachment.mht>


More information about the R-help mailing list