I have 5 independent variables (Columns B-F in attached data) and some dependent variables (columns G-M in the attached data) and I need to do multiple regressions for each of the dependent variable against all independent ones. The regressions have to have a window of 4 years of data and they have to move one month ahead for each new estimation. I need to extract the coefficients and make vasicek adjustment for each one (except the intercept). That adjustment is just:
The data looks like
And the whole data is:
Where independent variables are placed in columns B-F and dependent variables are placed in columns G-M. I have being struggling with this problem and I have built two parts of code. First I reached to extract coefficients for regressions of each dependent variable and adjusted them according to vasicek adjustment but without taking the mobile windows I need:
depvar <- c("LYB_UN_Equity" ,"AXP_UN_Equity", "VZ_UN_Equity", "AVGO_UW_Equity", "BA_UN_Equity", "CAT_UN_Equity", "JPM_UN_Equity")
regresults <- lapply(depvar, function(dv) {
tmplm <- lm(get(dv) ~ Mkt + SMB + HML + RMW + CMA, data=newdata
# ,subset=(Newdata$Fecha > "1996-01-01" & Newdata$Fecha < "1999-12-31"), na.action = na.exclude )
k=length(tmplm$cofficients)-1
SSE=sum(tmplm$residuals**2)
n=length(tmplm$residuals)
SE=sqrt(SSE/(n-(1-k))
coef(tmplm)*(summary(tmplm)$coef[,2]/SE+summary(tmplm)$coef[,2]) +coef(tmplm)*(SE/SE+summary(tmplm)$coef[,2])
})
allresults <- data.frame(depvar = depvar,
do.call(rbind, regresults))
names(allresults)[2] <- "intercept"
allresults}
It worked, but as I said, I need rolling windows of 4 years of daily data which moves one month ahead for each new estimation so I tried using nested for loop and it did not work:
for (j in 1:7) {
for (i in 1:length(newdata)) {
#try(
Model<-lm(newdata[seq(i,1056,24),j+6] ~ newdata[seq(i,1056,24), 2:6])
#, silent=T)
betas <- as.matrix(coefficients(Model))
}}
The error is:
Error in model.frame.default(formula = newdata[seq(i, 1056, 24), j + 6] ~ : invalid type (list) for variable 'newdata[seq(i, 1056, 24), j + 6]'
I am a beginner and I really appreciate your help