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=read.table("C:\\Users\\Krunal\\Desktop\\R tryout.csv",header=T,sep=",") > cnames <- names(tryout) > price <- cnames[grep("Price", cnames)] > media <- cnames[grep("Media", cnames)] > resp <- cnames[1] > regr <- cnames[7:8] > lm.list <- vector("list", 10) > for(i in 1:5) + { + 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) + } + } > summ.list <- lapply(lm.list, summary) > summ.list 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:ruipbarra...@sapo.pt] Sent: 27 September 2012 16:22 To: David Winsemius Cc: Krunal Nanavati; r-help@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: > On Sep 26, 2012, at 10:31 PM, Krunal Nanavati wrote: > >> Dear Rui, >> >> Thanks for your time. >> >> I have a question though, when I run the 5 regression, whose outputs >> are stored in "lm.list[i]", I only get the coefficients for the >> Intercept, Price, Trend & Seasonality as below >> >> >>> lm.list[1] >> [[1]] >> >> Call: >> >> lm(formula = as.formula(fmla), data = tryout) >> >> Coefficients: >> >> (Intercept) Price4 Trend Seasonality >> >> 9923123 -2606826 64616 551392 > summ.list <- lapply(lm.list, summary) > coef.list <- lapply(summ.list, coef) > coef.list > >> I am also looking out for t stats and p value and R squared. > For the r.squared > > rsq.vec <- sapply(summ.list, "$", "r.squared") adj.rsq <- > sapply(summ.list, "$", "adj.r.squared") > >> Do you know, >> how can I get all these statistics. Also, why is " as.formula " used >> in the lm function. It should work without that as well, right? > No. >> >> Can you please tell me, why the code that I had written, does not >> work with R. I thought it should work perfectly. > In R there is a difference between expression objects and character objects. > > >> Thanks & Regards, >> >> >> >> Krunal Nanavati >> >> 9769-919198 >> >> >> >> *From:* Rui Barradas [mailto:ruipbarra...@sapo.pt] >> *Sent:* 26 September 2012 17:13 >> *To:* Krunal Nanavati >> *Cc:* r-help@r-project.org >> *Subject:* Re: [R] Running different Regressions using for loops >> >> >> >> Hello, >> >> Try the following. >> >> >> #cnames <- names(tryout) # in your code, use this one cnames <- >> c("Volume", "Price1", "Price2", "Price3", "Price4", "Price5", >> "Trend", "Seasonaliy") >> >> price <- cnames[grep("Price", cnames)] resp <- cnames[1] regr <- >> cnames[7:8] >> >> #lm.list <- vector("list", 5) >> for(i in 1:5){ >> regress <- paste(price[i], paste(regr, collapse = "+"), sep = "+") >> fmla <- paste(resp, regress, sep = "~") >> print(fmla) >> #lm.list[[i]] <- lm(as.formula(fmla), data = tryout) } >> >> Hope this helps, >> >> Rui Barradas >> >> Em 26-09-2012 08:08, Krunal Nanavati escreveu: >> >> Hi, >> >> >> I am trying to run many different regressions using a FOR Loop. >> >> >> The input data that is read into R has the following variables >> >> . Volume >> . Price2 >> . Price3 >> . Price4 >> . Price5 >> . Trend >> . Seasonality >> >> I want to run 5 regressions, with the Volume as an dependent variable >> and >> >> Price, Trend & Seasonality as independent variables. I have read the >> above >> >> mentioned variables in a variable called "tryout" >> >> >> >> I am entering the following syntax in R >> >> >> for(i in 1:5) >> >> + { >> + result[i]=lm(Volume~Price[i]+Trend+Seasonaliy,data=tryout) >> + summary(result[i]) >> + } >> >> After running these lines.I am getting the following error message >> Error in eval(expr, envir, enclos) : object 'Price' not found >> >> Can someone help me out with this error message. Appreciate for your >> time >> >> and consideration. >> >> >> >> [[alternative HTML version deleted]] >> >> > David Winsemius, MD > Alameda, CA, USA > ______________________________________________ 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.