Quantiles with ordered categories

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Quantiles with ordered categories

Matthias Gondan
Dear R users,

This works:

quantile(1:10, probs=0.5)

This fails (obviously):

quantile(factor(1:10), probs=0.5)

But why do quantiles for ordered factors not work either?

quantile(ordered(1:10), probs=0.5)

Is it because interpolation (see the optional type argument) is not defined? Is there an elegant workaround?

Thank you.

Best wishes,

Matthias

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Quantiles with ordered categories

Bert Gunter-2
Inline.
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Mar 14, 2017 at 12:36 PM,  <[hidden email]> wrote:

> Dear R users,
>
> This works:
>
> quantile(1:10, probs=0.5)
>
> This fails (obviously):
>
> quantile(factor(1:10), probs=0.5)
>
> But why do quantiles for ordered factors not work either?
>
> quantile(ordered(1:10), probs=0.5)
>
> Is it because interpolation (see the optional type argument) is not defined?
Yes.


Is there an elegant workaround?
No. How can there be? By definition, all that is assumed by an ordered
factor is an ordering of the categories. How can you "interpolate" in
ordered(letters[1:3]) . ASAIK there is no "a.5"  .

-- Bert



>
> Thank you.
>
> Best wishes,
>
> Matthias
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Quantiles with ordered categories

Matthias Gondan
I found it:

quantile(ordered(1:10), probs=0.5, type=1)

works, because type=1 seems to round up or down, whatever. The default option for is 7, which wants to interpolate, and then produces the error.

Two options come to my mind:

- The error message could be improved.
- The default type could be 1 if the data is from ordered categories.
- Or both.

It is probably a little thing to fix, but I lack the skills to do this myself.

Best wishes,

Matthias

Von: Bert Gunter
Gesendet: Dienstag, 14. März 2017 21:34
An: [hidden email]
Cc: [hidden email]
Betreff: Re: [R] Quantiles with ordered categories

Inline.
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Mar 14, 2017 at 12:36 PM,  <[hidden email]> wrote:

> Dear R users,
>
> This works:
>
> quantile(1:10, probs=0.5)
>
> This fails (obviously):
>
> quantile(factor(1:10), probs=0.5)
>
> But why do quantiles for ordered factors not work either?
>
> quantile(ordered(1:10), probs=0.5)
>
> Is it because interpolation (see the optional type argument) is not defined?
Yes.


Is there an elegant workaround?
No. How can there be? By definition, all that is assumed by an ordered
factor is an ordering of the categories. How can you "interpolate" in
ordered(letters[1:3]) . ASAIK there is no "a.5"  .

-- Bert



>
> Thank you.
>
> Best wishes,
>
> Matthias
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Quantiles with ordered categories

R help mailing list-2
In reply to this post by Bert Gunter-2
You could round the quantiles of the codes of the ordered factor to
come up with a reasonable result.  E.g.,

quantile.ordered <- function(x, ...)
ordered(levels(x)[as.integer(quantile(as.integer(x), ...))],
levels=levels(x))

> unCut <- log2(2:30)
> Cut <- cut(unCut, breaks=0:6, ordered_result=TRUE)
> quantile(unCut)
      0%      25%      50%      75%     100%
1.000000 3.169925 4.000000 4.523562 4.906891
> quantile(Cut)
[1] (0,1] (3,4] (3,4] (4,5] (4,5]
Levels: (0,1] < (1,2] < (2,3] < (3,4] < (4,5] < (5,6]


Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Tue, Mar 14, 2017 at 1:34 PM, Bert Gunter <[hidden email]> wrote:

> Inline.
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Tue, Mar 14, 2017 at 12:36 PM,  <[hidden email]> wrote:
>> Dear R users,
>>
>> This works:
>>
>> quantile(1:10, probs=0.5)
>>
>> This fails (obviously):
>>
>> quantile(factor(1:10), probs=0.5)
>>
>> But why do quantiles for ordered factors not work either?
>>
>> quantile(ordered(1:10), probs=0.5)
>>
>> Is it because interpolation (see the optional type argument) is not defined?
> Yes.
>
>
> Is there an elegant workaround?
> No. How can there be? By definition, all that is assumed by an ordered
> factor is an ordering of the categories. How can you "interpolate" in
> ordered(letters[1:3]) . ASAIK there is no "a.5"  .
>
> -- Bert
>
>
>
>>
>> Thank you.
>>
>> Best wishes,
>>
>> Matthias
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Quantiles with ordered categories

Martin Maechler
In reply to this post by Matthias Gondan
>>>>>   <[hidden email]>
>>>>>     on Tue, 14 Mar 2017 21:54:42 +0100 writes:

    > I found it:
    > quantile(ordered(1:10), probs=0.5, type=1)

    > works, because type=1 seems to round up or down, whatever. The default option for is 7, which wants to interpolate, and then produces the error.

    > Two options come to my mind:

    > - The error message could be improved.
    > - The default type could be 1 if the data is from ordered categories.
    > - Or both.

Well, it is remarkable that nobody looks at the help page (or
the source code) of quantile() to be informed.

In 'Details' it has contained

    Types 1 and 3 can be used for class; "Date" and for ordered factors.

since Oct 15, 2009 ...

But I agree that the error message can be improved and have done
so now, so that instead of

    "factors are not allowed"

you now get

    "'type' must be 1 or 3 for ordered factors"


    > It is probably a little thing to fix, but I lack the skills to do this myself.

(Really? -- After seeing the change you will agree it was easy .. ?)


Thank you for the suggestion.

Best regards,

Martin Maechler
ETH Zurich


    > Best wishes,
    > Matthias


    > Von: Bert Gunter
    > Gesendet: Dienstag, 14. März 2017 21:34
    > An: [hidden email]
    > Cc: [hidden email]
    > Betreff: Re: [R] Quantiles with ordered categories

    > Inline.
    > Bert Gunter

    > "The trouble with having an open mind is that people keep coming along
    > and sticking things into it."
    > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


    > On Tue, Mar 14, 2017 at 12:36 PM,  <[hidden email]> wrote:
    >> Dear R users,
    >>
    >> This works:
    >>
    >> quantile(1:10, probs=0.5)
    >>
    >> This fails (obviously):
    >>
    >> quantile(factor(1:10), probs=0.5)
    >>
    >> But why do quantiles for ordered factors not work either?
    >>
    >> quantile(ordered(1:10), probs=0.5)
    >>
    >> Is it because interpolation (see the optional type argument) is not defined?
    > Yes.


    > Is there an elegant workaround?
    > No. How can there be? By definition, all that is assumed by an ordered
    > factor is an ordering of the categories. How can you "interpolate" in
    > ordered(letters[1:3]) . ASAIK there is no "a.5"  .

    > -- Bert



    >>
    >> Thank you.
    >>
    >> Best wishes,
    >>
    >> Matthias
    >>
    >> [[alternative HTML version deleted]]
    >>
    >> ______________________________________________
    >> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    >> https://stat.ethz.ch/mailman/listinfo/r-help
    >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    >> and provide commented, minimal, self-contained, reproducible code.


    > [[alternative HTML version deleted]]

    > ______________________________________________
    > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    > https://stat.ethz.ch/mailman/listinfo/r-help
    > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    > and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.