Announcement

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

  • Generate random points around a centroid in circle

    Hi everyone,

    I am struggling to generate random points around a fixed point (defined by latitude and longitude) in Stata.

    Here is a snapshot of data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int _ID double(_CX _CY) float points
    111  181.0026701446173  -103.6144711763335 2
     35 179.83644482349095 -102.25931438232391 2
     95 173.62410139032926 -108.25203422744671 7
    120   176.696210689136  -109.6219042419326 2
    126 177.62000961513976 -109.32669927586673 4
     21  178.2751472150729 -104.06769361471842 4
     18 175.99721564981806 -108.14985411307012 4
     60   178.872501415726 -102.72883884892121 3
    119 176.90713462711736 -109.26361044014674 1
     20 178.09477094181506  -105.2803015017441 5
     40 176.37581822494718 -107.06071352670077 2
     36  176.2594233462089 -105.51304940173509 3
     34 178.38042942412704 -102.75056129345269 2
     69 176.36218391402446 -106.42921144019289 3
     97 175.82515586054384 -104.95031844141755 3
     51  172.8322553280801 -108.84294571923179 8
     77 175.52420409001067 -105.79796467844942 5
      3 174.49476863694187 -108.68563432712384 8
     25 178.91401313578396  -102.5597069872018 1
     14 179.57927904199687 -103.20172421743248 4
    end
    I was able to generate random points, however, they seems to be very far apart and mixed with other centroids. I am latter using these random points in the grmap command for choropleth mappping. I used the following following codes.

    Code:
    expand points
    gen y = _CY + runiform(0, .1)
    gen x = _CX + runiform(0, .1)
    scatter y x,msize(.5)
    Some of these points are good to go, but some were not identifiable with respect to its centroids.
    Does anyone know how to drawn these random number in a circle around the centroids(lat,long in data) that does not get mixed with other points in the data.

    Thanks and appreciation.





  • #2
    I don't understand all of the question. I focus here only on generating random points uniformly distributed within a circle.

    It seems that you're assuming a locally flat Earth, and ignoring sphericity, spheroidicity, geoidicity and elevation. As a geographer it's my job to point out that's all wrong, and then move on.

    I find it easier just to treat this as a problem in radial coordinates.

    Code:
    clear 
    set obs 1000
    set seed 2803  
    gen theta = 2 * _pi * runiform()  
    gen radius = sqrt(runiform())
    gen y = radius * cos(theta)
    gen x = radius * sin(theta)
    scatter y x , aspect(1) ms(oh)

    Comment


    • #3
      Let me clarify.

      I have no knowledge of sphericity, spheroidicity, geoidicity and elevation and do not know if it is relevant here or not?

      I want to draw random points around each observation in my dataset defined by X and Y coordinates. (ideally in circles)
      In the data given above, _ID 111 need to have 2 such random points around it. Obs. 95 needs 7 points, while obs 51 needs 8.
      The center points is defined by the _CX and _CY coordinates and the exact number of random points to draw is given in the variable "points"

      One solution in mind is to input these random points manually for each X and Y coordinate. But the dataset is large so I am searching for more time efficient and error free method.



      Thanks

      Comment


      • #4
        You do really: the Earth is a sphere, no spheroid, no geoid, no not flat because of mountains, and so forth.

        Otherwise my code is compatible with yours: just combine.

        Comment


        • #5
          Thank you. I am working on it.

          Comment

          Working...
          X