The first thing I'd try is "scale", as that should not affect the
Mahalinobis distances:
Fgmat <- scale(fgmatrix)
fg.cov <- cov.wt(Fgmat)
mahalanobis(Fgmat, center = Fg.cov$center, cov = Fg.cov$cov)
Does this give you the same result. If no, the problem was that
fgmatrix was not sufficiently well conditioned to support this
computation.
If this does NOT solve the problem, I'd manually contruct a ginverse
of Fg.cov$cov, proceeding roughly as outlined in the following example:
set.seed(1)
X10 <- array(rnorm(760), dim=c(76, 10))
X15.10 <- cbind(X10, X10[,1:5])
fg.cov <- cov.wt(X15.10)
mahalanobis(X15.10, center = fg.cov$center, cov = fg.cov$cov)
(S15.10 <- eigen(fg.cov$cov, symmetric=TRUE))
# Only 10 non-zero eigenvalues
fg.Info <- crossprod(S15.10$vectors[,1:10] /
rep(sqrt(S15.10$values[1:10]), 15))
mahalanobis(X15.10, center = fg.cov$center,
cov = fg.cov$cov, inverted=TRUE)
The key is computing your own generalized inverse and using that with
"inverted=TRUE".
spencer graves
Karen Kotschy wrote:> Dear R community
>
> Have just recently got back into R after a long break and have been amazed
at
> how much it has grown, and how active the list is! Thank you so much to all
> those who contribute to this amazing project.
>
> My question:
> I am trying to calculate Mahalanobis distances for a matrix called
"fgmatrix"
>
>
>>dim(fgmatrix)
>
> [1] 76 15
>
>
>>fg.cov <- cov.wt(fgmatrix)
>>mahalanobis(fgmatrix, center = fg.cov$center, cov = fg.cov$cov)
>
>
> Then I get an error message "Covariance matrix is apparently
singular"
>
> What does this mean? I can't see anything strange about the covariance
matrix,
> and am not getting anywhere with the help files.
>
>
>>dim(fg.cov$cov)
>
> [1] 15 15
>
>>length(fg.cov$center)
>
> [1] 15
>
>
> Thanks
--
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA
spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel: 408-938-4420
Fax: 408-280-7915