Calculating percentile differences from ordered categorical and continuous variables

Jorge Cimentada

2017-09-14

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(tidyverse)
library(haven)

Let’s prepare the data.

temp_file <- tempfile(fileext = ".zip")
download.file("http://gss.norc.org/Documents/stata/2016_stata.zip", temp_file)
unzip(temp_file, exdir = tempdir())

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

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

unlink(c(temp_file, data_link)) # deleting both files.

gss
## # A tibble: 2,867 x 3
##       income  prestige        wt
##    <dbl+lbl> <dbl+lbl> <dbl+lbl>
##  1        NA        60 0.9569935
##  2        12        40 0.4784968
##  3        NA        38 0.9569935
##  4         9        35 1.9139870
##  5         2        31 1.4354903
##  6        NA        36 0.9569935
##  7        NA        65 1.4354903
##  8        10        48 0.9569935
##  9        11        48 0.9569935
## 10        NA        35 0.9569935
## # ... 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.775162   1.028419

We can play around with other percentiles

perc_diff(gss, income, prestige, percentiles = c(50, 10))
## difference         se 
##   2.355837   2.374071

And we can add a vector of weights

perc_diff(gss, income, prestige, weights = wt)
## difference         se 
## 15.2582596  0.9722601

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..

*--------
use "/Users/cimentadaj/Downloads/gss_income.dta", clear

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 inc`i' [aw=wt]
    loc per`i' = r(mean)
                                    
    qui sum prestige if inc`i'==1 
                            
    if `r(N)'>0 {
        qui regress prestige if inc`i'==1 [aw=wt]
        post `memhold' (`i') (_b[_cons]) (_se[_cons]) (`per`i'')
                            
    }               
}
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 diff`hi_p'`lo_p' = r(estimate)
    loc se`hi_p'`lo_p' = r(se)
    
    di "`hi_p'-`lo_p' gap:     `diff`hi_p'`lo_p''"
    di "se(`hi_p'-`lo_p' gap): `se`hi_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.2582596  0.9722601

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.1274451 0.1756691
## 2          2 0.2485077 0.3389754
## 3          3 0.3634337 0.4902497
## 4          4 0.4724689 0.6298252
## 5          5 0.5758592 0.7580375
## 6          6 0.6738505 0.8752246

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.25826 0.006003805

compared to

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

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.