[R-SIG-Finance] Antwort: loop for multiple regressions

Matthias.Koberstein at hsbctrinkaus.de Matthias.Koberstein at hsbctrinkaus.de
Tue Jul 22 17:24:32 CEST 2008


Hi,
Basically the problem is that the regression returns a LIST not a single
value.
You can access the single values of the list with $valuename.
Please look into help(lm) for the values contained in the list.
If you select for example
Example[i]<-lm(y~x)$coefficient[1]

it should save the intercept to the matrix Example at place i.

Cheers

Matthias S. Koberstein
__________________________________
HSBC Trinkaus
Structured Solutions Group
Königsalle 21/23, 40212 Düsseldorf

Phone: +49 211 910 4412
e-mail: matthias.koberstein at hsbctrinkaus.de



**** Ressourcen schonen, weniger drucken - Think before you print! ****

---------------------------------------------------------------------
Diese E-Mail sowie eventuelle Anhänge enthalten vertrauliche und / oder
rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat
sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte
sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren
oder Speichern sowie die unbefugte Weitergabe dieser E-Mail sind nicht
gestattet.

This e-mail and any attachments may contain confidential and / or
privileged information. If you are not the intended recipient or have
received this e-mail in error, please notify the sender immediately and
destroy this e-mail . Any unauthorized copying, storing, disclosure or
distribution of the contents of this e-mail is strictly forbidden.

---------------------------------------------------------------------
HSBC Trinkaus & Burkhardt AG
Sitz: Düsseldorf, Königsallee 21/23, 40212 Düsseldorf, Handelsregister:
Amtsgericht Düsseldorf HRB 54447
Mitglieder des Vorstands: Andreas Schmitz (Sprecher), Paul Hagen, Dr. Olaf
Huth, Carola Gräfin v. Schmettow
Vorsitzender des Aufsichtsrats: Dr. Sieghardt Rometsch


                                                                           
             "Denise Xifara"                                               
             <denisexifaras at go                                             
             oglemail.com>                                              An 
             Gesendet von:               r-sig-finance at stat.math.ethz.ch   
             r-sig-finance-bou                                       Kopie 
             nces at stat.math.et                                             
             hz.ch                                                   Thema 
                                         [R-SIG-Finance] loop for multiple 
             Fax-Deckblatt:              regressions                       
             HSBCTuB                                                       
             22.07.2008 17:05                                              
                                                                           
                                                                           
                                                                           
                                                                           




   Dear all,
I have the following data in excel:

    day1   y 1 2 3 2 3 x1 0.2 0.3 0.4 0.3 0.2 x2 7 3.4 2 8 6  day2   y 2 4
3
2 2 x1 0.4 0.5 0.3 0.3 0.2 x2 7 8 9.1 6 5

I have the following problems:
first of all, when I ask R to read the file (with the package xlsReadWrite
and the command read.xls) it has a problem with the fact that the left most
corner is labelled the same way, so in order for it to work I need to do:



 day1



 y1
 1
 2
 3
 2
 3
 x11
 0.2
 0.3
 0.4
 0.3
 0.2
 x21
 7
 3.4
 2
 8
 6


 day2



 y2
 2
 4
 3
 2
 2
 x12
 0.4
 0.5
 0.3
 0.3
 0.2
 x22
 7
 8
 9.1
 6
 5

Next I need to do the regression y~ x1+x2, for the different days.
I have converted the above data in the form of a matrix and labelled it
mat.
The following loop works:

results<-c(0,0,0,0,0)
for (i in c(1,5)){
results[i]<-lm(mat[i,]~mat[i+1,]+mat[i+2,])}
results

And the results are:

Warning messages:
1: In results[i] <- lm(mat[i, ] ~ mat[i + 1, ] + mat[i + 2, ]) :
  number of items to replace is not a multiple of replacement length
2: In results[i] <- lm(mat[i, ] ~ mat[i + 1, ] + mat[i + 2, ]) :
  number of items to replace is not a multiple of replacement length
> results
[[1]]
 (Intercept) mat[i + 1, ] mat[i + 2, ]
   2.4918415    1.5151515   -0.1356220
[[2]]
[1] 0
[[3]]
[1] 0
[[4]]
[1] 0
[[5]]
 (Intercept) mat[i + 1, ] mat[i + 2, ]
  -0.3741935    3.4193548    0.2580645

The method does work but it is not ideal.  The problem is that the real
data
that I will be using will involve regressions of 20 or so variables over
3000 days. I also want to avoid finding the exact values that "i" should
take inside the loop.
Is there a better way of carrying out these regressions so that I do not
need to relabel everything, the results are faster and it is clear and easy
to isolate the results of the linear models for each day?

Thank you very much everyone in advance,
Regards,
Denise

             [[alternative HTML version deleted]]

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only.
-- If you want to post, subscribe first.



More information about the R-SIG-Finance mailing list