Announcement

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

  • rounding ID numbers in adjacency matrix

    Hi everyone,

    I am new to Stata and Mata (please bear with me!). I just recently learned how to create adjacency matrices using mata. When I cross-checked the original data set with the matrix, I noticed that the matrix looked funny. All the ID numbers in the matrix are rounded to the nearest 100,000th (91600000 instead of 91578541). I adapted the code from UCLA ATS http://www.ats.ucla.edu/stat/stata/code/adj_matrix.htm. I would appreciate any insights.

    Best,

    Sarah Trinh

  • #2
    Sarah --

    While it is hard to know exactly what is going on, I have run into trouble before with large integers. This might help.

    Best,

    Matt

    Comment


    • #3
      Hmm, this is interesting. I would not think the precision loss is happening in Mata, where numbers are in double precision, which should give something like 16 digits for a decimal integer.

      I think it would help if you could tell us how your id numbers are stored in Stata, show the code for how you have been constructing the adjacency matrix in Mata, and show a sample of your data.

      All this being said, storing id numbers as strings is often preferable.

      Regards, Mike

      Comment


      • #4
        Hi again,

        Thank you Matthew and Mike for your responses and help. All variables are stored as long. I have tried to id variables as string but the code would not run. To provide further information, I have provided the code below
        describe aid
        describe mf_aid1
        drop scid1
        destring aid, replace
        destring mf_aid1, replace
        destring mf_aid2, replace
        destring mf_aid3, replace
        destring mf_aid4, replace
        destring mf_aid5, replace

        destring ff_aid1, replace
        destring ff_aid2, replace
        destring ff_aid3, replace
        destring ff_aid4, replace
        destring ff_aid5, replace
        describe mf_aid1
        describe aid

        rename aid id
        describe id
        describe mf_aid1 mf_aid2

        gen fid = _n

        set matsize 11000
        mkmat mf_aid* ff_aid*, mat (F)
        mkmat id, mat (id)
        scalar define n = _N
        mata

        a=st_matrix("F")
        id = st_matrix("id")
        b = J(st_numscalar("n"),st_numscalar("n"), 0)
        for(i = 1; i <= rows(a); i++) {
        for(j = 1; j<=cols(a); j++) {
        for(k=1; k<=rows(id); k++) {
        if (st_data(k,"id")==a[i,j]) {
        b[i, k]=1
        }
        }
        }
        }
        c = id,b
        id = .\id
        c = id'\c
        st_matrix("adj_W1_058", c)
        end

        mat list adj_W1_058
        preserve
        set obs `=_N+1'
        svmat adj_W1_058, names(col)
        outsheet c* using "D:\ahd\researchers\sltrinh\adj_W1_058.txt"


        I want to reiterate how thankful I am for your help. I do not personally know anyone who uses mata.

        Best,

        Sarah Trinh

        Comment


        • #5
          I don't have any way to understand the structure or meaning of your data. As it is, I'm having a hard time linking your description of your problem with your code.

          I take your problem to be something like: "I have a data file. I want to create an adjacency matrix from it in Mata." So, let's back up: What is the structure of your data file? I can imagine the following two organizations as likely:

          a) A B1, B2, B3, ...
          B C1, C2, C3, ...
          .....
          where A represents the id of one agent in a relationship, and B1, B2, ... represent all the other agents with which A has a relationship. In this case, your data file would contain one observation for each agent (country, person, etc.) that has relationships with any other.

          or

          b) A B1
          A B2
          ...
          ...
          B Bn
          B C1
          B C2
          ...

          In this case, each observation contains the id numbers of each pair of agents that has a relationship.

          If we knew which organization of data you have, our chances of figuring out how to get it into Mata would be better.

          Comment


          • #6
            I don't know the solution to this rounding issue, but one tip from one programming beginner to another- research foreach loops! It will save you a lot of tedium.

            for instance:
            destring mf_aid1, replace
            destring mf_aid2, replace
            destring mf_aid3, replace
            destring mf_aid4, replace
            destring mf_aid5, replace

            loops to

            foreach num of numlist 1/5{
            destring mf_aid`num', replace

            }

            Where the loop is iterating over values 1 through 5 and `num' subs in that particular value for the iteration.

            Comment

            Working...
            X