I have a list called res.Sigma
containing 100 covariance matrices of order 4*4, where each matrix corresponds to a fixed time. The values of the time variable are stored in a variable called X
.
Now, we can compute the time-varying sample variances and store them in a dataframe as follows:
sample.var <- data.frame(X = X,
v11 = res.Sigma %>% lapply(function(val) val[1,1]) %>% unlist(),
v22 = res.Sigma %>% lapply(function(val) val[2,2]) %>% unlist(),
v33 = res.Sigma %>% lapply(function(val) val[3,3]) %>% unlist(),
v44 = res.Sigma %>% lapply(function(val) val[4,4]) %>% unlist())
Similarly, the time-varying sample correlations can be computed and stored as follows:
sample.corr <- data.frame(X = X,
r12 = res.Sigma %>% lapply(function(val)
val[1,2]/sqrt(val[1,1]*val[2,2])) %>% unlist(),
r13 = res.Sigma %>% lapply(function(val)
val[1,3]/sqrt(val[1,1]*val[3,3])) %>% unlist(),
r14 = res.Sigma %>% lapply(function(val)
val[1,4]/sqrt(val[1,1]*val[4,4])) %>% unlist(),
r23 = res.Sigma %>% lapply(function(val)
val[2,3]/sqrt(val[2,2]*val[3,3])) %>% unlist(),
.
.
.
)
There will be 4C2 = 6 columns for sample correlations between each pair of variables. However, if there were 20 variables instead of 4, there would be 20C2 = 190 such columns which will be impossible to write manually as I have written above.
I'm looking for a way to do this automatically, and name the columns appropriately (r_{i,j}
for the column containing correlations between the i-th and the j-th variables).
Any help will be appreciated. Let me know if my question is unclear.