-1

I'm trying to calculate the covariance of a matrix which has two colinear vectors. I have read that it was impossible with the "cov" function from R.

Does a different function exist on R to calculate the covariance of a matrix which has two colinear vectors (since it works on Matlab and Excel).

Thank you in advance for your answers

willo
  • 1
  • 1

1 Answers1

2

Please consider providing a reproducible example with sample of your data and the corresponding code. Broadly speaking, a covariance matrix can be created with use of the code below:

# Vectors
V1 <- c(1:4)
V2 <- c(4:8)
V3 <- runif(n = 4)
V4 <- runif(n = 4)

#create matrix
M <- cbind(V1,V2, V3, V4)

# Covariance
cov(M)

I'm guessing that you may be getting the following error:

number of rows of result is not a multiple of vector length (arg 1)

You could first try to use the cov function as discussed here.

Community
  • 1
  • 1
Konrad
  • 17,740
  • 16
  • 106
  • 167
  • The "cov" function does not give me an error but the determinant of the covariant matrix is always 0 when two column of the initial matrix are colinear... – willo Nov 23 '15 at 12:33
  • @willo Have a look at [this discussion](http://stats.stackexchange.com/questions/50318/how-to-avoid-0-determinant-when-sample-covariance-matrix-has-very-small-values) on dealing with 0 determinant. It appears to me that your question is more concerned with the statistical aspects of the work you are willing to undertaken than with **R** as such. For alternatives to the `cov` function you may have a look at the `corpcor` [package](https://cran.r-project.org/web/packages/corpcor/corpcor.pdf). – Konrad Nov 23 '15 at 13:07