Search code examples
rfinance

Creating list of matrices in R


> fact.cov <- matrix(NA,ncol(fact.weight),ncol(fact.weight))

> for (row in 15:nrow(fact.weight)) {
    >> fact.cov[[row]] <- var(fact.wealth.return[(row-1):row,])
}

When I run this code I get the following error message:

Error in fact.cov[[row]] <- var(fact.wealth.return[(row - 1):row, ]) : 
  more elements supplied than there are to replace

For each point in time (row), I need to create a covariance matrix. Do someone knows how to do this?

Thank you in advance!


Solution

  • This could be helpful:

    fact.cov <- list()
    for (k in 15:fact.weight) {
     fact.cov[[k]] <- var(fact.wealth.return[(k-1):k,])    
    };rm(k)
    

    However, you should provide more details in order for your code to be reproduceable (e.g. what is fact.wealth.return or fact.weight).