Announcement

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

  • Graph bar for lots of binary variables

    Hello, I think my question is not hard to answer but I can´t figure it out.

    I have a dataset of multiple indicators for 31 countries. There are a lot of missing values since in some places is hard to colect some information and not all the countries have all the indicators (variables) in the dataset. I want to check the number of missing and non missing values of each indicator and show it in one single graph bar. So far, I generated a new binary level for each indicator depending on the availability of the data (0=not available, 1=available). Now I don´t know how to graph the nuimber of available observations for each variable in a single figure.

    Any help is much appreciated, thank you.

  • #2
    This example requires labmask from the Stata Journal. You can findit by typing in Stata findit labmask.

    I had to create an example dataset for you. That is work for me, and there is a risk that my example data does not match yours. As is discussed in the Statalist FAQ (there is a link at the top of your screen), it is better if you include an extract of your data with dataex.

    Code:
    //---------------------------------------------------------- create example data
    clear all
    set obs 31
    gen cntry = _n
    label define cntry_lab ///
    1  "NLD" ///
    2  "NOR" ///
    3  "NPL" ///
    4  "NRU" ///
    5  "NZL" ///
    6  "OMN" ///
    7  "PAK" ///
    8  "PAN" ///
    9  "PCN" ///
    10 "PER" ///
    11 "PHL" ///
    12 "PLW" ///
    13 "PNG" ///
    14 "POL" ///
    15 "PRI" ///
    16 "PRK" ///
    17 "PRT" ///
    18 "PRY" ///
    19 "PSE" ///
    20 "PYF" ///
    21 "QAT" ///
    22 "REU" ///
    23 "ROU" ///
    24 "RUS" ///
    25 "RWA" ///
    26 "SAU" ///
    27 "SDN" ///
    28 "SEN" ///
    29 "SGP" ///
    30 "SGS" ///
    31 "SHN"
    label value cntry cntry_lab
    
    forvalues i = 1/10 {
        // create a variable x1, x2, ... , x10
        // containing a draw from a standard normal distribution
        // but a 10% change of having a missing value
        gen x`i' = rnormal() if runiform() < .9
    }
    
    //------------------------------- count number of missings per country and graph
    egen nmiss = rowmiss(x*)
    // basic graph
    twoway bar nmiss cntry
    
    // a more fancy graph
    gen nnmiss = - nmiss
    sort nnmiss cntry
    gen order = _n
    drop nnmiss
    
    labmask order, values(cntry) decode
    
    twoway bar nmiss order, ylab(1/31,val) horizontal xlab(0/3) ytitle(country)
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X