# [R] Percentiles/Quantiles with Weighting

David Winsemius dwinsemius at comcast.net
Tue Feb 17 18:22:45 CET 2009

```I do know that Harrell's Quantile function in the Hmisc package will
allow quantile estimates from models. Whether it is general enough to
extend to time series, I have no experience and cannot say.

--
David Winsemius

On Feb 17, 2009, at 11:57 AM, Brigid Mooney wrote:

> Hi All,
>
> I am looking at applications of percentiles to time sequenced data.
> 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
> recent
> data more heavily.
>
> I wrote the following function, but it seems quite inefficient, and
> not
> really very flexible in its applications - so if anyone has any
> suggestions
> 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("Percentiles:")
>  print(quantile(X, pctile))
>  print("Weighted:")
>  print(Xprime)
>  print("Weighted Percentiles:")
>  print(quantile(Xprime, pctile, na.rm=TRUE))
> }
>
> WtPercentile(1:10)
> WtPercentile(rnorm(10))
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help