I am running a multiple rolling window quantile regressions on stock data such that the resulting output is an xts file with coefficients estimated at each point in time. Final estimators are then approximated from the quantiles. The 5 regressions are then together looped using a for argument across all my stocks.
What i am trying to do? I need to loop and store xts output that looks like this one presented below in a list and under unique name so that I can use it later in the next step of my methodology.
(Intercept) rmrf smb hml rmw cma
2015-05-21 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
2015-05-22 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
2015-05-26 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
2015-05-27 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
2015-05-28 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
2015-05-29 -0.001070362 0.9647046 -0.1206183 -0.05204882 0.01866969 -0.0361021
My problem arises at the end when I want to store my result into a list. This happens because I want the name of the dataset to have the same name as the column I perform the regression on.
The code has been simplified to just one regression. My best attempt to solve this is below:
testlist <- list()
for(i in 1:ncol(stocks) {
stock_data <- stocks[,i]
# merge data together
regression_input <- merge(stock_data, rmrf, smb, hml, rmw, cma, rf)
#rename
colnames(regression_input) = c("stock_returns" , "rmrf" , "smb" , "hml" , "rmw" , "cma" , "rf")
quantile005 <- as.xts(
rollapply(zoo(regression_input),
width=200,
FUN = function(Z)
{
t = rq(stock_returns ~ rmrf + smb + hml + rmw + cma, tau=0.05, data = as.data.frame(Z),
method="br", model = TRUE);
return(t$coef)
},
by.column=FALSE, align="right")
)
final_estimators <- # additional calculations are performed with results stored here
#save
name <- paste(rownames(i), sep = "")
testlist[[name]] <- final_estimators
}
Furthermore: this command does not seem to read the name of my row even when outside loop.
> name <- paste(rownames(return_data[,1]), sep = "")
> name
character(0)
>
EDIT W SOLUTION
It seems like the solution was already there for me. the issue was that the colnames function retrieves names from a dataframe object only. here is the final version of the code.
# Create list to store dataframes
testlist <- list()
# Loop over entire regression methodology
for(i in 1:ncol(test_3_stocks)) {
# Get regression input together
stock_data <- test_3_stocks[,i] - ff_data[,7]/100
regression_input <- merge(stock_data, rmrf, smb, hml, rmw, cma, rf)
colnames(regression_input) = c("stock_returns" , "rmrf" , "smb" , "hml" , "rmw" , "cma" , "rf")
#Rolling window regression - Quantile coefficients data
quantile005 <- as.xts(
rollapply(zoo(regression_input),
width=200,
FUN = function(Z)
{
t = rq(stock_returns ~ rmrf + smb + hml + rmw + cma, tau=0.05, data = as.data.frame(Z),
method="br", model = TRUE);
return(t$coef)
},
by.column=FALSE, align="right")
)
print(tail(summary(quantile005)))
tmp <- summary(quantile005)
name <- colnames(as.data.frame(test_3_stocks[,i]))
testlist[[name]] <- tmp
}
End result:
> tail(testlist$TEST1)
(Intercept) rmrf smb hml rmw cma
2015-05-21 1255.853 -7.16453 531.4655 1870.740 1422.398 -4034.082
2015-05-22 1256.221 -40.88781 512.3803 1700.796 1569.501 -3830.814
2015-05-26 1256.413 -152.42752 713.5793 1754.086 1452.681 -3771.936
2015-05-27 1256.707 15.39627 451.2127 1568.405 1246.730 -3665.781
2015-05-28 1257.893 -133.72554 705.0280 1816.560 1232.326 -4188.772
2015-05-29 1258.239 -148.11936 624.4424 1837.348 1098.122 -4301.114
So close! You've correctly isolated the portion of code that sets up the name incorrectly:
name <- paste(rownames(return_data[,1]), sep = "")
Assuming return_data
looks like the data.frame you listed at the top of your post, where the rownames are the dates, as in 2015-05-21
, then it's an easy tweak:
name <- row.names(return_data)[i]