An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120704/1c30fb3d/attachment.pl>
(no subject)
3 messages · Akhil dua, Hasan Diwan, Rui Barradas
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120703/58c10b08/attachment.pl>
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.12 at 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