Announcement

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

  • Count with nearstat

    Hi all,
    My dataset is made up of gasoline stations located by latitude and longitude. These stations are divided into different brands.

    I was able to count how many stations there are between zero and one kilometre for each station using:

    Code:
    nearstat latitudine longitudine, near(latitudine longitudine)  distvar(distnn1) ncount(nn_01) dband(0 1) replace
    Now I should count how many stations there are belonging to a particular brand between zero and one kilometre for every single station.
    For example: take the first station, Q8. How many Shell stations are there close to Q8 between zero and one kilometre?
    take the second station, Tamoil. How many Shell stations are there close to Tamoil between zero and one kilometre?
    and so on for every station in my dataset.

    I tried to insert the "if" operator but I get the same result as before.

    Code:
    nearstat latitudine longitudine if Shell==1, near(latitudine longitudine)  distvar(distnn1) ncount(nn_01) dband(0 1) replace
    Does anyone know how to do it?

    Thanks in advance.

    Mario

  • #2
    I post here a small piece of my dataset

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long idimpianto str22 bandiera float(latitudine longitudine)
    17169 "Repsol"    41.91485 12.463936
    21227 "Tamoil"    41.89114 12.502335
     9893 "Agip Eni"  41.87744  12.47566
    15691 "Q8"        41.90972 12.451632
    30852 "Api-Ip"     41.9213 12.460135
    14017 "Api-Ip"    41.90893 12.492738
    16793 "Agip Eni"  41.91527 12.469448
    13832 "Agip Eni"  41.92608 12.457652
     9580 "Agip Eni"  41.89259    12.508
    20188 "Api-Ip"    41.91012 12.474128
    12185 "Agip Eni"   41.9104   12.4465
    19955 "Agip Eni"  41.89331 12.488094
    18204 "Api-Ip"    41.90123 12.504909
    16807 "Agip Eni"  41.88868 12.478415
    16854 "Agip Eni"  41.87151  12.49283
    17108 "Api-Ip"    41.90509  12.46026
    18523 "Q8"        41.91019 12.474433
    28514 "Api-Ip"    41.88104 12.484383
    16769 "Api-Ip"    41.91599  12.45428
    20893 "Total Erg" 41.93837 12.537647
    17837 "Api-Ip"    41.91119 12.444272
    22288 "Retitalia" 41.90407 12.446028
    11812 "Q8"        41.94366 12.537134
     4864 "Agip Eni"  42.01099 12.517746
    17955 "Api-Ip"    41.91938 12.473583
    end
    where "idimpianto" is the unique number that identifies gasoline station and "bandiera" is station brand

    Code:
    * Count how many stations there are between zero and one kilometre for each station
    nearstat latitudine longitudine, near(latitudine longitudine) distvar(distnn1) ncount(nn_01) dband(0 1) replace
    Code:
    * how many Agip Eni are there in a kilometer for each station? using 
    
    Code:
     if
    operator nearstat latitudine longitudine if bandiera=="Agip Eni", near(latitudine longitudine) distvar(distnn_eni1) ncount(nn_eni_01) dband(0 1) replace

    Comment


    • #3
      Also posted on https://stackoverflow.com/questions/...rstat-in-stata

      Comment


      • #4
        I'm not familiar with nearstat, but I think you should look into geonear. You'll get a combination of all neighbors and then you should be able to do the qualifiers/tabulations from there.

        Note geonear is from SSC and written by Robert Picard.

        Comment


        • #5
          Going with Justin Blasongame's advice, here is one way:

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input long idimpianto str22 bandiera float(latitudine longitudine)
          17169 "Repsol"    41.91485 12.463936
          21227 "Tamoil"    41.89114 12.502335
           9893 "Agip Eni"  41.87744  12.47566
          15691 "Q8"        41.90972 12.451632
          30852 "Api-Ip"     41.9213 12.460135
          14017 "Api-Ip"    41.90893 12.492738
          16793 "Agip Eni"  41.91527 12.469448
          13832 "Agip Eni"  41.92608 12.457652
           9580 "Agip Eni"  41.89259    12.508
          20188 "Api-Ip"    41.91012 12.474128
          12185 "Agip Eni"   41.9104   12.4465
          19955 "Agip Eni"  41.89331 12.488094
          18204 "Api-Ip"    41.90123 12.504909
          16807 "Agip Eni"  41.88868 12.478415
          16854 "Agip Eni"  41.87151  12.49283
          17108 "Api-Ip"    41.90509  12.46026
          18523 "Q8"        41.91019 12.474433
          28514 "Api-Ip"    41.88104 12.484383
          16769 "Api-Ip"    41.91599  12.45428
          20893 "Total Erg" 41.93837 12.537647
          17837 "Api-Ip"    41.91119 12.444272
          22288 "Retitalia" 41.90407 12.446028
          11812 "Q8"        41.94366 12.537134
           4864 "Agip Eni"  42.01099 12.517746
          17955 "Api-Ip"    41.91938 12.473583
          end
          *ssc install geonear
          tempfile gas_stations names names2
          preserve
          rename (idimpianto latitudine longitudine) (nid nlat nlon)
          save `gas_stations', replace
          keep nid bandiera
          save `names', replace
          rename (nid bandiera) (idimpianto name)
          save `names2', replace
          restore
          *ALL GAS STATIONS WITHIN 1 KM
          geonear id lat lon using "`gas_stations'", n( nid nlat nlon) ign long within(1) near(0)
          merge m:1 nid using `names', keep(master match) nogen
          merge m:1 idimpianto using `names2', keep(master match) nogen
          order idimpianto name nid bandiera  
          *TOTAL NUMBER - ALL GAS STATIONS
          bys idimpianto: egen total= count(nid)
          *TOTAL NUMBER AGIP-ENI
          bys idimpianto: egen Agip_Eni= total( bandiera== "Agip Eni")
          Res.:

          Code:
          . l, sepby(id)
          
               +-------------------------------------------------------------------------+
               | idimpi~o        name     nid    bandiera   km_to_nid   total   Agip_Eni |
               |-------------------------------------------------------------------------|
            1. |     9580    Agip Eni   18204      Api-Ip   .99425279       2          0 |
            2. |     9580    Agip Eni   21227      Tamoil   .49590694       2          0 |
               |-------------------------------------------------------------------------|
            3. |     9893    Agip Eni   28514      Api-Ip   .82550686       1          0 |
               |-------------------------------------------------------------------------|
            4. |    11812          Q8   20893   Total Erg   .58985922       1          0 |
               |-------------------------------------------------------------------------|
            5. |    12185    Agip Eni   17837      Api-Ip   .20419082       4          0 |
            6. |    12185    Agip Eni   15691          Q8   .43131316       4          0 |
            7. |    12185    Agip Eni   22288   Retitalia   .70478981       4          0 |
            8. |    12185    Agip Eni   16769      Api-Ip    .8947647       4          0 |
               |-------------------------------------------------------------------------|
            9. |    13832    Agip Eni   30852      Api-Ip   .56982039       1          0 |
               |-------------------------------------------------------------------------|
           10. |    15691          Q8   17108      Api-Ip   .88038104       5          1 |
           11. |    15691          Q8   22288   Retitalia   .78082912       5          1 |
           12. |    15691          Q8   16769      Api-Ip   .73056183       5          1 |
           13. |    15691          Q8   12185    Agip Eni   .43131316       5          1 |
           14. |    15691          Q8   17837      Api-Ip   .63051576       5          1 |
               |-------------------------------------------------------------------------|
           15. |    16769      Api-Ip   12185    Agip Eni    .8947647       5          1 |
           16. |    16769      Api-Ip   15691          Q8   .73056183       5          1 |
           17. |    16769      Api-Ip   17169      Repsol   .80897278       5          1 |
           18. |    16769      Api-Ip   30852      Api-Ip   .76377999       5          1 |
           19. |    16769      Api-Ip   17837      Api-Ip   .98514433       5          1 |
               |-------------------------------------------------------------------------|
           20. |    16793    Agip Eni   20188      Api-Ip   .69162537       4          0 |
           21. |    16793    Agip Eni   17169      Repsol   .45852914       4          0 |
           22. |    16793    Agip Eni   18523          Q8   .69954855       4          0 |
           23. |    16793    Agip Eni   17955      Api-Ip   .57075678       4          0 |
               |-------------------------------------------------------------------------|
           24. |    16807    Agip Eni   28514      Api-Ip   .98282863       2          1 |
           25. |    16807    Agip Eni   19955    Agip Eni   .95245506       2          1 |
               |-------------------------------------------------------------------------|
           26. |    17108      Api-Ip   15691          Q8   .88038104       1          0 |
               |-------------------------------------------------------------------------|
           27. |    17169      Repsol   17955      Api-Ip   .94399078       5          1 |
           28. |    17169      Repsol   16793    Agip Eni   .45852914       5          1 |
           29. |    17169      Repsol   16769      Api-Ip   .80897278       5          1 |
           30. |    17169      Repsol   30852      Api-Ip   .78317707       5          1 |
           31. |    17169      Repsol   20188      Api-Ip   .99393177       5          1 |
               |-------------------------------------------------------------------------|
           32. |    17837      Api-Ip   15691          Q8   .63051576       4          1 |
           33. |    17837      Api-Ip   16769      Api-Ip   .98514433       4          1 |
           34. |    17837      Api-Ip   12185    Agip Eni   .20419082       4          1 |
           35. |    17837      Api-Ip   22288   Retitalia   .80473515       4          1 |
               |-------------------------------------------------------------------------|
           36. |    17955      Api-Ip   16793    Agip Eni   .57075678       2          1 |
           37. |    17955      Api-Ip   17169      Repsol   .94399078       2          1 |
               |-------------------------------------------------------------------------|
           38. |    18204      Api-Ip    9580    Agip Eni   .99425279       1          1 |
               |-------------------------------------------------------------------------|
           39. |    18523          Q8   16793    Agip Eni   .69954855       2          1 |
           40. |    18523          Q8   20188      Api-Ip   .02650829       2          1 |
               |-------------------------------------------------------------------------|
           41. |    19955    Agip Eni   16807    Agip Eni   .95245506       1          1 |
               |-------------------------------------------------------------------------|
           42. |    20188      Api-Ip   17169      Repsol   .99393177       3          1 |
           43. |    20188      Api-Ip   16793    Agip Eni   .69162537       3          1 |
           44. |    20188      Api-Ip   18523          Q8   .02650829       3          1 |
               |-------------------------------------------------------------------------|
           45. |    20893   Total Erg   11812          Q8   .58985922       1          0 |
               |-------------------------------------------------------------------------|
           46. |    21227      Tamoil    9580    Agip Eni   .49590694       1          1 |
               |-------------------------------------------------------------------------|
           47. |    22288   Retitalia   15691          Q8   .78082912       3          1 |
           48. |    22288   Retitalia   12185    Agip Eni   .70478981       3          1 |
           49. |    22288   Retitalia   17837      Api-Ip   .80473515       3          1 |
               |-------------------------------------------------------------------------|
           50. |    28514      Api-Ip    9893    Agip Eni   .82550686       2          2 |
           51. |    28514      Api-Ip   16807    Agip Eni   .98282863       2          2 |
               |-------------------------------------------------------------------------|
           52. |    30852      Api-Ip   16769      Api-Ip   .76377999       3          1 |
           53. |    30852      Api-Ip   13832    Agip Eni   .56982039       3          1 |
           54. |    30852      Api-Ip   17169      Repsol   .78317707       3          1 |
               +-------------------------------------------------------------------------+
          
          .

          Comment

          Working...
          X