Announcement

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

  • Doubts about conditional and missing values

    Hello everyone, I am a beginner level and I have questions about the missing values. In this example, I do not understand why in the condition of the third line it establishes that condition for the missing values, if already in the first line and according to the indications, the missing values were considered in level 1.

    "We wanted to split people into 3 achievement levels to best address each group's needs.
    Generate a variable that divides the students into 3 tracked groups of different levels (call the variable level), those below 25, those above 80 and those in the middle. Assign the missings to the lowest level just for the purposes of this exercise."

    gen level = 1 if math < 25 | math == .
    replace level = 2 if math >=25 & math < 80
    replace level = 3 if math >= 80 & math != .

    Thanks

  • #2
    Fiamma:
    welcome to this forum.
    The missing-related condition in the third line of code is actually redundant.
    Hence, your concern was correct.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      I am sorry to disagree with Carlo.

      If the third line of code has the missing-related condition removed and is replaced by
      Code:
      replace level = 3 if math >= 80
      it will change the level from 1 to 3 for any observation with a missing value for math, because to Stata, missing numeric values are greater than all other numbers.
      Code:
      . input math
      
                math
        1. 10
        2. 50
        3. 90
        4. .
        5. end
      
      . list if math < 25 | math == .  , clean noobs
      
          math  
            10  
             .  
      
      . list if math >=25 & math < 80  , clean noobs
      
          math  
            50  
      
      . list if math >= 80 & math != . , clean noobs
      
          math  
            90  
      
      . list if math >= 80             , clean noobs
      
          math  
            90  
             .
      Code:
      . generate level = 1 if math < 25 | math == .
      (2 missing values generated)
      
      . replace level = 2 if math >=25 & math < 80
      (1 real change made)
      
      . replace level = 3 if math >= 80 
      (2 real changes made)
      
      . list, clean noobs
      
          math   level  
            10       1  
            50       2  
            90       3  
             .       3
      Last edited by William Lisowski; 13 Jul 2018, 10:04.

      Comment


      • #4
        Carlos, unless I'm missing something, I think it is necessary. Without the missing condition in the third line, all missing values would be replaced with level=3 instead of the desired level=1.

        Consider the following data:


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float math
         5
        10
        15
        19
        20
        30
        30
        40
        60
        80
        88
        90
         .
         .
         .
        end

        And the following code:

        Code:
        *Original code
        gen level = 1 if math < 25 | math == .
        replace level = 2 if math >=25 & math < 80
        replace level = 3 if math >= 80 & math != .
        
        *INCORRECT
        gen level2 = 1 if math < 25 | math == .
        replace level2 = 2 if math >=25 & math < 80
        replace level2 = 3 if math >= 80
        
        *Alternative
        recode math (0/24=1) (.=1) (25/79=2) (80/max=3), gen(level3)
        
        *Alternative
        gen level4 = 1 if math < 25
        replace level4 = 2 if math >=25 & math < 80
        replace level4 = 3 if math >= 80
        replace level4 = 1 if math==.
        
        list
        Output:

        Code:
        . list
        
             +-----------------------------------------+
             | math   level   level2   level3   level4 |
             |-----------------------------------------|
          1. |    5       1        1        1        1 |
          2. |   10       1        1        1        1 |
          3. |   15       1        1        1        1 |
          4. |   19       1        1        1        1 |
          5. |   20       1        1        1        1 |
             |-----------------------------------------|
          6. |   30       2        2        2        2 |
          7. |   30       2        2        2        2 |
          8. |   40       2        2        2        2 |
          9. |   60       2        2        2        2 |
         10. |   80       3        3        3        3 |
             |-----------------------------------------|
         11. |   88       3        3        3        3 |
         12. |   90       3        3        3        3 |
         13. |    .       1        3        1        1 |
         14. |    .       1        3        1        1 |
         15. |    .       1        3        1        1 |
             +-----------------------------------------+
        Fiamma, Stata considers numeric missing values as very large numbers. Therefore, the code >=80 would include missing values. The replace command does not know or care what replace commands came before, and so does not take into account the previous missing condition.
        Last edited by Carole J. Wilson; 13 Jul 2018, 10:08. Reason: Crossed with William on this
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Dear All,
          You're right and I have to disgaree with myself.
          I tried to replicate the example under investigation but I forgot to include -math- variable (that I simòply read in the code of the original poster but did not include in my code) and went directly to -level-.
          I'm sorry for the confusion.
          Kind regards,
          Carlo
          (StataNow 18.5)

          Comment


          • #6
            Thanks to everyone !!!

            Comment

            Working...
            X