r - Creating a matrix of OLS results -


i have data set of excess returns 576 years 25 asset portfolios(it goes on till 201012 , er25)

 date    er1     er2     er3     er4     er5   market-rf 196301  12.77   11.19   9.15    10.71   10.87   4.93 196302  -3.48   -3.72   -0.94   -1.06   2.51   -2.42 196303  4.75    -1.7    -0.34    0.99   2.36    3.06 196304  4.55    1.25     1.8     3.29   2.52    4.49 196305  3.15    1.44     2.51    3.89   7.63    1.77 

i need run 25 regressions capm model , need arrange alphas(intercepts), betas(the co-efficient) , t-statistic of intercept in 25x3 matrix form.

here regressions.

capm1 <- lm(er1~market.rf, data=ff25) capm2 <- lm(er2~market.rf, data=ff25) capm3 <- lm(er3~market.rf, data=ff25) etc until capm25. 

i can results of coeftest this.

coeftest(capm1) 

t test of coefficients:

#             estimate std. error t value pr(>|t|)     #(intercept) -0.395188   0.204474 -1.9327  0.05376 .   #market.rf    1.434851   0.045032 31.8629  < 2e-16 *** #--- #signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

also, can extract 3 variables of interest using

summary(capm1)$coef[1,1] summary(capm1)$coef[2,1] summary(capm1)$coef[1,3] 

could please me in arranging these variables (i end getting 25 intercepts, 25 coefficients , 25 t-statistic of intercept) in matrix or tabular form. there loop code can written run ols regression had manually run regression 25 times each asset.

a straight forward looping example this:

# index matrix extract 3 values of interest indx <- matrix(c(1,1,                  2,1,                  1,3),nrow=3,byrow=true)  # initialize output matrix null out <- null  # iterate on 25 variables for(i in seq(25))      out  <- rbind(out,                  coeftest(lm(formula(paste0('er',i,'~market.rf')),                               data=ff25))[indx]) 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -