Skip to content
Prev 306435 / 398506 Next

Running different Regressions using for loops

Hi,

Thanks for all your help. I am stuck again, but with a new problem, on
similar lines.

I have taken the problem to the next step now...i have now added 2 "for"
loops... 1 for the Price variable...and another for the Media variable

I have taken 5 price variables...and 2 media variables with the "trend and
seasonality"(appearing in all of them)....so in all there will be 10
regression to run now

Price 1, Media 1

Price 1, Media 2

Price 2, Media 1'

Price 2, Media 2

...and so on

I have built up a code for it...
tryout.csv",header=T,sep=",")
+ {
+ regress <- paste(price[i], paste(regr, collapse = "+"), sep = "+")
+ for(j in 1:2)
+ {
+ regress1 <- paste(media[j],regress,sep="+")
+ fmla <- paste(resp, regress1, sep = "~")
+ lm.list[[i]] <- lm(as.formula(fmla), data = tryout)
+ }
+ }
But it is only running...5 regressions...only Media 1 along with the 5
Price variables & Trend & Seasonality is regressed on Volume...giving only
5 outputs

I feel there is something wrong with the    " lm.list[[i]] <-
lm(as.formula(fmla), data = tryout)"   statement. I am not sure about its
placement...whether it should be in loop 2 or in loop 1

Can you please help me out??










Thanks & Regards,

Krunal Nanavati
9769-919198

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

Hello,

Just to add that you can also

lapply(lm.list, coef)

with a different output.

Rui Barradas
Em 27-09-2012 09:24, David Winsemius escreveu:
objects.