I am trying to do a similar regression for 25 different portfolios and then finding the R^2 of all 25 regressions. Obviously i can do them individually by running
P1<-lm(formula = df[1:24,1] - RiskFree ~ Mkt.RF + SMB + HML, data = df )
summary(P1)$r.squared
25 times to get all the r.square which is really time consuming (can't imagine if it's 100 or greater). I thought of doing a loop and here is where i got stuck. This is what i did
sequence<-seq(1,25)
P<-cbind(sequence)
for(i in 2:26){
P[i-1]<-lm(formula = df[1:24,i] - RiskFree ~ Mkt.RF + SMB + HML, data = df )
return(summary(P[i-1])$r.squared)
which returns error
Error in summary(P[i - 1])$r.squared : $ operator is invalid for atomic vectors In addition: Warning message: In P[i - 1] <- lm(formula = df[1:24, i] - RiskFree ~ Mkt.RF + SMB + : number of items to replace is not a multiple of replacement length`
How do i get my R^2 and then place them in a matrix form?
(edit) this is the sample data that i am working on
df <- "Year SMALL.LoBM ME1.BM2 ME1.BM3 ME1.BM4 Mkt.RF SMB HML RiskFree
1991 -4.61 22.74 16.42 27.89 37.88 2.59 13.60 23.22
1992 8.20 20.59 22.90 25.94 40.05 6.66 15.14 16.04
1993 1.20 12.41 19.27 21.39 37.59 5.46 17.19 23.40
1994 -22.67 -0.56 -3.86 1.34 1.93 -3.38-2.28 0.25
Data <- read.table(text=df, header = TRUE)