Hello,

Or maybe to avoid the typo (Market is column 5), use variables names, in something like


myData <- 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)
myData$Date <- as.Date(myData$Date, fortmat="%m/%d/%Y")
myData

# Avoid typos
stocks <- grep("Stock", names(myData))
models <- lapply(myData[, stocks], function(x) lm(x ~ myData$Market))

# Do whatever you want with results
lapply(models, summary)


Hope this helps,

Rui Barradas

Em 04-07-2012 06:29, Hasan Diwan escreveu:
On 3 July 2012 22:03, Akhil dua <akhil.dua...@gmail.com> wrote:

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...


1. Do give a subject line -- a blank one is commonly used by a virus.
2. In R/S+/most functional languages, you do not want to write a "for
loop". Use apply (and friends) instead.

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


For example, if you wanted to know the stocks share of the total market as
a fraction, you'd use something like:
sapply(myData[,c(2:4)], function(x) {
return(as.numeric(x)/as.numeric(myData[,4])) })

Hope that helps.. -- H


______________________________________________
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.

Reply via email to