Quantcast

(no subject)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

(no subject)

Akhil dua
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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: (no subject)

Hasan Diwan-2
On 3 July 2012 22:03, Akhil dua <[hidden email]> 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
--
Sent from my mobile device
Envoyait de mon portable

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: (no subject)

Rui Barradas
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 <[hidden email]> 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
>

______________________________________________
[hidden email] 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.
Loading...