Skip to content

Running different Regressions using for loops

16 messages · David Winsemius, Rui Barradas, Krunal Nanavati +1 more

#
On Sep 26, 2012, at 10:31 PM, Krunal Nanavati wrote:

            
summ.list <- lapply(lm.list, summary)
coef.list <- lapply(summ.list, coef)
coef.list
For the r.squared

rsq.vec <- sapply(summ.list, "$", "r.squared")
adj.rsq <- sapply(summ.list, "$", "adj.r.squared")
No.
In R there is a difference between expression objects and character objects.
David Winsemius, MD
Alameda, CA, USA
#
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:
#
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.
#
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
#
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
"+")
#
Hello, Krunal,

try
Note the double square brackets (as summ.list is a list)!

Hth,

Gerrit
On Fri, 28 Sep 2012, Krunal Nanavati wrote:

            
<<<snip>>>
#
Hello,

To access list elements you need `[[`, like this:

summ.list[[2]]$coefficients

Or Use the extractor function,

coef(summ.list[[2]])

Rui Barradas
Em 28-09-2012 07:23, Krunal Nanavati escreveu:
#
Ok...this solves a part of my problem

When I type   " lm.list2[2] " ...I get the following output

[[1]]

Call:
lm(formula = as.formula(fmla), data = tryout2)

Coefficients:
(Intercept)       Price2       Media1      Distri1        Trend
Seasonality
   13491232     -5759030        -1520        34370        48628
445351




When I enter   " lm.list2[[2]]$coefficient[2] " it gives me the below
output

Price2
-5759030

And when I enter   " lm.list2[[2]]$coefficient[[2]] " ...I get the
number...which is   -5759030


I am looking out for a way to get just the  " Price2 "....is there a
statement for that??



Thanks & Regards,

Krunal Nanavati
9769-919198


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

Hello,

To access list elements you need `[[`, like this:

summ.list[[2]]$coefficients

Or Use the extractor function,

coef(summ.list[[2]])

Rui Barradas
Em 28-09-2012 07:23, Krunal Nanavati escreveu:
"summ.list
all
analysis.
"for"
#
Hello,

Try

names(lm.list2[[2]]$coefficient[2] )

Rui Barradas
Em 28-09-2012 11:29, Krunal Nanavati escreveu:
#
Hi,

Yes the thing that you provided...works fine....but probably I should have
asked for some other thing.

Here is what I am trying to do....

I am trying to get the mean of Price variable....so I am entering the
below function:

         mean(names(lm.list2[[2]]$coefficient[2] ))

but this gives me an error....

	[1] NA
	Warning message:
	In mean.default(names(lm.list2[[2]]$coefficient[2])) :
  	argument is not numeric or logical: returning NA

I thought by getting the text from the list variable...will help me
generate the mean for that text...which is a variable in the data...say
Price 1, Media 2....and so on

Is this a proper approach...if it is...then something more needs to be
done with the function that you provided.

If not, is there a better way...to generate the mean of a particular
variable inside the " for loop " used earlier...given below:
Thanks & Regards,

Krunal Nanavati
9769-919198


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

Hello,

Try

names(lm.list2[[2]]$coefficient[2] )

Rui Barradas
Em 28-09-2012 11:29, Krunal Nanavati escreveu:
which is stored in"
#
Ok, if I'm understanding it well, you want the mean value of Price1,   , 
Price5? I don't know if it makes any sense, the coefficients already are 
mean values, but see if this is it.

price.coef <- sapply(lm.list, function(x) coef(x)[2])
mean(price.coef)

Rui Barradas
Em 28-09-2012 12:07, Krunal Nanavati escreveu:
#
Ok...I am sorry for the misunderstanding....

what I am trying to do is....
When I run...this set of statements....the 1st regression to be run, will
have Price 1, Media 1...as X variables....and in the second loop it will
have Price 1 & Media 2 ....

So, what I was thinking is...if I can generate inside the for loop....the
mean for Price 1 and Media 1 during the 1st loop....and then mean for
Price 1 and Media 2 during the second loop...and so on...for all the 10
regressions


Is the method that I was trying appropriate...or is there a better method
there...I am sorry for the earlier explanation, I hope this one makes it
more understandable


Thanks for your time...and all the quick replies




Thanks & Regards,

Krunal Nanavati
9769-919198


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

Ok, if I'm understanding it well, you want the mean value of Price1,   ,
Price5? I don't know if it makes any sense, the coefficients already are
mean values, but see if this is it.

price.coef <- sapply(lm.list, function(x) coef(x)[2])
mean(price.coef)

Rui Barradas
Em 28-09-2012 12:07, Krunal Nanavati escreveu:
right?
#
On Sep 28, 2012, at 4:35 AM, Krunal Nanavati wrote:

            
Perhaps (and that is a really large 'perhaps'):
lm.means <- list()
lm.means[[i]]  <- mean(lm.list2[[i]]$coefficients[c("Price1", "Media1")]
            }
One generally want ones methods to be determinate while allowing the results to be approximate.

Had you followed the posting guide a offered a reproducible example it would have been much more "understandable".
David Winsemius, MD
Alameda, CA, USA