Announcement

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

  • store the mean value of all the elements in matrix in certain variable

    my data is as the picture shows. Now I want to compute the dissimilarity of partners for one firm. For example, for firm "170040", I first calculate the Euclidean distance between any two partners (i.e., the Euclidean distance between "392896" and "433578", the Euclidean distance between "433578" and "434390", and the Euclidean distance between "434390" and "392896"), using the following code.

    forvalue i=1/3{
    matrix dissim D`i'=cat1 cat2 cat3 cat4 cat5 if n==`i', Euclidean
    matrix list D`i'
    matsum D`i', all(D`i')d
    }

    The result only show in the results window. But I want to generate a variable of "distance", which store the mean of the above results.

    How can this be done? How to store the mean value of all the elements in one matrix into a variable?

    Thanksf.

  • #2
    I don't know what the -matsum- command is, nor where it comes from. So I'm not entirely sure what you're doing here. But if what you wish is to calculate the mean value of the elements of D`i' and store it in a variable in those observations where n == `i', you can do this:
    Code:
    gen mean_of_Dn = .
    forvalue i=1/3{
        matrix dissim D`i'=cat1 cat2 cat3 cat4 cat5 if n==`i', Euclidean
        matrix list D`i'
        local r = rowsof(D`i')
        local c = colsof(D`i')
        matrix Jpost = J(`c', 1, 1)
        matrix Jpre = J(1, `r', 1)
        matrix S = Jpre * D`i '* Jpost
        replace mean_of_Dn = S[1,1]/(`r'*`c') if n == `i'
        matsum D`i', all(D`i')d //WHATEVER THIS IS??
     }
    I'm sure there's an even simpler way to do this in Mata, but I wouldn't know how. Note that if any elements of D`i' are missing, then mean_of_Dn will also be missing for observations where n= D`i'.

    A couple of points of Statalist etiquette: when using a command that is not part of official Stata (such as -matsum-) you are asked to say what it is and where it comes from. And the posting of screen snapshots to show data is discouraged: yours happens to be (barely) readable, but most are not. Listing out the relevant data excerpt and posting the result within code delimiters is the preferred way.

    Comment

    Working...
    X