I have two datasets:
- a $1*m$ matrix of "ideal" conditions for $m$ factors
- a $n*m$ matrix of $n$ observations (rows) for each of the $m$ factors
I would like to calculate for each observation in the second matrix, how far it is from the "ideal" condition. So the output would be $n$ values that represent "distances" from ideal conditions.
First question, is the Mahalanobis distance appropriate to use here? The $m$ factors are spatial in nature, and are related to each other.
Second question, how do I set this up in R?
I have tried a few examples with mahalanobis()
, mahalanobis.dist()
, and pairwise.mahalnobis()
, but I cannot see how these can be used with my example.
When I've tried to use my matrices with these functions, I get an error:
Error in solve.default(cov, ...) :
Lapack routine dgesv: system is exactly singular: U[8,8] = 0
Which I have come to understand means that one of my matrices is singular and therefore cannot be inverted. I am not entirely sure how to get around this issue, or if it needs to be gotten around at all for my purposes.
I have also read this thread, but it's a bit over my head.
The overall goal of this is to use the results to map out "ideal" habitat ranges for a particular species.
Any help, thoughts, or suggestions would be greatly appreciated!