2.0

2.0 | 1 rating Rate this file 49 Downloads (last 30 days) File Size: 2.67 KB File ID: #32115
image thumbnail

quantreg.m - quantile regression

by Aslak Grinsted

 

07 Jul 2011 (Updated 08 Jul 2011)

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 Statistics Toolbox
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
07 Jul 2011 AS

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.

08 Jul 2011 Aslak Grinsted

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

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

08 Aug 2011 Aslak Grinsted

@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);

17 Apr 2012 Satis

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

Please login to add a comment or rating.
Updates
08 Jul 2011

Fixed a few issues with input parameter parsing.

08 Jul 2011

Fixed another small bug.

Tag Activity for this File
Tag Applied By Date/Time
statistics Aslak Grinsted 07 Jul 2011 12:01:55
quantile Aslak Grinsted 07 Jul 2011 12:01:55
linear Aslak Grinsted 07 Jul 2011 12:01:56
multiple Aslak Grinsted 07 Jul 2011 12:01:56
median Aslak Grinsted 07 Jul 2011 12:01:56
regression Aslak Grinsted 07 Jul 2011 12:01:56
robust fitting Aslak Grinsted 07 Jul 2011 12:01:56
quadratic Aslak Grinsted 07 Jul 2011 12:01:56
robustfit Aslak Grinsted 07 Jul 2011 12:01:56
percentile Aslak Grinsted 08 Jul 2011 18:32:46
polynomial Aslak Grinsted 11 Jul 2011 02:24:41
fitting Aslak Grinsted 11 Jul 2011 02:24:45
fit Aslak Grinsted 11 Jul 2011 02:24:49
percentile Aslak Grinsted 11 Jul 2011 02:26:33

Contact us at files@mathworks.com