There are a lot of problems here. @Sven has addressed some of them. The subset error you are getting is because you are trying to use j
as an index to an array, but j
is not an index, but rather the actual data.
This should work:
betas<-c()
alphas<-c()
for(j in c("d1","d4","d6","d10")){
m <- lm(French[,j] ~ French[,"rmrf"])
betas<-c(betas,m$coefficients[2])
alphas<-c(alphas,m$coefficients[1])
}
Incidentally, doing a t-test on your alphas and betas is probably not the best way to tell if they are statistically significant. A t-test on the array of alphas, for example, would test whether collectively these alphas differ significantly from 0, but it won't tell you whether any individual portfolio's alpha is statistically significant. Another, probably better choice, would be to test the significance of each regression coefficient individually. For example, this code will give you the T-statistics for the alpha and beta coefficients of each regression.
betaT<-c()
alphaT<-c()
for(j in c("d1","d4","d6","d10")){
m <- lm(French[,j] ~ French[,"rmrf"])
betaT<-c(betaT,summary(m)$coefficients[2,3])
alphaT<-c(alphaT,summary(m)$coefficients[1,3])
}
See ?summary.lm
for details and more info.