Compute quantiles with values and correspondent frequencies

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Compute quantiles with values and correspondent frequencies

Paolo Radaelli
Dear List,
quantile(x) function allows to obtain specified quantiles of a vector of observations x.

Is there an analogous function to compute quantiles in the case one have the vector of the observations x and the correspondent vector f of relative frequencies ?

Thank you

Paolo Radaelli



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Compute quantiles with values and correspondent frequencies

Roger Bivand
On Fri, 3 Mar 2006, Paolo Radaelli wrote:

> Dear List, quantile(x) function allows to obtain specified quantiles of
> a vector of observations x.
>
> Is there an analogous function to compute quantiles in the case one have
> the vector of the observations x and the correspondent vector f of
> relative frequencies ?

Just use rep():

x <- rpois(100, 4) # data
quantile(x)
tx <- table(x) # frequencies (need to be integer counts)
tx
v <- as.numeric(names(tx)) # values
quantile(rep(v, as.integer(tx)))

You have v and tx, so this should work.

>
> Thank you
>
> Paolo Radaelli
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Roger Bivand
Department of Economics
NHH Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: Compute quantiles with values and correspondent frequencies

Paolo Radaelli
Yes, but it works only when you have integer counts for each value.
When I have only relative frequencies I can't repeat each value a
non-integer number of times.
Paolo

----- Original Message -----
From: "Roger Bivand" <[hidden email]>
To: "Paolo Radaelli" <[hidden email]>
Cc: <[hidden email]>
Sent: Friday, March 03, 2006 1:18 PM
Subject: Re: [R] Compute quantiles with values and correspondent frequencies


> On Fri, 3 Mar 2006, Paolo Radaelli wrote:
>
> > Dear List, quantile(x) function allows to obtain specified quantiles of
> > a vector of observations x.
> >
> > Is there an analogous function to compute quantiles in the case one have
> > the vector of the observations x and the correspondent vector f of
> > relative frequencies ?
>
> Just use rep():
>
> x <- rpois(100, 4) # data
> quantile(x)
> tx <- table(x) # frequencies (need to be integer counts)
> tx
> v <- as.numeric(names(tx)) # values
> quantile(rep(v, as.integer(tx)))
>
> You have v and tx, so this should work.
>
> >
> > Thank you
> >
> > Paolo Radaelli
> >
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
> >
>
> --
> Roger Bivand
> Economic Geography Section, Department of Economics, Norwegian School of
> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> e-mail: [hidden email]
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Compute quantiles with values and correspondent frequencies

Sean Davis
How about this:

> x <- rpois(100,4)
> vals <- as.numeric(names(table(x)))
> vals
 [1]  0  1  2  3  4  5  6  7  8  9 10
> tx.freq <- as.numeric(table(x))/100
> tx.freq
 [1] 0.01 0.07 0.12 0.16 0.27 0.10 0.07 0.07 0.11 0.01 0.01
> quantile(x)
  0%  25%  50%  75% 100%
   0    3    4    6   10
> quantile.from.freq <- function(vals,freq,quant) {
+ ord <- order(vals)
+ cs <- cumsum(freq[ord])
+ return(vals[max(which(cs<quant))+1])}
> quantile.from.freq(vals,tx.freq,0.5)
[1] 4
> quantile.from.freq(vals,tx.freq,0.25)
[1] 3
> quantile.from.freq(vals,tx.freq,0.75)
[1] 6
> quantile.from.freq(vals,tx.freq,1)
[1] 10

Sean


On 3/3/06 7:30 AM, "Paolo Radaelli" <[hidden email]> wrote:

> Yes, but it works only when you have integer counts for each value.
> When I have only relative frequencies I can't repeat each value a
> non-integer number of times.
> Paolo
>
> ----- Original Message -----
> From: "Roger Bivand" <[hidden email]>
> To: "Paolo Radaelli" <[hidden email]>
> Cc: <[hidden email]>
> Sent: Friday, March 03, 2006 1:18 PM
> Subject: Re: [R] Compute quantiles with values and correspondent frequencies
>
>
>> On Fri, 3 Mar 2006, Paolo Radaelli wrote:
>>
>>> Dear List, quantile(x) function allows to obtain specified quantiles of
>>> a vector of observations x.
>>>
>>> Is there an analogous function to compute quantiles in the case one have
>>> the vector of the observations x and the correspondent vector f of
>>> relative frequencies ?
>>
>> Just use rep():
>>
>> x <- rpois(100, 4) # data
>> quantile(x)
>> tx <- table(x) # frequencies (need to be integer counts)
>> tx
>> v <- as.numeric(names(tx)) # values
>> quantile(rep(v, as.integer(tx)))
>>
>> You have v and tx, so this should work.
>>
>>>
>>> Thank you
>>>
>>> Paolo Radaelli
>>>
>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>>>
>>
>> --
>> Roger Bivand
>> Economic Geography Section, Department of Economics, Norwegian School of
>> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
>> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
>> e-mail: [hidden email]
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Compute quantiles with values and correspondent frequencies

Paolo Radaelli
Yes, it should work.
Thanks

----- Original Message -----
From: "Sean Davis" <[hidden email]>
To: "Paolo Radaelli" <[hidden email]>; <[hidden email]>
Cc: "r-help" <[hidden email]>
Sent: Friday, March 03, 2006 1:49 PM
Subject: Re: [R] Compute quantiles with values and correspondent frequencies


> How about this:
>
> > x <- rpois(100,4)
> > vals <- as.numeric(names(table(x)))
> > vals
>  [1]  0  1  2  3  4  5  6  7  8  9 10
> > tx.freq <- as.numeric(table(x))/100
> > tx.freq
>  [1] 0.01 0.07 0.12 0.16 0.27 0.10 0.07 0.07 0.11 0.01 0.01
> > quantile(x)
>   0%  25%  50%  75% 100%
>    0    3    4    6   10
> > quantile.from.freq <- function(vals,freq,quant) {
> + ord <- order(vals)
> + cs <- cumsum(freq[ord])
> + return(vals[max(which(cs<quant))+1])}
> > quantile.from.freq(vals,tx.freq,0.5)
> [1] 4
> > quantile.from.freq(vals,tx.freq,0.25)
> [1] 3
> > quantile.from.freq(vals,tx.freq,0.75)
> [1] 6
> > quantile.from.freq(vals,tx.freq,1)
> [1] 10
>
> Sean
>
>
> On 3/3/06 7:30 AM, "Paolo Radaelli" <[hidden email]> wrote:
>
> > Yes, but it works only when you have integer counts for each value.
> > When I have only relative frequencies I can't repeat each value a
> > non-integer number of times.
> > Paolo
> >
> > ----- Original Message -----
> > From: "Roger Bivand" <[hidden email]>
> > To: "Paolo Radaelli" <[hidden email]>
> > Cc: <[hidden email]>
> > Sent: Friday, March 03, 2006 1:18 PM
> > Subject: Re: [R] Compute quantiles with values and correspondent
frequencies
> >
> >
> >> On Fri, 3 Mar 2006, Paolo Radaelli wrote:
> >>
> >>> Dear List, quantile(x) function allows to obtain specified quantiles
of
> >>> a vector of observations x.
> >>>
> >>> Is there an analogous function to compute quantiles in the case one
have

> >>> the vector of the observations x and the correspondent vector f of
> >>> relative frequencies ?
> >>
> >> Just use rep():
> >>
> >> x <- rpois(100, 4) # data
> >> quantile(x)
> >> tx <- table(x) # frequencies (need to be integer counts)
> >> tx
> >> v <- as.numeric(names(tx)) # values
> >> quantile(rep(v, as.integer(tx)))
> >>
> >> You have v and tx, so this should work.
> >>
> >>>
> >>> Thank you
> >>>
> >>> Paolo Radaelli
> >>>
> >>>
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> [hidden email] mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >>>
> >>
> >> --
> >> Roger Bivand
> >> Economic Geography Section, Department of Economics, Norwegian School
of
> >> Economics and Business Administration, Helleveien 30, N-5045 Bergen,
> >> Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
> >> e-mail: [hidden email]
> >>
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Compute quantiles with values and correspondent frequencies

Frank Harrell
In reply to this post by Roger Bivand
Roger Bivand wrote:

> On Fri, 3 Mar 2006, Paolo Radaelli wrote:
>
>
>>Dear List, quantile(x) function allows to obtain specified quantiles of
>>a vector of observations x.
>>
>>Is there an analogous function to compute quantiles in the case one have
>>the vector of the observations x and the correspondent vector f of
>>relative frequencies ?
>
>
> Just use rep():
>
> x <- rpois(100, 4) # data
> quantile(x)
> tx <- table(x) # frequencies (need to be integer counts)
> tx
> v <- as.numeric(names(tx)) # values
> quantile(rep(v, as.integer(tx)))
>
> You have v and tx, so this should work.
>
>
>>Thank you
>>
>>Paolo Radaelli
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>

Look at wtd.quantile in the Hmisc package.

Frank

--
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Frank Harrell
Department of Biostatistics, Vanderbilt University