[R] Percentiles/Quantiles with Weighting
rkoenker at uiuc.edu
Tue Feb 17 21:13:38 CET 2009
url: www.econ.uiuc.edu/~roger Roger Koenker
email rkoenker at uiuc.edu Department of Economics
vox: 217-333-4558 University of Illinois
fax: 217-244-6678 Champaign, IL 61820
On Feb 17, 2009, at 1:58 PM, Brigid Mooney wrote:
> Thanks for pointing me to the quantreg package as a resource. I was
> hoping to ask be able to address one quick follow-up question...
> I get slightly different variants between using the rq funciton with
> formula = mydata ~ 1 as I would if I ran the same data using the
> quantile function.
> mydata <- (1:10)^2/2
> pctile <- seq(.59, .99, .1)
> quantile(mydata, pctile)
> 59% 69% 79% 89% 99%
> 20.015 26.075 32.935 40.595 49.145
> rq(mydata~1, tau=pctile)
> rq(formula = mydata ~ 1, tau = pctile)
> tau= 0.59 tau= 0.69 tau= 0.79 tau= 0.89 tau= 0.99
> (Intercept) 18 24.5 32 40.5 50
> Degrees of freedom: 10 total; 9 residual
> Is it correct to assume this is due to the different accepted
> methods of calculating quantiles? If so, do you know where I would
> be able to see the algorithms used in these functions? I'm not
> finding it in the documentation for function rq, and am new enough
> to R that I don't know where those references would generally be.
Yes, quantile() in base R documents 9 varieties of quantiles, 2 more
William Empson's famous 7 Types of Ambiguity. In quantreg the function
rq() finds a solution to an underlying optimization problem and
any further into the nature of the ambiguity -- it does often produce a
warning indicating that there may be more than one solution. The
base R quantile is interpolated, while the default rq() with method =
using the simplex algorithm finds an order statistic, typically. If
something more like interpolation, you can try rq() with method = "fn"
which is using an interior point algorithm and when there are multiple
solutions it tends to produce something more like the centroid of the
solution set. I hope that this helps.
> On Tue, Feb 17, 2009 at 12:29 PM, roger koenker <rkoenker at uiuc.edu>
> gives one possibility...
> url: www.econ.uiuc.edu/~roger Roger Koenker
> email rkoenker at uiuc.edu Department of Economics
> vox: 217-333-4558 University of Illinois
> fax: 217-244-6678 Champaign, IL 61820
> On Feb 17, 2009, at 10:57 AM, Brigid Mooney wrote:
> Hi All,
> I am looking at applications of percentiles to time sequenced data.
> I had
> just been using the quantile function to get percentiles over various
> periods, but am more interested in if there is an accepted (and/or
> R-implemented) method to apply weighting to the data so as to weigh
> data more heavily.
> I wrote the following function, but it seems quite inefficient, and
> really very flexible in its applications - so if anyone has any
> on how to look at quantiles/percentiles within R while also using a
> weighting schema, I would be very interested.
> Note - this function supposes the data in X is time-sequenced, with
> the most
> recent (and thus heaviest weighted) data at the end of the vector
> WtPercentile <- function(X=rnorm(100), pctile=seq(.1,1,.1))
> Xprime <- NA
> for(i in 1:length(X))
> Xprime <- c(Xprime, rep(X[i], times=i))
> print(quantile(X, pctile))
> print("Weighted Percentiles:")
> print(quantile(Xprime, pctile, na.rm=TRUE))
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help