问题
I'm trying to run the LM function to collect the full output (summary) over a fairly large xts series (etfadj3) of different securities' returns. Currently, I can calculate a beta and capture all the regression outputs for the first column's return series against each column (security's) return series that follows - this is what I want. However, I would like to be able to modify this script so that I can vary the frequency of the regression to see a monthly/quarterly/yearly result. Is there a way to modify what I have to accomplish this? Thanks!!!
storage <- list()
for(i in names(etfadj3)[-1]){
storage[[i]] <- summary(lm(etfadj3$OPP~get(i), etfadj3)
)}
# Regression Coefficient Data
coeffs=sapply(storage, FUN=function(item){item$coefficients})
coeffs= as.data.frame(coeffs)
rownames(coeffs) <- c('Intercept_Coeff','ETF_Beta','Intercept_Std_Err','ETF_Beta_Std_Err','Intercept_T-Stat','ETF_Beta_T_Stat',
'Intercept_p-value','ETF_Beta_p-value')
# R-Squared
rsq=sapply(storage, FUN=function(item){item$r.squared})
rsq= t(as.data.frame(rsq))
rownames(rsq) <- c('R-Squared')
# Mean squared error
mse=sapply(storage, FUN=function(item){mean(item$residuals^2)})
mse= t(as.data.frame(mse))
rownames(mse) <- c('Mean Squared Error')
storage2 <- rbind(coeffs,rsq,mse)
storage3 <- as.data.frame(t(storage2))
来源:https://stackoverflow.com/questions/55595810/beta-loop-that-allows-for-segmented-time-series-frequency