Hi:
> a <- matrix(1:8, 2, 4)
> cov(a) # using cov() from stats package
[,1] [,2] [,3] [,4]
[1,] 0.5 0.5 0.5 0.5
[2,] 0.5 0.5 0.5 0.5
[3,] 0.5 0.5 0.5 0.5
[4,] 0.5 0.5 0.5 0.5
After using package sos to discover that ccov() is a function in package
robust (which was conveniently omitted), we find that
> library(robust)
Loading required package: MASS
Loading required package: lattice
Loading required package: robustbase
Loading required package: rrcov
Loading required package: pcaPP
Loading required package: mvtnorm
Scalable Robust Estimators with High Breakdown Point (version 1.0-01)
Attaching package: 'robust'
The following object(s) are masked from 'package:stats':
cov
> ccov(a)
Error in solve.default(cov, ...) :
Lapack routine dgesv: system is exactly singular
so the reason for the singularity is that you're using a robust method of
computing the covariance matrix, and when you resort to cov() as a backup,
it's using robust's version rather than the one in package stats (see
the
message above). I'm not expert enough in robust statistics to explain why
the singularity occurs, but there is some linear dependency that is produced
when the robust method of fitting a covariance matrix is applied to your
input matrix, possibly due to the weighting that takes place when the robust
estimation algorithm is applied.
This also applies to the transpose of your matrix:
> stats::cov(t(a))
[,1] [,2]
[1,] 6.666667 6.666667
[2,] 6.666667 6.666667> ccov(t(a))
Error in solve.default(cov, ...) :
Lapack routine dgesv: system is exactly singular
HTH,
Dennis
On Mon, Jul 19, 2010 at 7:09 AM, Wendy Han <wendyhan08@gmail.com> wrote:
> Hi,
>
> Excuse me for asking this silly question. But I really couldn't
understand
> why cov() and ccov() don't work for my calculation of covariance
matrix.
>
> a <- matrix(1:8, 2, 4)
> a
> [,1] [,2] [,3] [,4]
> [1,] 1 3 5 7
> [2,] 2 4 6 8
> > ccov(a)
> Error in solve.default(cov, ...) :
> Lapack routine dgesv: system is exactly singular
> I also tried colume bind, but it still doesn't work.
> > a1<- a[,1]
> > a2 <- a[,2]
> > a3<- a[,3]
> > a4 <- a[,4]
> > acomb <- cbind(a1,a2,a3,a4)
> > acomb
> a1 a2 a3 a4
> [1,] 1 3 5 7
> [2,] 2 4 6 8
> > ccov(acomb)
> Error in solve.default(cov, ...) :
> Lapack routine dgesv: system is exactly singular
> Yesterday, I used cov(), my function worked well. But today I ran the same
> code, it returned an error like:
>
> cov(td12)
> Error in solve.default(cov, ...) :
> system is computationally singular: reciprocal condition number >
1.97276e-24
> In addition: Warning message:
> 'cov' is deprecated.
> Use 'ccov' instead.
> See help("Deprecated")
> I don't know what is wrong, and I appreciate you kind help! Thank you!
>
> Wendy
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]