4.0
4.0 | 3 ratings Rate this file 79 Downloads (last 30 days) File Size: 3.94 KB File ID: #32115
image thumbnail

quantreg(x,y,tau,order,Nboot)

by

Aslak Grinsted (view profile)

 

07 Jul 2011 (Updated )

Quantile regression with bootstrapping confidence intervals

| Watch this File

File Information
Description

Quantile Regression

  USAGE: [p,stats]=quantreg(x,y,tau[,order,nboot]);
  
  INPUTS:
    x,y: data that is fitted. (x and y should be columns)
         Note: that if x is a matrix with several columns then multiple
         linear regression is used and the "order" argument is not used.
    tau: quantile used in regression.
    order: polynomial order. (default=1)
    nboot: number of bootstrap surrogates used in statistical inference.(default=200)
 
  stats is a structure with the following fields:
       .pse: standard error on p. (not independent)
       .pboot: the bootstrapped polynomial coefficients.
       .yfitci: 95% confidence interval on polyval(p,x)
 
 
  Note: uses bootstrap on residuals for statistical inference. (see help bootstrp)
  check also: http://www.econ.uiuc.edu/~roger/research/intro/rq.pdf
 
  EXAMPLE:
  x=(1:1000)';
  y=randn(size(x)).*(1+x/300)+(x/300).^2;
  [p,stats]=quantreg(x,y,.9,2);
  plot(x,y,x,polyval(p,x),x,stats.yfitci,'k:')
  legend('data','2nd order 90th percentile fit','95% confidence interval','location','best')
  
  For references on the method check e.g. and refs therein:
  http://www.econ.uiuc.edu/~roger/research/rq/QRJEP.pdf
 
   Copyright (C) 2008, Aslak Grinsted

Required Products Optimization Toolbox
Statistics Toolbox
MATLAB release MATLAB 7 (R14)
MATLAB Search Path
/
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (10)
16 Mar 2015 Aslak Grinsted

Aslak Grinsted (view profile)

@SimeonYurek: thank you for the suggestion. It has been implemented.

Comment only
13 Aug 2014 wei

wei (view profile)

hi. just a quick question. How do we calculate the goodness of fit for quantile regression? thanks

20 May 2014 Simeon Yurek

Very nice code for Koenker and Hallock (2001). Thanks for posting.

One question: in your statement of the function rho (line 85), when r >= 0 (all positive residuals above x*p), does the function reduce to abs(r), and thus is not weighted by tau? It's true that [r - 0.*r/tau] = r.*tau, but is the tau lost (being multiplied by zero)? Could you have stated instead:

rho=@(r)sum(abs(r).*abs(tau-(r<0)))

and this way weight both over and under residuals? The stats for the bootstrap are slightly less robust but not by much. Please let me know if I'm off.

Antony: help fminsearch. 'MaxFunEvals' and 'MaxIter' can be defined as options.

Comment only
22 Jan 2014 Antony

Antony (view profile)

Hi, first of all, thank you very much for the code.

When I run the code I get the following message:

"Exiting: Maximum number of function evaluations has been exceeded
- increase MaxFunEvals option.
Current function value: 3183.464509 "

I get this message a number of times with just the current function value changing.
While it is not an error message and I still obtain the estimations I need, I was wondering if this had any influence on the validity of my results.

Thank you in advance for anyone who can help me out here.

Comment only
21 Aug 2012 Wolfgang Schwanghart

Excellent. Well written help and code! Runs as advertised!

17 Apr 2012 Satis

Satis (view profile)

If X and y have some missing observations, then

[p,stats]=quantreg(X,y,.9); does not work. I got error message.

Could you please suggest me how to resolve this problem??

Comment only
08 Aug 2011 Aslak Grinsted

Aslak Grinsted (view profile)

@mohammad: The code does do MLR. Here's an example with multiple predictors:

X=randn(100,4);
y=X*[4 2 4 1]'+randn(100,1)*.2;
[p,stats]=quantreg(X,y,.9);

Comment only
07 Aug 2011 Mohammad

In fact I wanted to run this M-file for multi column X, but I got error, so it is not implement for MLR? if it is , could you please put an example? because when I do not mention Order , I get error when i mention it is not MLR

Comment only
08 Jul 2011 Aslak Grinsted

Aslak Grinsted (view profile)

Thanks for the comment, and suggestions for improvement. I've just uploaded a new version (it should be online shortly).

Comment only
07 Jul 2011 AS

AS (view profile)

One unexpected thing with this code. Suppose I have a Y vector and want to regress it on one explanatory variable, but also include a constant in my regression. Then my X matrix has 2 columns, the first column is just ones, the second is the explanatory variable. It should still be possible to plot this along with the results from the regression.

Currently, however, if you run:
quantreg([ones(length(x),1) x], y,.5)
You get an error because it tries to plot it, but order has been set (line 44) to [], so things get messed up.

Additionally, the plots that do get produced look odd because the default is to draw lines between all of the points, which usually isn't what you want. For example, this doesn't look like what it should:
x=randn(1000,1);
y=1+5*x+randn(1000,1);
quantreg(x,y,0.5)

Finally, there's a problem with the error checking of inputs (lines 41-47) because it's all one big if-else statement. If you only put in three inputs, then line 44 runs and order gets set to [], but then the program exits the if-else statement and so Nboot never gets set correctly. You should split these up into separate if statements because if there are only 3 inputs then you need to set both order and Nboot.

Updates
08 Jul 2011

Fixed a few issues with input parameter parsing.

08 Jul 2011

Fixed another small bug.

16 Mar 2015

implemented suggested change from Simeon Yurek in a FEX comment

Contact us