# [R] loop for regression

arun smartpink111 at yahoo.com
Wed Jul 4 21:06:24 CEST 2012

```HI Bert

Thanks for the reply.  You are right.  In the case if it was a matrix, then lm automatically fits each column.

Stocks<-dat1[,2:4]
> is.matrix(Stocks)
[1] FALSE
> is.data.frame(Stocks)
[1] TRUE

#Not working

models<-lm(Stocks~Market,data=dat1)
Error in model.frame.default(formula = Stocks ~ Market, data = dat1, drop.unused.levels = TRUE) :

#Works

Stocks1<-as.matrix(Stocks)
models1<-lm(Stocks1~Market,data=dat1)
models1

Call:
lm(formula = Stocks1 ~ Market, data = dat1)

Coefficients:
Stock1  Stock2  Stock3
(Intercept)  -3      -2      -1
Market        1       1       1

A.K.

________________________________
From: Bert Gunter <gunter.berton at gene.com>
To: arun <smartpink111 at yahoo.com>
Cc: Akhil dua <akhil.dua.12 at gmail.com>; R help <r-help at r-project.org>
Sent: Wednesday, July 4, 2012 2:48 PM
Subject: Re: [R] loop for regression

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.

-- Bert

On Wed, Jul 4, 2012 at 9:44 AM, arun <smartpink111 at yahoo.com> wrote:

>
>Hi,
>
>You could also use:
>
>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
>
>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
>and provide commented, minimal, self-contained, reproducible code.
>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>and provide commented, minimal, self-contained, reproducible code.
>

--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website: