---------- Forwarded message ---------- From: Akhil dua <akhil.dua.12@gmail.com> Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help@r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a "for loop" so that I wont have to write codes again and again to run the regression... my data is in the format given below Date Stock1 Stock2 Stock3 Market 01/01/2000 1 2 3 4 01/02/2000 5 6 7 8 01/03/2000 1 2 3 4 01/04/2000 5 6 7 8 So can any one help me how to write this loop [[alternative HTML version deleted]]
Homework? (We don't do homework here). -- Bert On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua <akhil.dua.12@gmail.com> wrote:> ---------- Forwarded message ---------- > From: Akhil dua <akhil.dua.12@gmail.com> > Date: Wed, Jul 4, 2012 at 10:33 AM > Subject: > To: r-help@r-project.org > > > Hi everyone I > have data on stock prices and market indices > > and I need to run a seperate regression of every stock on market > so I want to write a "for loop" so that I wont have to write codes again > and again to run the regression... > my data is in the format given below > > > > Date Stock1 Stock2 Stock3 Market > 01/01/2000 1 2 3 4 > 01/02/2000 5 6 7 8 > 01/03/2000 1 2 3 4 > 01/04/2000 5 6 7 8 > > > So can any one help me how to write this loop > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
?lm and note in particular the section beginning "If response is a matrix..." -- Bert On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua <akhil.dua.12@gmail.com> wrote:> ---------- Forwarded message ---------- > From: Akhil dua <akhil.dua.12@gmail.com> > Date: Wed, Jul 4, 2012 at 10:33 AM > Subject: > To: r-help@r-project.org > > > Hi everyone I > have data on stock prices and market indices > > and I need to run a seperate regression of every stock on market > so I want to write a "for loop" so that I wont have to write codes again > and again to run the regression... > my data is in the format given below > > > > Date Stock1 Stock2 Stock3 Market > 01/01/2000 1 2 3 4 > 01/02/2000 5 6 7 8 > 01/03/2000 1 2 3 4 > 01/04/2000 5 6 7 8 > > > So can any one help me how to write this loop > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]]
Hi, A few comments. First a for loop is probably not optimally efficient. Consider instead (using a bulit in example dataset): lm(cbind(mpg, hp) ~ cyl + vs, data = mtcars) which gives: Call: lm(formula = cbind(mpg, hp) ~ cyl + vs, data = mtcars) Coefficients: mpg hp (Intercept) 39.6250 -15.6279 cyl -3.0907 27.5843 vs -0.9391 -19.1148 i.e., same predictors used on both outcomes. Note that this is substantially faster than running separately. See ?lm for details. If you need to run separate models (e.g., predictors are changing), and you have many models and a lot of data (which would not be surprising when working with stock data), consider using the RcppEigen package. You can get it by: install.packages("RcppEigen") require(RcppEigen) # load package it has a function called fastLm which is orders of magnitude faster than lm() and works almost identically. lapply(mtcars[, c("mpg", "hp")], function(x) fastLm(X = cbind(Int = 1, mtcars[, c("cyl", "vs")]), y = x)) you just give it the design matrix (X) and response vector (y) see ?fastLm Cheers, Josh On Tue, Jul 3, 2012 at 10:08 PM, Akhil dua <akhil.dua.12 at gmail.com> wrote:> ---------- Forwarded message ---------- > From: Akhil dua <akhil.dua.12 at gmail.com> > Date: Wed, Jul 4, 2012 at 10:33 AM > Subject: > To: r-help at r-project.org > > > Hi everyone I > have data on stock prices and market indices > > and I need to run a seperate regression of every stock on market > so I want to write a "for loop" so that I wont have to write codes again > and again to run the regression... > my data is in the format given below > > > > Date Stock1 Stock2 Stock3 Market > 01/01/2000 1 2 3 4 > 01/02/2000 5 6 7 8 > 01/03/2000 1 2 3 4 > 01/04/2000 5 6 7 8 > > > So can any one help me how to write this loop > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
Hi, You could also use: dat1 <- read.table(text=" Date????????????? Stock1? Stock2? Stock3??? Market 01/01/2000??????? 1????????? 2????????? 3??????????? 4 01/02/2000??????? 5????????? 6????????? 7??????????? 8 01/03/2000??????? 1????????? 2????????? 3??????????? 4 01/04/2000??????? 5????????? 6????????? 7??????????? 8 ", header=TRUE, stringsAsFactors=FALSE) Stocks<-dat1[,2:4] apply(Stocks,2,function(x) lm(x~Market,data=dat1)) $Stock1 Call: lm(formula = x ~ Market, data = dat1) Coefficients: (Intercept)?????? Market? ???????? -3??????????? 1? $Stock2 Call: lm(formula = x ~ Market, data = dat1) Coefficients: (Intercept)?????? Market? ???????? -2??????????? 1? $Stock3 Call: lm(formula = x ~ Market, data = dat1) Coefficients: (Intercept)?????? Market? ???????? -1??????????? 1? A.K. ----- Original Message ----- From: Akhil dua <akhil.dua.12 at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, July 4, 2012 1:08 AM Subject: [R] loop for regression ---------- Forwarded message ---------- From: Akhil dua <akhil.dua.12 at gmail.com> Date: Wed, Jul 4, 2012 at 10:33 AM Subject: To: r-help at r-project.org Hi everyone I have data on stock prices and market indices and I need to run a seperate regression of every stock on market so I want to write a? "for loop"? so that I wont have to write codes again and again to run the regression... my data is in the format given below Date? ? ? ? ? ? ? Stock1? Stock2? Stock3? ? Market 01/01/2000? ? ? ? 1? ? ? ? ? 2? ? ? ? ? 3? ? ? ? ? ? 4 01/02/2000? ? ? ? 5? ? ? ? ? 6? ? ? ? ? 7? ? ? ? ? ? 8 01/03/2000? ? ? ? 1? ? ? ? ? 2? ? ? ? ? 3? ? ? ? ? ? 4 01/04/2000? ? ? ? 5? ? ? ? ? 6? ? ? ? ? 7? ? ? ? ? ? 8 So can any one help me how to write this loop ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Apologies to Bert, I see now that "market" is a variable in the dataset, not a generic name for market indicators. lm() with a matrix as the outcome seems the best approach here. Josh On Wed, Jul 4, 2012 at 12:12 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:> On Wed, Jul 4, 2012 at 11:48 AM, Bert Gunter <gunter.berton at gene.com> wrote: >> Please carefully read ?lm. As I previously told the OP, no looping/apply is >> necessary. The left hand side of the lm formula can be a matrix for which >> separate fits will be done on each column automatically. > > Which is a great option if the design matrix is constant, but suppose > you want to predict each stock from every other stock in a dataset? > This is a one liner with lapply: > > lapply(colnames(mtcars), function(n) lm(substitute(y ~ ., list(y > as.name(n))), data = mtcars)) > > granted, not the prettiest or most efficient thing on the planet (and > falls apart for some reason with fastLm(), which I am still > investigating work arounds to). The matrix outcome to lm() approach: > > lm(as.matrix(mtcars) ~ ., data = mtcars) > > yeilds perfect explanation by the variable of itself, as expected, > which is not really useful. The OP did not give many details other > than "write a for loop". It is not clear what should be varying. If > it is *just* the outcome, you are absolutely right, giving lm a matrix > seems the most sensible route. > > Cheers, > > Josh > >> >> -- Bert >> >> On Wed, Jul 4, 2012 at 9:44 AM, arun <smartpink111 at yahoo.com> wrote: >> >>> >>> >>> Hi, >>> >>> You could also use: >>> dat1 <- read.table(text=" >>> >>> Date Stock1 Stock2 Stock3 Market >>> 01/01/2000 1 2 3 4 >>> 01/02/2000 5 6 7 8 >>> 01/03/2000 1 2 3 4 >>> 01/04/2000 5 6 7 8 >>> ", header=TRUE, stringsAsFactors=FALSE) >>> >>> Stocks<-dat1[,2:4] >>> apply(Stocks,2,function(x) lm(x~Market,data=dat1)) >>> $Stock1 >>> >>> Call: >>> lm(formula = x ~ Market, data = dat1) >>> >>> Coefficients: >>> (Intercept) Market >>> -3 1 >>> >>> >>> $Stock2 >>> >>> Call: >>> lm(formula = x ~ Market, data = dat1) >>> >>> Coefficients: >>> (Intercept) Market >>> -2 1 >>> >>> >>> $Stock3 >>> >>> Call: >>> lm(formula = x ~ Market, data = dat1) >>> >>> Coefficients: >>> (Intercept) Market >>> -1 1 >>> >>> A.K. >>> >>> >>> >>> >>> ----- Original Message ----- >>> From: Akhil dua <akhil.dua.12 at gmail.com> >>> To: r-help at r-project.org >>> Cc: >>> Sent: Wednesday, July 4, 2012 1:08 AM >>> Subject: [R] loop for regression >>> >>> ---------- Forwarded message ---------- >>> From: Akhil dua <akhil.dua.12 at gmail.com> >>> Date: Wed, Jul 4, 2012 at 10:33 AM >>> Subject: >>> To: r-help at r-project.org >>> >>> >>> Hi everyone I >>> have data on stock prices and market indices >>> >>> and I need to run a seperate regression of every stock on market >>> so I want to write a "for loop" so that I wont have to write codes again >>> and again to run the regression... >>> my data is in the format given below >>> >>> >>> >>> Date Stock1 Stock2 Stock3 Market >>> 01/01/2000 1 2 3 4 >>> 01/02/2000 5 6 7 8 >>> 01/03/2000 1 2 3 4 >>> 01/04/2000 5 6 7 8 >>> >>> >>> So can any one help me how to write this loop >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-help at 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. >>> >>> >>> ______________________________________________ >>> R-help at 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. >>> >> >> >> >> -- >> >> Bert Gunter >> Genentech Nonclinical Biostatistics >> >> Internal Contact Info: >> Phone: 467-7374 >> Website: >> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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. > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > Programmer Analyst II, Statistical Consulting Group > University of California, Los Angeles > https://joshuawiley.com/-- Joshua Wiley Ph.D. Student, Health Psychology Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/