# Calculating percentile differences from ordered categorical and continuous variables

#### 2018-03-30

Reardon (2011) introduced a very interesting concept in which he calculates percentile differences from ordered categorical variables. He explains his procedure very much in detail in the appendix of the book chapter but no formal implementation has been yet available on the web. With this package I introduce a function that applies the procedure, following a step-by-step Stata script that Sean Reardon kindly sent me.

In this vignette I show you how to use the function and match the results to the Stata code provided by Reardon himself.

We’ll be using a real world data set, the General Social Survey, that surveys American citizens on attitudes, behaviors and attributes. For this example we’ll need the packages below.

library(dplyr)
library(ggplot2)
library(haven)

Let’s prepare the data.

temp_file <- tempfile(fileext = ".zip")
unzip(temp_file, exdir = tempdir())

data_link <- list.files(tempdir(), full.names = TRUE, pattern = "*.DTA")

gss <-
select(rincome, prestg10, wtss) %>%
rename(
income = rincome,
prestige = prestg10,
wt = wtss
)

gss
## # A tibble: 2,867 x 3
##    income    prestige  wt
##    <dbl+lbl> <dbl+lbl> <dbl+lbl>
##  1 <NA>      60        0.956993511490439
##  2 12        40        0.478496755745219
##  3 <NA>      38        0.956993511490439
##  4 9         35        1.91398702298088
##  5 2         31        1.43549026723566
##  6 <NA>      36        0.956993511490439
##  7 <NA>      65        1.43549026723566
##  8 10        48        0.956993511490439
##  9 11        48        0.956993511490439
## 10 <NA>      35        0.956993511490439
## # ... with 2,857 more rows

This is the minimum dataset that the function will accept. This means that it needs to have at least one categorical variable (rincome) and a continuous variable (prestg10) (the vector of weights is optional). The first variable is the typical income variable but asked in income brackets rather than in a continuous fashion. The second one measures the prestige of the respondents occupation.

The package is called perccalc, short for percentile calculator and we can install and load it with this code:

devtools::install_github("cimentadaj/perccalc")
library(perccalc)

The package only has two functions called perc_diff and perc_dist. They’re very easy to use, we just specify the data, the name of the categorical and continuous variable and the percentile difference we want.

Let’s put it to use!

perc_diff(gss, income, prestige, percentiles = c(90, 10))
## Error: is_ordered_fct is not TRUE

I generated that error on purpose to raise a very important requirement of the function: the categorical variable needs to be an ordered factor. This is very important because otherwise we could be calculating percentile differences from categorical variables which have no apparent order such as married, single and widowed.

We can turn the previous into an ordered factor with the code below.

gss <-
gss %>%
mutate(income = factor(income,
ordered = TRUE))

Now it’ll work.

perc_diff(gss, income, prestige, percentiles = c(90, 10))
## difference         se
##  13.774772   1.022807

We can play around with other percentiles

perc_diff(gss, income, prestige, percentiles = c(50, 10))
## difference         se
##   2.351532   2.358102

And we can add a vector of weights

perc_diff(gss, income, prestige, weights = wt)
## difference         se
## 15.2556667  0.9686197

Now, how are we sure that these estimates are as accurate as the Reardon (2011) implementation? We can compare the Stata output using this data set.

# Saving the dataset to a path
gss %>%
write_dta(path = "/Users/cimentadaj/Downloads/gss_income.dta", version = 13)

Running the code below using the gss_income.dta..

*--------

drop if missing(income)
drop if missing(prestige)

tab income, gen(inc)
*--------

/*-----------------------
Making a data set that has
one observation per income category
and has mean and se(mean) in each category
and percent of population in the category
------------------------*/

tempname memhold
tempfile results
postfile memhold' income mean se_mean per using results'

forv i = 1/12 {
sum inci' [aw=wt]
loc peri' = r(mean)

qui sum prestige if inci'==1

if r(N)'>0 {
qui regress prestige if inci'==1 [aw=wt]
post memhold' (i') (_b[_cons]) (_se[_cons]) (peri'')

}
}
postclose memhold'

/*-----------------------
Making income categories
into percentiles
------------------------*/

use results', clear

sort income
gen cathi = sum(per)
gen catlo = cathi[_n-1]
replace catlo = 0 if income==1
gen catmid = (catlo+cathi)/2

/*-----------------------
Calculate income
achievement gaps
------------------------*/

sort income

g x1 = catmid
g x2 = catmid^2 + ((cathi-catlo)^2)/12
g x3 = catmid^3 + ((cathi-catlo)^2)/4

g cimnhi = mean + 1.96*se_mean
g cimnlo = mean - 1.96*se_mean

reg mean x1 x2 x3 [aw=1/se_mean^2]

twoway (rcap cimnhi cimnlo catmid) (scatter mean catmid) ///
(function y = _b[_cons] + _b[x1]*x + _b[x2]*x^2 + _b[x3]*x^3, ran(0 1))

loc hi_p = 90
loc lo_p = 10

loc d1 = [hi_p' - lo_p']/100
loc d2 = [(hi_p')^2 - (lo_p')^2]/(100^2)
loc d3 = [(hi_p')^3 - (lo_p')^3]/(100^3)

lincom d1'*x1 + d2'*x2 + d3'*x3
loc diffhi_p'lo_p' = r(estimate)
loc sehi_p'lo_p' = r(se)

di "hi_p'-lo_p' gap:     diffhi_p'lo_p''"
di "se(hi_p'-lo_p' gap): sehi_p'lo_p''"

I get that the 90/10 difference is 15.25 with a standard error of .972. Does it sound familiar?

perc_diff(gss, income, prestige, weights = wt)
## difference         se
## 15.2556667  0.9686197

Finally, there’s perc_dist that shares a lot of similarity with perc_diff. It calculates the estimate for every percentile. You can use this to perform your own analysis. For example,

perc_dist(gss, income, prestige) %>%
head()
##   percentile  estimate std.error
## 1          1 0.1273929 0.1749636
## 2          2 0.2483978 0.3376261
## 3          3 0.3632609 0.4883166
## 4          4 0.4722282 0.6273666
## 5          5 0.5755458 0.7551097
## 6          6 0.6734598 0.8718822

We can also add the optional set of weights and graph it in a more convenient way.

gss %>%
perc_dist(income, prestige, wt) %>%
mutate(ci_low = estimate - 1.96 * std.error,
ci_hi = estimate + 1.96 * std.error) %>%
ggplot(aes(percentile, estimate)) +
geom_point() +
geom_errorbar(aes(ymin = ci_low, ymax = ci_hi))

Please note that for calculating the difference between two percentiles it is more accurate to use the perc_diff function. The perc_diff function calculates the difference through a linear combination of coefficients.

For example:

perc_dist(gss, income, prestige, wt) %>%
filter(percentile %in% c(90, 10)) %>%
summarize(diff = diff(estimate),
se_diff = diff(std.error))
##       diff     se_diff
## 1 15.25567 0.009851745

compared to

perc_diff(gss, income, prestige, weights = wt, percentiles = c(90, 10))
## difference         se
## 15.2556667  0.9686197

Note how the coefficients are the same but the standard error is different.

I hope this was a convincing example, I know this will be useful for me. All the intellectual ideas come from Sean Reardon, as well as the Stata code. The R implementation is my own work.

• Reardon, Sean F. “The widening academic achievement gap between the rich and the poor: New evidence and possible explanations.” Whither opportunity (2011): 91-116.