/* cls cd "C:/Users/sttp/Documents/nhb/STATA/StataHacks/docs/pseudo_percentiles" do2md using pseudo_percentiles, css("../../css/nhb.css") //show */ //OFF cls clear cd "C:/Users/sttp/Documents/nhb/STATA/StataHacks/docs/pseudo_percentiles" //ON /*** --- title: Pseudo percentiles ... # Pseudo percentiles ## Introduction It has been hard to report centiles, eg. the median on data from Statistics Denmark. The reason is that a calculated must be based on at least 5 observations. So a mean of 5 values is ok. But a median might be based on a single values or a linear combination of two values and hence it must not be reported. One solution to this reporting problem is to use pseudo centiles. Similar requirements might be present other places than Statistics Denmark. In this document pseudo percentiles are defined. Pseudo percentiles are implemented in Stata commands [-sumat-](http://www.bruunisejs.dk/StataHacks/My%20commands/basetable/basetable_demo/) and [-basetable-](http://www.bruunisejs.dk/StataHacks/My%20commands/sumat/sumat_demo/). These commands are demonstrated below. **It is important to have newest versions of the commands from ssc.** ## How pseudo centiles are defined A variable *x* is generated to demonstrate the definition. ***/ /**/set seed 123 /**/set obs 15 /**/generate id = _n /**/generate x = runiformint(1,7) /*** First, a pseudo variable, *pseudo_x*, of *x* is generated as the moving average of five surrounding *x* values. ***/ /**/sort x /**/generate pseudo_x = (x[_n-2] + x[_n-1] + x[_n] + x[_n+1] + x[_n+2]) / 5 if inrange(_n, 3, _N-2) /**/format x pseudo_x %6.2f list /*** The calculation can be summarised as: ***/ forvalues start = 3/13 { /****/display "* Value `start' is: (`=x[`=`start'-2']' + `=x[`=`start'-1']' + `=x[`start']' + `=x[`=`start'+1']' + `=x[`=`start'+2']') / 5 = ", pseudo_x[`start'] } /*** In other words, every value of *pseudo_x* is a average of 5 surrounding *x* values. Hence, they are allowed to be reported from Statistics Denmark as single values or as e.g. centiles. The pseudo centiles are based on the variable *pseudo_x* and are calculated similar as described in Methods and formulas for Stata command -centile- as the default case with the difference that the number of observations (*n*) is that of variable of variable *x* (here 15). # A demonstration of pseudo percentiles using -sumat- Suppose minimum, maximum, lower and upper percentetiles, quartiles and deciles are wanted. Working outside Statistics Denmark one would simply do something like: ***/ sumat x, statistics(min p01 p10 p25 p50 p75 p90 p99 max) /*** To get pseudo percentiles based on a average of 5 one simply adds the option **hide(5)**: ***/ sumat x, statistics(min p01 p10 p25 p50 p75 p90 p99 max) hide(5) /*** It is seen that there might be small differences in values, but the levels will be right. **And pseudo percentiles will always be averages of (in this case 5) neighboring values.** # pseudo centiles in commands -sumat- and -basetable- An example on a proper dataset could be using ``` webuse lbw, clear ``` ***/ //OFF use lbw, clear //ON /****/metadata, style(html) title(Meta data for the lbw dataset.) /*** ## Using -sumat- A summary of quartiles by mother smoking during pregnanacy and race To get the real quartiles one could do: ***/ sumat bwt, statistics(median iqi) decimals(1) rowby(smoke) colby(race) total label title(The real quartiles) /*** And reporting pseudo percentiles (note only difference from above is option **hide(5)**): ***/ sumat bwt, statistics(median iqi) decimals(1) rowby(smoke) colby(race) total label title(Quartiles based on pseudo percentiles) hide(5) /*** ## Pseudo percentiles in -basetable- One regular use of -basetable- ***/ basetable smoke bwt(%6.1f, iqi), title(The real quartiles) /*** The command -basetable- also handles pseudo percentiles. But here there are two options required: * **ps**eudo: indicating that pseudo percentiles should be used * **sm**all: specifying number of values to use in the moving average. Default is 5. Reporting pseudo percentiles (average of 5) is done by: ***/ basetable smoke bwt(%6.1f, iqi) bwt(%6.1f, idi) bwt(%6.1f, imi), title(Using pseudo percentiles) pseudo