In [1]:
options(digits=2)
# R Imports Boilerplate
rm(list=ls())
In [2]:
library(LalRUtils)
load_or_install(c('tidyverse','data.table', 'stargazer','lfe',
  'magrittr','AER','quantreg'))
theme_set(theme_bw())

exp_type = 'text'
  1. TRUE
  2. TRUE
  3. TRUE
  4. TRUE
  5. TRUE
  6. TRUE
  7. TRUE

7. Quantile regression

based on @KleiberAppliedeconometrics2008

Least-squares models the conditional mean of a response, but one may be interested in treatment effects at different quantiles of the conditional distribution of Y. The Quantile regression model is given by

$$ Q_y(\tau|x) = x_i' \beta_\tau $$

where $Q_y(\tau|x)$ denotes the $\tau$-quantile of $y$ conditional on $x$.

The conditional quantile function at quantile $\tau$ is defined as

$$ Q_\tau(Y_i | X_i) = F_y^{-1} (\tau|X_i) $$

CQF solves the following problem:

$$ Q_\tau (Y_i | X_i) = argmin_{q(x)} E [ \rho_\tau (Y_i - q(X_i))] $$

where $\rho_\tau(u)$ is the check function, which weights positive and negative terms asymmetrically (unless $\tau=0.5$, in which case it collapses to a Least-Absolute-Deviation function). $$ \rho_\tau(u) = (\tau - 1(u \leq 0))u = 1(u > 0) \tau |u| + 1(u\leq 0) (1-\tau)|u| $$

In [3]:
tau_25 = function(x) (x>0) * 0.25 * abs(x) + (x<=0) * (1 - 0.25) * abs(x)
tau_50 = function(x) (x>0) * 0.50 * abs(x) + (x<=0) * (1 - 0.50) * abs(x)
tau_75 = function(x) (x>0) * 0.75 * abs(x) + (x<=0) * (1 - 0.75) * abs(x)

# dummy dataset
p <- ggplot(data = data.frame(x = 0), mapping = aes(x = x))

# plot functions
p +
   stat_function(fun = tau_25, aes(colour='tau_25')) +
   stat_function(fun = tau_50, aes(colour='tau_50')) +
   stat_function(fun = tau_75, aes(colour='tau_75')) +
   xlim(-10,10)+
   labs(title='Check functions')
In [4]:
data('CPS1988')

cps_f <- log(wage) ~ experience + I(experience^2) + education
# models
cps_lm <- lm(cps_f, data= CPS1988)
cps_lad <- rq(cps_f, data= CPS1988)
cps_p25 <-  rq(cps_f, tau = 0.25, data= CPS1988)
cps_p75 <-  rq(cps_f, tau = 0.75, data= CPS1988)
In [5]:
stargazer(cps_lm, cps_lad, cps_p25, cps_p75, type=exp_type, df = F)
==============================================================
                               Dependent variable:            
                    ------------------------------------------
                                    log(wage)                 
                        OLS                quantile           
                                          regression          
                        (1)         (2)       (3)       (4)   
--------------------------------------------------------------
experience            0.078***   0.077***  0.092***  0.064*** 
                      (0.001)     (0.001)   (0.002)   (0.001) 
                                                              
I(experience2)       -0.001***   -0.001*** -0.002*** -0.001***
                     (0.00002)   (0.00003) (0.00004) (0.00002)
                                                              
education             0.087***   0.094***  0.093***  0.094*** 
                      (0.001)     (0.001)   (0.002)   (0.001) 
                                                              
Constant              4.300***   4.200***  3.800***  4.700*** 
                      (0.019)     (0.022)   (0.029)   (0.020) 
                                                              
--------------------------------------------------------------
Observations           28,155     28,155    28,155    28,155  
R2                     0.330                                  
Adjusted R2            0.330                                  
Residual Std. Error    0.590                                  
F Statistic         4,546.000***                              
==============================================================
Note:                              *p<0.1; **p<0.05; ***p<0.01

Variation in coefficients as a function of $\tau$

In [6]:
#%%
cps_rqbig <- rq(cps_f, tau = seq(0.05, 0.95, by = 0.05),
  data = CPS1988)
#%%
cps_rqbigs <- summary(cps_rqbig)
plot(cps_rqbig)
Warning message in summary.rq(xi, U = U, ...):
“18 non-positive fis”
In [ ]: