Announcement

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

  • Adding flag for imputed missing value

    In ILO data on unemployment, I am imputing missing values
    Code:
    * Impute unemployment  
    sort countrycode year  
    replace unemployment_wb = (unemployment_wb[_n-1] + unemployment_wb[_n+1])/2 if unemployment_wb == . & countrycode == countrycode[_n-1] & countrycode == countrycode[_n+1]  
    replace unemployment_wb = (unemployment_wb[_n-2] + unemployment_wb[_n+2])/2 if unemployment_wb == . & countrycode == countrycode[_n-2] & countrycode == countrycode[_n+2]  
    replace unemployment_wb = (unemployment_wb[_n-1] + unemployment_wb[_n+1])/2 if unemployment_wb == . & countrycode == countrycode[_n-1] & countrycode == countrycode[_n+1]  
    replace unemployment = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == . & countrycode == countrycode[_n-1] & countrycode == countrycode[_n+1]  
    replace unemployment = unemployment_wb if unemployment == . & unemployment_wb != .  
    replace unemployment = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == . & countrycode == countrycode[_n-1] & countrycode == countrycode[_n+1]   
    replace unemployment = (unemployment[_n-2] + unemployment[_n+2])/2 if unemployment == . & countrycode == countrycode[_n-2] & countrycode == countrycode[_n+2]  
    replace unemployment = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == . & countrycode == countrycode[_n-1] & countrycode == countrycode[_n+1]
    Whenever I impute a missing value, I want to add a flag...let's say I have a flag column (boolean), which should be TRUE for every imputed missing value. How could I go about achieving that?



    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

  • #2
    In the code below I cleaned up your imputation code by using the bysort : and by: prefixes and created the flags (in Stata 1 is true and 0 is false)

    Code:
     gen byte imp_wb = (unemployment_wb == .)
    * Impute unemployment  
    bysort countrycode (year): replace unemployment_wb = (unemployment_wb[_n-1] + unemployment_wb[_n+1])/2 if unemployment_wb == .  
    by     countrycode       : replace unemployment_wb = (unemployment_wb[_n-2] + unemployment_wb[_n+2])/2 if unemployment_wb == .  
    by     countrycode       : replace unemployment_wb = (unemployment_wb[_n-1] + unemployment_wb[_n+1])/2 if unemployment_wb == .  
    replace imp_wb = 0 if unemployment_wb == .
    gen byte imp = (unemployment == .)  
    by     countrycode       : replace unemployment    = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == .  
    by     countrycode       : replace unemployment    = unemployment_wb if unemployment == . & unemployment_wb != .  
    by     countrycode       : replace unemployment    = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == .  
    by     countrycode       : replace unemployment    = (unemployment[_n-2] + unemployment[_n+2])/2 if unemployment == .  
    by     countrycode       : replace unemployment    = (unemployment[_n-1] + unemployment[_n+1])/2 if unemployment == .  
    replace imp = 0 if unemployment == .
    Last edited by Maarten Buis; 26 Mar 2019, 03:39.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X