Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Compute Mahalanobis Distance between 2 Vectors (generated out of two different varlists)

    Hello,

    i am trying to calculate the Mahalanobis distance between two sets of variables i have for each observation in my sample.


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(moffsiterecoveryA monsiterecoveryA moffsiterecyclingA monsiterecyclingA moffsitetreatmentA monsitetreatmentA moffsiterecoveryT monsiterecoveryT moffsiterecyclingT monsiterecyclingT moffsitetreatmentT monsitetreatmentT)
     0  0  0 0  0   0 0 1        1 0         0 0
    .2  0 .8 0  0   0 . .        . .         . .
     0  0  1 0  0   0 1 0        1 0         0 0
     0  0  0 0 .5 .25 0 0        0 0         1 0
     0 .1 .3 0 .1  .1 0 0 .7142857 0 .14285715 0
    end
    ------------------ copy up to and including the previous line ------------------



    specifically, i want to compute the mahalanobis distance between these 6 variables: moffsiterecoveryA monsiterecoveryA moffsiterecyclingA monsiterecyclingA moffsitetreatmentA monsitetreatmentA and these 6 variables: moffsiterecoveryT monsiterecoveryT moffsiterecyclingT monsiterecyclingT moffsitetreatmentT monsitetreatmentT for every observation in my dataset.
    i tried using mahascore or mahascore2 from the mahapick package but i am struggling with creating the distance from one set of variables (or one vector) to another set of variables (or vector) and NOT to the mean of the first defined varlist. the idea is to create two vectors and then calculate the mahalanobis distance between them.
    if someone has an example of how to generate vectors out of variables, and then calculate the mahalanobis distance, i would be very greatful !



  • #2
    Please include the code you tried

    Comment


    • #3
      Code:
      mkmat moffsiterecoveryT monsiterecoveryT moffsiterecyclingT monsiterecyclingT moffsitetreatmentT monsitetreatmentT, matrix(targetmanagement)
      matname targetmanagement  moffsiterecoveryA monsiterecoveryA moffsiterecyclingA monsiterecyclingA moffsitetreatmentA monsitetreatmentA, columns(.)
      mahascore  moffsiterecoveryA monsiterecoveryA moffsiterecyclingA monsiterecyclingA moffsitetreatmentA, gen(temp) refvals(targetmanagement) unsq compute_invcovarmat
      it doesnt work though ! gives me an error

      Code:
      mahascore: refvals (targetmanagement) must have as many rows as vars in varlist

      Comment

      Working...
      X