Announcement

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

  • Using geonear command within groups

    I am trying to find the nearest neighbour using the geonear command conditional on observations sharing the same group. For example, matching individuals to their nearest neighbour (in kms) who shares the same gender. Below is an example of trying to match "cell towers" that share the same group identifier. This is adapted from the description in the geonear help file to match cell towers based on their assigned group. Is there a more efficient way to do this other than what I am doing below?

    ** Create Dataset
    clear
    set obs 1000
    gen ctid = _n
    gen double ctlat = 37 + (41 - 37) * uniform()
    gen double ctlon = -109 + (109 - 102) * uniform()
    sort ctid
    g ctgroup = ceil(ctid / 10) // group cell towers into groups of 10
    sum ctgroup
    local G = `r(max)'
    tempfile cell
    save `cell',replace

    ** Use geonear withn each group
    forvalues j=1(1)`G'{
    use `cell' , clear
    keep if ctgroup == `j'
    tempfile cell`j'
    save `cell`j'' , replace
    geonear ctid ctlat ctlon using `cell`j'', n(ctid ctlat ctlon) ignoreself
    save `cell`j'' , replace
    }

    ** Append the per group datasets
    use `cell1'
    forvalues j = 2(1)`G'{
    append using `cell`j''
    }
    ]


  • #2
    geonear is from SSC (FAQ Advice #12). Here is an example of using geodist (from SSC) to calculate distances within groups: https://www.statalist.org/forums/for...ating-distance. Same principle.

    Comment

    Working...
    X