Skip to content
Prev 306507 / 398506 Next

Running different Regressions using for loops

Hi Rui,

Excellent!!  This is what I was looking for. Thanks for the help.

So, now I have stored the result of the 10 regressions in      "summ.list
<- lapply(lm.list2, summary)"

And now once I enter        " sum.list "....it gives me the output for all
the 10 regressions...

I wanted to access a beta coefficient of one of the regressions....say
"Price2+Media1+Trend+Seasonality"...the result of which is stored in     "
sum.list[2] "

I entered the below statement for accessing the Beta coefficient for
Price2...
NULL

But this is giving me " NULL " as the output...

What I am looking for, is to access a beta value of a particular variable
from a particular regression output and use it for further analysis.

Can you please help me out with this. Greatly appreciate, you guys
efforts.




Thanks & Regards,

Krunal Nanavati
9769-919198

-----Original Message-----
From: Rui Barradas [mailto:ruipbarradas at sapo.pt]
Sent: 27 September 2012 21:55
To: Krunal Nanavati
Cc: David Winsemius; r-help at r-project.org
Subject: Re: [R] Running different Regressions using for loops

Hello,

Inline.
Em 27-09-2012 13:52, Krunal Nanavati escreveu:
No, I don't think so. If it's giving you only 5 outputs the error is
probably in the fmla construction. Put print statements to see the results
of those paste() instructions.

Supposing your data.frame is now called tryout2,


price <- paste("Price", 1:5, sep = "")
media <- paste("Media", 1:2, sep = "")
pricemedia <- apply(expand.grid(price, media, stringsAsFactors = FALSE),
1, paste, collapse="+")

response <- "Volume"
trendseason <- "Trend+Seasonality"  # do this only once

lm.list2 <- list()
for(i in seq_along(pricemedia)){
     regr <- paste(pricemedia[i], trendseason, sep = "+")
     fmla <- paste(response, regr, sep = "~")
     lm.list2[[i]] <- lm(as.formula(fmla), data = tryout2) }

The trick is to use ?expand.grid

Hope this helps,

Rui Barradas
"+")