Skip to content
Prev 306450 / 398506 Next

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