Announcement

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

  • Matching observations

    Hello everyone,

    I have one calculation in Stata and would like to ask your advice. The dataset is as below:
    City Region Height Weight
    London North 2.5 20
    London North 1.3 22
    London South 2.4 12
    London South 2.6 13
    London South 1 14
    New York North 1.2 11
    New York South 2 14
    New York South 1.8 12
    I need to match (pair) each observation in the Region (North) - treated group, with one observation in the Region (South) - controlled group. The conditions are these both observations are in same city, and with the lowest distance (nearest neighbour) calculated based on Height and Weight.

    Could you please help and give me advice on the Stata code?

    Many thanks.

  • #2
    Save for the title, #1 is verbatim from https://www.statalist.org/forums/for...nobis-distance. There was a response to that post. Did you try the suggestion? Repeating the same question rarely results in more helpful replies. Rather, see what FAQ Extra Advice 1.2 recommends: https://www.statalist.org/forums/help#adviceextras.

    Comment


    • #3
      Hi Andrew Musau, thank you very much. I am quite new to Stata and Statalist Forum, so I did not notice that you kindly gave some suggestions on my last post.

      Could you also please help me to write the Stata commands on details for the above exercise? I tried the syntax suggestions but seem that I still struggled with it.

      The data structure as below, each observation is one firm. I want to match firm in the North with one firm in the South, conditions: firms are same City, and the near neighbour of Weight and Height.

      Many thanks.

      Firm City Region Height Weight
      1 London North 2.5 20
      2 London North 1.3 22
      3 London South 2.4 12
      4 London South 2.6 13
      5 London South 1 14
      6 New York North 1.2 11
      7 New York South 2 14
      8 New York South 1.8 12
      Last edited by Ash Hoang; 20 Jul 2023, 08:07.

      Comment


      • #4
        I am reluctant to respond further as you do not outline what you did not understand after looking through the documentation. The best way to understand a subject in my opinion is through hands-on experience. In your case, it is literally a two-liner. Do the matching within-city if you want to constrain the matches in this way.


        Code:
        *ssc install psmatch2, replace
        
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str8 city str5 region float height byte weight
        "London"   "North" 2.5 20
        "London"   "North" 1.3 22
        "London"   "South" 2.4 12
        "London"   "South" 2.6 13
        "London"   "South"   1 14
        "New York" "North" 1.2 11
        "New York" "South"   2 14
        "New York" "South" 1.8 12
        end
        
        gen treated= region=="North"
        psmatch2 treated, mahalanobis(height weight) neighbor(1)
        Res.:

        Code:
        . l, sep(0)
        
             +----------------------------------------------------------------------------------------------------+
             |     city   region   height   weight   treated    _treated     _support   _weight   _id   _n1   _nn |
             |----------------------------------------------------------------------------------------------------|
          1. |   London    North      2.5       20         1     Treated   On support         1     6     4     1 |
          2. |   London    North      1.3       22         1     Treated   On support         1     7     3     1 |
          3. |   London    South      2.4       12         0   Untreated   On support         .     1     .     0 |
          4. |   London    South      2.6       13         0   Untreated   On support         .     2     .     0 |
          5. |   London    South        1       14         0   Untreated   On support         1     3     .     0 |
          6. | New York    North      1.2       11         1     Treated   On support         1     8     5     1 |
          7. | New York    South        2       14         0   Untreated   On support         1     4     .     0 |
          8. | New York    South      1.8       12         0   Untreated   On support         1     5     .     0 |
             +----------------------------------------------------------------------------------------------------+
        Again, look at the documentation to review the output.
        Last edited by Andrew Musau; 20 Jul 2023, 14:24.

        Comment


        • #5
          Dear Andrew Musau thank you, I can run the code and it works well.

          One more thing that I would like to ask, do you know how to add the code to constrain the City. As the matching observations should stay in the same city.

          I did this code: psmatch2 treated[if City], mahalanobis(Height Weight) neighbor(1), but it seems a wrong one.

          Very much appreciate to have your advice and opinions.

          Thanks.

          Comment


          • #6
            You have to do it one at a time as the command generates variables within the current dataset. I would use frames.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str8 city str5 region float height byte weight
            "London"   "North" 2.5 20
            "London"   "North" 1.3 22
            "London"   "South" 2.4 12
            "London"   "South" 2.6 13
            "London"   "South"   1 14
            "New York" "North" 1.2 11
            "New York" "South"   2 14
            "New York" "South" 1.8 12
            "New York" "North"   2.8 13
            "New York" "South" 1.5 18
            end
            
            gen treated= region=="North"
            levelsof city, local(cities)
            local i 0 
            foreach city of local cities{
                local++i
                cap drop frame city`i'
                frame put * if city=="`city'", into(city`i')
                frame city`i': psmatch2 treated, mahalanobis(height weight) neighbor(1)
                frame city`i': list, sep(0)
            }
            Res.:

            Code:
             
            . levelsof city, local(cities)
            `"London"' `"New York"'
            
            . 
            . local i 0 
            
            . 
            . foreach city of local cities{
              2. 
            .     local++i
              3. 
            .     cap drop frame city`i'
              4. 
            .     frame put * if city=="`city'", into(city`i')
              5. 
            .     frame city`i': psmatch2 treated, mahalanobis(height weight) neighbor(1)
              6. 
            .     frame city`i': list, sep(0)
              7. 
            . }
            
                 +--------------------------------------------------------------------------------------------------+
                 |   city   region   height   weight   treated    _treated     _support   _weight   _id   _n1   _nn |
                 |--------------------------------------------------------------------------------------------------|
              1. | London    North      2.5       20         1     Treated   On support         1     4     2     1 |
              2. | London    North      1.3       22         1     Treated   On support         1     5     2     1 |
              3. | London    South      2.4       12         0   Untreated   On support         .     1     .     0 |
              4. | London    South      2.6       13         0   Untreated   On support         2     2     .     0 |
              5. | London    South        1       14         0   Untreated   On support         .     3     .     0 |
                 +--------------------------------------------------------------------------------------------------+
            
                 +----------------------------------------------------------------------------------------------------+
                 |     city   region   height   weight   treated    _treated     _support   _weight   _id   _n1   _nn |
                 |----------------------------------------------------------------------------------------------------|
              1. | New York    North      1.2       11         1     Treated   On support         1     4     2     1 |
              2. | New York    South        2       14         0   Untreated   On support         1     1     .     0 |
              3. | New York    South      1.8       12         0   Untreated   On support         1     2     .     0 |
              4. | New York    North      2.8       13         1     Treated   On support         1     5     1     1 |
              5. | New York    South      1.5       18         0   Untreated   On support         .     3     .     0 |
                 +----------------------------------------------------------------------------------------------------+
            
            .

            Comment

            Working...
            X