-1

I want to run time series regressions (Fama-French three factor with my new factor). I have following tables.

Table01

    Date     Port_01  Port_02 --------- Port_18
    01/1965     0.85    0.97               1.86
    02/1965     8.96    7.2                0.98
    03/1965     8.98    7.9                8.86 

Table 02

    Date        Market   SMB    HML     WXO
    01/1965      0.85    0.97    0.86    0.87
    02/1965      8.96    7.2     0.98    0.79
    03/1965      8.98     7.9    8.86    0.86

I have to run 18 regressions and store their intercepts in a vector. Something like this

      Port_1=inter(1)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e
      Port_2=inter(2)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e
      Port_18=inter(18)+Beta1(Market)+Beta2(SMB)+Beta3(HML)+Beta3(WXO)+e

I want these 18 intercepts to be stored in a vector. I can do it individually. But If there is a way to do with coding that will help me a lot of time.

Myurathan Kajendran
  • 647
  • 3
  • 8
  • 15
  • If you are using least-squares method, then this can easily be done with linear algebra. – s_baldur Jul 25 '16 at 17:35
  • 1
    http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – shayaa Jul 25 '16 at 17:46
  • Your question is unclear (read the above link), but here's a general approach to the issue: http://edinbr.org/edinbr/2016/05/11/may-Hadley-Update2-PostingTalk.html – alistaire Jul 25 '16 at 17:51
  • Sorry for being unclear. I have edited my question. I'm fairly newbie to R, I do not quite understand those links contents. :( – Myurathan Kajendran Jul 25 '16 at 19:19

1 Answers1

0

Consider vapply(), a variant of lapply() that allows specification of the output here being atomic numeric vector (length of 1). However, first, you need to merge the tables by Date field and then create a list of Port formulas (assuming that's the needed underlying data). Below runs linear regression, lm, but adjust to actual model which might require adjusting intercept extraction:

data <- merge(Table_01, Table_02, by="Date")

ports <- colnames(Table_01)[2:ncol(Table_01)]    
formulas <- paste(ports, "~ Market + SMB + HML + WXO")

intercepts <- vapply(formulas, function(i) { 
                          output <- lm(i, data)
                          coef(output)["(Intercept)"]
                     }, numeric(1))
Parfait
  • 104,375
  • 17
  • 94
  • 125