Announcement

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

  • How to compute the distance between some key centroids and other ones, using Harversine formula?

    Hi Statalist Community,

    I have to compute the distance between centroids for some municipalities in my sample. But it is the first time that I am doing this, and I do not know how to compute the Harversine formula.

    Basically, I want to create a dataset and keep municipalities that are betweeen 50 and 200 kilometres from key municipalities that I have in my sample.

    Here is a -dataex-:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float cmunine str47 munname double(lat lon)
    1001 "Alegría-Dulantzi"       42.83981158 -2.51243731
    1002 "Amurrio"                 43.05427776 -3.00007326
    1003 "Aramaio"                 43.05119653 -2.56540037
    1004 "Artziniega"              43.12084358 -3.12791718
    1006 "Armiñón"               42.72326199 -2.87183475
    1008 "Arratzua-Ubarrundia"     42.89116171 -2.63878816
    1009 "Asparrena"               42.88968768 -2.31670754
    1010 "Ayala/Aiara"             43.07768283 -3.04306746
    1011 "Baños de Ebro/Mañueta" 42.52925602 -2.67863583
    1013 "Barrundia"               42.91537538 -2.49407452
    end
    For example, let say that I am interested in centroid distance between "Amurrio" and the other municipalities.
    • How could I compute the Harversine formula and compute the distance in stata? Is there any command that allows me to do that, please?
    Thank you very much in advance for your help.

    Best,

    Michael


  • #2
    Code:
    ssc install geodist, replace
    help geodist

    Comment


    • #3
      Hi Andrew Musau,

      Thank you so much for your answer. Everything works perfectly well.

      Here is the code that I implemented:

      Code:
          clear all
      
          use "./municipalityCoordinates.dta", clear
      
          * Define the municipalities for which to calculate distances
          local municipalities "Alcázar_de_San_Juan Alcalá_de_Guadaíra Carmona Escatrón Totana"
      
          * Loop over the specified municipalities
      
          foreach mun of local municipalities {
      
              * Get the coordinates of the current municipality
      
              local munname : subinstr local mun "_" " " , all
      
              qui su lat if munname == "`munname'", meanonly
      
              local lat1 = r(mean)
      
              qui su lon if munname == "`munname'", meanonly
      
              local lon1 = r(mean)
              
              
              * Calculate the distance using geodist
      
              geodist `lat1' `lon1' lat lon, generate(`mun'_km) sphere
              
              label variable `mun'_km "Centroid Distance from `munname'"
              
          }
      Thank you very much for your help, again.
      if my code is not right, don't hesitate to give me feedback and correct me if necessary.

      Michael
      Last edited by Michael Duarte Goncalves; 07 Jun 2024, 08:31.

      Comment


      • #4
        Stata's native -search- command often (though not always) produces useful results if one simply searches with a keyword that would appear in a post. In my experience this is easier than searching the past content stream of StataList, which -search- does *not* do. In the current situation,
        Code:
        search haversine
        reveals several potentially helpful programs, including -geodist-, a useful, efficient, and friendly package.

        -search- takes seconds, and skimming its results takes minutes, so I'd encourage more people to give it a try for virtually any question.

        Comment


        • #5
          Hi Mike Lacy,

          Thank you so much! I didn't know about this nice feature of search.
          Generally, what I do is exactly that: I start searching for topics by keyword on StataList. Sometimes this works well, but unfortunately many other times it doesn't.

          Thank you for sharing this very useful tool with me!

          All the best,


          Michael

          Comment

          Working...
          X