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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Em nome 
de Caio Ramos Casimiro Enviada em: terça-feira, 31 de maio de 2011 12:23
Para: r-help@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@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."
--- Begin Message ---
        Hi Raphael, using your data as is, and if I understood what you
need

> birds
     Square Sp1 Sp2 Sp3 Sp4 Spn Natprop Effort
[1,]      1   1   0   1   1   0     0.5     10
[2,]      2   1   0   1   1   0     0.6     20
[3,]      3   1   1   0   1   0     0.8     23
[4,]      4   1   0   1   0   0     0.8     50
[5,]      5   0   1   0   1   1     0.9     30

        
        I store all results from regressions in a list to then extract
what I want

bird_regressions <- lapply(1:5, function(i) lm(birds[,i+1] ~ birds[,
"Natprop"] + birds[, "Effort"]))   
# Will just loop through Sp1, Sp2... Spn keeping "Natprop" and "Effort"
fixed


        Now retrieving some important stats

r_sq  <- sapply(1:length(bird_regressions),function(i)
summary(bird_regressions[[i]])$r.squared)
betas <- sapply(1:length(bird_regressions),function(i)
summary(bird_regressions[[i]])$coefficients[2,1])
tstat <- sapply(1:length(bird_regressions),function(i)
summary(bird_regressions[[i]])$coefficients[2,3])


        Now with
        
cbind(r_sq, betas, tstat)


        You will have a matrix summarizing what I believe you need from
Sp1, Sp2... Spn. Hope this was helpful.
        

-----Mensagem original-----
De: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
Em nome de Rafael Xavier de Camargo
Enviada em: segunda-feira, 23 de maio de 2011 13:05
Para: r-help@r-project.org
Assunto: [R] Linear regression - several response variables vs few
indvariables

Hi all,

I need to run several simple linear regressions at once, using the
following data. Response variables: Bird species (sp 1, sp2, sp3...spn).
Independent variable: Natprop - proportion of natural area. covarate:
Effort = hours). One single linear regression would be: lmSp1 <- lm(sp1~
natprop + effort). However, I need to run this linear regression for all
bird species that I have individually (n = 163). I would like to do it
at
once and store the coefficients in a single data frame. Is that
possible?

Table that I have:
Square  Sp1  Sp2  Sp3 Sp4  Spn  Natprop Effort

1        1    0    1   1    0     0.5     10
2        1    0    1   1    0     0.6     20
3        1    1    0   1    0     0.8     23
4        1    0    1   0    0     0.8     50
n        0    1    0   1    1     0.9     30


Thanks in advance.


Rafael.



**********************************************
RAFAEL CAMARGO
Postgraduate Student
Biology Department of University of Ottawa
30 Marie Curie, room # 351
Ottawa, ON, CANADA
Tel: +1 (613) 562-5800 ext. 6366
Cel: +1 (613) 869-3772
e-mail: rcama...@uottawa.ca
        rafael.x.cama...@gmail.com

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

Reply via email to