Announcement

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

  • naive Mata syntax question / st_subview, selectvar, loop

    In Mata, I need to do a series of operations on each of many groups within my dataset, ending with the creation of a variable that is constant within each group. From what I have read in the documentation I think this should be done with a series of st_subviews. As far as I can see, the 'selectvar' element of st_data/view/subview picks all non-zero elements. I suppose I could create a dummy for each group, and then loop over those columns for selectvar. But I'm wondering if there's any way to just move down group by group without creating all those dummies.

  • #2
    Hi Frederick
    It would be nice if you made some sample code showing us where and how the problem appears.
    Sample data could some of the standard dataset in Stata, eg auto.dta (To get it: sysuse auto).

    Regards/nhb
    Kind regards

    nhb

    Comment


    • #3
      I think you should take a look at help panelsetup() and its companion functions. These are tools suited to your problem

      Comment


      • #4
        Hi, Niels,
        The following code works on my data, but on the entire set of observations that have been read in by st_view. There are two things I don't undrstand:
        1. How to do this [or any st_view operation] over a large number of groups, defined by one of the variables. Is there some i.varname operation to use, perhaps?
        2. I am sorry to say that I have also not understood the simple operation of using the scalar result if have at the end (prox) to modify the variable "prox" in the Stata dataset.
        Fred

        mata
        mata clear
        // reading in data. "prox" is a column of missing values, to be filled group-by-group
        // "ate" is the group identifier
        // "x_c" and "y_c" are coordinates. We are computing distances
        // we're interested in the sum of the inverse distances across all pairwise combinations within the group
        // the other variables don't matter for now
        x=.
        st_view(x, ., ("id_com ate ul add id x_c y_c prox"))

        // I need to do this operation on each group defined by the group "ate"
        // st_subview(?? )

        obs = rows(x) // here we make square matrices for the observations in the group, to fill with pair-wise distances
        distances = J(obs,obs,0)
        d_inverse = J(obs,obs,0)

        for (i=1; i<=obs; i++) {
        for (j=1; j<=obs; j++) {
        jlat = x[j,7]
        ilat = x[i,7]
        jlon = x[j,6]
        ilon = x[i,6]

        if (i != j) {
        d=((ilat-jlat)^2 + (ilon-jlon)^2)^.5
        }
        else {
        d=1
        }
        dinv = 1/(d^.5)
        distances[i,j]=d
        d_inverse[i,j]=dinv
        }
        }

        prox = ((sum(d_inverse) - trace(d_inverse)) / 2) + trace(d_inverse) // but how to use this value to replace the value for the *variable* prox, within the group?

        end

        // on to the next group...

        Comment


        • #5

          I've just seen Christophe's suggestion, which looks like the solution to the main problem. Thank you very much, Christophe. Now I just need to understand this triivial thing of writing a scalre result to replace a variable's value (in this case, as a constant withing the group).

          Comment

          Working...
          X