Announcement

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

  • How to replace values of a variable in this case

    Dear all,

    I want to replace the mean of a health outcome in post-policy period with pre-policy period. However, it returns "(0 real changes made)". Any help would be highly appreciated.

    Code:
    // compute mean of health for pre-policy
    bys country region urban: egen m_health = mean(health) if policy==1
    
    // try replacing the missing values of m_health in post-policy with those of the pre-policy means
    bys country region urban: replace m_health = m_health if policy==0
    
    // Result
    (0 real changes made)
    Data
    Code:
    clear
    input byte health str32 country byte(region urban policy)
    0 "A" 1 1 1
    1 "A" 2 0 1
    0 "A" 3 0 1
    1 "A" 4 0 1
    1 "A" 5 1 1
    1 "A" 6 0 0
    0 "A" 7 1 0
    0 "A" 2 0 0
    0 "A" 4 1 0
    0 "A" 6 1 0
    1 "A" 3 0 0
    0 "B" 1 0 1
    1 "B" 2 1 1
    1 "B" 3 1 1
    0 "B" 4 0 1
    1 "B" 5 0 1
    1 "B" 5 0 0
    1 "B" 6 0 0
    1 "B" 4 0 0
    0 "B" 2 0 0
    0 "B" 4 0 0
    0 "B" 3 0 0
    0 "B" 6 0 0
    0 "C" 2 1 1
    0 "C" 3 0 1
    0 "C" 4 0 1
    1 "C" 5 0 1
    1 "C" 6 0 1
    0 "C" 7 1 0
    1 "C" 1 0 0
    0 "C" 2 1 0
    1 "C" 6 0 0
    0 "C" 1 1 0
    1 "C" 5 0 0
    0 "C" 1 1 0
    1 "C" 3 0 0
    end

  • #2
    I think the mean value you are calculating needs some rethinking here. The data is already at the "country region urban" level. A mean here would give exactly the same value as that in the Health variable. Maybe I am not understanding your question properly. Can you describe what mean you want to calculate, that way we can think better on how to replace pre-policy values

    Comment


    • #3
      Originally posted by Fahad Mirza View Post
      I think the mean value you are calculating needs some rethinking here. The data is already at the "country region urban" level. A mean here would give exactly the same value as that in the Health variable. Maybe I am not understanding your question properly. Can you describe what mean you want to calculate, that way we can think better on how to replace pre-policy values
      Dear Fahad,

      Thank you for your reply. Much appreciated. I don't know what happened with my data example because in my full data, I was able to compute the means of health using code in #1. Let's assume that the means of health variable for pre-policy period was calculated using code in #1 (thus, variable m_health was generated). How to do what I am looking for?

      Data
      Code:
      clear
      input byte health str32 country byte(region urban policy) float m_health
      0 "A" 1 1 1 .0311927
      1 "A" 2 0 1 .0319532
      0 "A" 3 0 1    .0458015
      1 "A" 4 0 1    .0482759 
      1 "A" 5 1 1    .0504808
      1 "A" 6 0 0    .
      0 "A" 5 1 0    .
      0 "A" 2 0 0    .
      0 "A" 4 1 0    .
      0 "A" 6 1 0    .
      1 "A" 3 0 0    .
      0 "B" 1 0 1    .0615496
      1 "B" 2 1 1    .0625978
      1 "B" 3 1 1    .065769
      0 "B" 4 0 1    .0672783
      1 "B" 5 0 1    .0688099
      1 "B" 5 0 0    .
      1 "B" 3 0 0    .
      1 "B" 4 0 0    .
      0 "B" 2 0 0    .
      0 "B" 4 0 0    .
      0 "B" 3 0 0    .
      0 "B" 6 0 0    .
      0 "C" 2 1 1    .0803898
      0 "C" 3 0 1    .0847059
      0 "C" 4 0 1    .08519
      1 "C" 5 0 1    .0857561
      1 "C" 6 0 1    .0907184 
      0 "C" 4 1 0    .
      1 "C" 1 0 0    .
      0 "C" 2 1 0    .
      1 "C" 6 0 0    .
      0 "C" 1 1 0    .
      1 "C" 5 0 0    .
      0 "C" 1 1 0    .
      1 "C" 3 0 0    .
      end

      Comment


      • #4
        The problem arises because observations for policy 0 and for policy 1 are separate. So, the command

        Code:
         bys country region urban: egen m_health = mean(health) if policy==1 
        populates, as instructed, only observations for which policy == 1. So, those mean values are not copied to observations for which policy == 0. Stata doesn't know what you want in those observations, so the result is missing values. Missing values are replaced with missing values, hence the report of (0 changes made). The twist you want is that the mean for policy == 1 should also appear even if policy == 0. Here is one way to do it:
        Code:
        bys country region urban: egen m_health = mean(cond(policy == 1, health, .))
        See also https://journals.sagepub.com/doi/pdf...867X1101100210 Sections 9 and 10. It could also be done as a two-step
        Code:
        bys country region urban (policy): egen m_health = mean(health) if policy==1
        
        by country region urban: replace m_heath = m_health[_N]
        Last edited by Nick Cox; 01 Aug 2023, 03:02.

        Comment


        • #5
          Thank you Nick for excellent advice. Your code works well.

          Comment

          Working...
          X