I have a large matrix (1,000,000 rows by 1,140 columns) which I'm storing using the ff package.
Is there an efficient way to calculate a covariance matrix from this? Using the cov function gives the error:
Error in cov(X) : supply both 'x' and 'y' or a matrix-like 'x'
Which is not surprising given that cov doesn't understand ff objects. I'm currently using a simple nested for loop:
covarianceMatrix <- matrix(0,nrow=ncol(ffObject),ncol=ncol(ffObject))
distinctValues <- sum(ncol(ffObject):1)
for(i in 1:ncol(ffObject))
{
for(j in i:ncol(ffObject))
{
if(i==j)
{
covarianceMatrix[i,j] <- var(ffObject[,i])
}
else
{
covarianceMatrix[i,j] <- covarianceMatrix[j,i] <- cov(ffObject[,i],ffObject[,j])
}
}
}
which works but is very slow.