options(digits=2)
# R Imports Boilerplate
rm(list=ls())
library(LalRUtils)
load_or_install(c('tidyverse','data.table', 'stargazer','lfe',
'magrittr','AER','quantreg'))
theme_set(theme_bw())
exp_type = 'text'
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| $$
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')
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)
stargazer(cps_lm, cps_lad, cps_p25, cps_p75, type=exp_type, df = F)
Variation in coefficients as a function of $\tau$
#%%
cps_rqbig <- rq(cps_f, tau = seq(0.05, 0.95, by = 0.05),
data = CPS1988)
#%%
cps_rqbigs <- summary(cps_rqbig)
plot(cps_rqbig)