***************************************************************************************************;
#delimit;
** define the program that calculates the Pendakur, Pendakur and Woodcock (2008) Representation Index;
**    cite Pendakur, Krishna, Ravi Pendakur and Simon D. Woodcock, 2008, A Representation Index:        ;
**         Measuring the Representation of Minorities in the Income Distribution, Berkeley Electronic   ;
**         Journal of Economic Analysis and Policy: ADVANCES.  						;
** this program uses UNIQUE;
** -- VARLIST is dependent variable followed by covariates;
** -- can use weighted data with AWEIGHTS;
** -- QTILE selects the quantile, and ABOVE specifies above measures (with below measures as default);
** -- GROUPS is a single variable taking a different value for each group;
** -- ANCHOR is a scalar specifying the group number of the anchoring distribution;
** --   (0 indicates the population as the anchoring distribution);
program define representation, sortpreserve byable(recall);
version 10;
syntax varlist [aweight] [if] [in], [qtile(real 0.5)] Groups(varlist numeric) [above] [anchor (integer 0)];
    marksample touse;
    preserve;
    tokenize `varlist';
    local depvar `1';
        mac shift;
        local xvar `*';
    unique `groups' if `touse';
    scalar ng=r(sum);
    di "representation index will be computed on " ng " groups using group " `anchor' " as the anchor";
    if "`above'"=="above" di "calculating representation ABOVE " `qtile';
    else di "calculating representation BELOW " `qtile' " quantile";
    matrix rep = J(ng,1,0);     * this holds representation;
    * get point estimates;
    di "estimating representation index ... ";
    g anchor_group=`groups';
    if `anchor'==0 {;
	replace anchor_group=0;
    };
    quietly qreg `varlist' [`weight'`exp'] if ((`touse')&(anchor_group==`anchor')), q(`qtile');
    quietly predict yhat ;
    if "`above'"=="above" {;
        quietly g y_under_yhat=`depvar'>yhat if ((`touse')&(`depvar'~=.)&(yhat~=.));
    };
    else {;
        quietly g y_under_yhat=`depvar'<=yhat if ((`touse')&(`depvar'~=.)&(yhat~=.));
    };
    local gg=1;
    di "representation of the following group numbers";
    levelsof `groups', local(levels);
    di "using anchor group " `anchor';
    foreach g of local levels {;
        quietly summ y_under_yhat if ((`touse') & (`groups'==`g')&(`depvar'~=.)&(yhat~=.)) [`weight'`exp'];
        matrix rep[`gg',1] = r(mean);
        local gg = `gg' + 1;
    };
    drop yhat y_under_yhat anchor_group;
    matrix rep=rep';
    matrix list rep;
restore;
end;