quantile() type 1 for some ordered factors in R-devel

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

quantile() type 1 for some ordered factors in R-devel

Tobias Rockel
Hi,

In R-devel (2020-05-17 r78478) quantile() type 1 seems to behave a little
bit strange for some ordered factors:
quantile(factor(1:3, ordered = TRUE), 0.5, type = 1)
returns “2” as expected. But
quantile(factor(2:4, ordered = TRUE), 0.5, type = 1)
returns “4” and
quantile(factor(3:5, ordered = TRUE), 0.5, type = 1)
returns “NA”. Furthermore, the function returns “NA” for calls like
quantile(factor(c("a", "b", "c"), ordered = TRUE), 0.5, type = 1)

In R 4.0.0 everything seems fine (return values “2”, “3”, “4”, “b”). If the
vectors are treated as numeric, everything seems to work fine in R-devel,
too. For example
quantile(3:5, 0.5, type = 1)
returns “4” in R-devel and R 4.0.0.

Best regards,
Tobias Rockel

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: quantile() type 1 for some ordered factors in R-devel

Trang Le
Hi Tobias,

I believe this is due to the implementation of the new method for c():
c.factor() on May 11, 2020. I have very recently experienced issues around
this addition as well.

For this particular instance, the following line of code in
quantile.default() no longer gives "integer" but instead yields "factor".

x <- c(x[1L], x[1L], x, x[n], x[n])

Hence, the following calculation qs results in named factor instead of
integer, which is not expected as input in the final

qs <- factor(qs, levels = seq_along(lx), labels = lx, ordered = TRUE)

Personally, I'm a bit surprised that this change is being made on a minor
release.

Best,
Trang Le

On Mon, May 18, 2020 at 12:36 PM Tobias Rockel <[hidden email]> wrote:

> Hi,
>
> In R-devel (2020-05-17 r78478) quantile() type 1 seems to behave a little
> bit strange for some ordered factors:
> quantile(factor(1:3, ordered = TRUE), 0.5, type = 1)
> returns “2” as expected. But
> quantile(factor(2:4, ordered = TRUE), 0.5, type = 1)
> returns “4” and
> quantile(factor(3:5, ordered = TRUE), 0.5, type = 1)
> returns “NA”. Furthermore, the function returns “NA” for calls like
> quantile(factor(c("a", "b", "c"), ordered = TRUE), 0.5, type = 1)
>
> In R 4.0.0 everything seems fine (return values “2”, “3”, “4”, “b”). If the
> vectors are treated as numeric, everything seems to work fine in R-devel,
> too. For example
> quantile(3:5, 0.5, type = 1)
> returns “4” in R-devel and R 4.0.0.
>
> Best regards,
> Tobias Rockel
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: quantile() type 1 for some ordered factors in R-devel

Kurt Hornik-5
In reply to this post by Tobias Rockel
>>>>> Tobias Rockel writes:

Thanks for spotting this, and also to Hadley for reporting to me
directly.

Fixed now with c78501.

Best
-k

> Hi,
> In R-devel (2020-05-17 r78478) quantile() type 1 seems to behave a little
> bit strange for some ordered factors:
> quantile(factor(1:3, ordered = TRUE), 0.5, type = 1)
> returns “2” as expected. But
> quantile(factor(2:4, ordered = TRUE), 0.5, type = 1)
> returns “4” and
> quantile(factor(3:5, ordered = TRUE), 0.5, type = 1)
> returns “NA”. Furthermore, the function returns “NA” for calls like
> quantile(factor(c("a", "b", "c"), ordered = TRUE), 0.5, type = 1)

> In R 4.0.0 everything seems fine (return values “2”, “3”, “4”, “b”). If the
> vectors are treated as numeric, everything seems to work fine in R-devel,
> too. For example
> quantile(3:5, 0.5, type = 1)
> returns “4” in R-devel and R 4.0.0.

> Best regards,
> Tobias Rockel

> [[alternative HTML version deleted]]

> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: quantile() type 1 for some ordered factors in R-devel

Trang Le
Hi Kurt,

Thank you for fixing quantile(). However, do you think c.factor() can
potentially break more functions? For example, with this new change,
classification from the partykit package using predict() comes back NA
because of this:

https://github.com/cran/partykit/blob/597245ef3dfc98411ce919b74c68ba565f077c47/R/party.R#L500

I understand that most of the fixes will probably be simple with
as.numeric() or as.integer(), but tracing down these breaks can be
time-consuming <https://twitter.com/trang1618/status/1260906905041432578>.
What about a warning whenever code that would trigger c.factor() is called?
This way users are given a chance to update packages and code.

Thanks,
Trang

On Wed, May 20, 2020 at 1:53 AM Kurt Hornik <[hidden email]> wrote:

> >>>>> Tobias Rockel writes:
>
> Thanks for spotting this, and also to Hadley for reporting to me
> directly.
>
> Fixed now with c78501.
>
> Best
> -k
>
> > Hi,
> > In R-devel (2020-05-17 r78478) quantile() type 1 seems to behave a little
> > bit strange for some ordered factors:
> > quantile(factor(1:3, ordered = TRUE), 0.5, type = 1)
> > returns “2” as expected. But
> > quantile(factor(2:4, ordered = TRUE), 0.5, type = 1)
> > returns “4” and
> > quantile(factor(3:5, ordered = TRUE), 0.5, type = 1)
> > returns “NA”. Furthermore, the function returns “NA” for calls like
> > quantile(factor(c("a", "b", "c"), ordered = TRUE), 0.5, type = 1)
>
> > In R 4.0.0 everything seems fine (return values “2”, “3”, “4”, “b”). If
> the
> > vectors are treated as numeric, everything seems to work fine in R-devel,
> > too. For example
> > quantile(3:5, 0.5, type = 1)
> > returns “4” in R-devel and R 4.0.0.
>
> > Best regards,
> > Tobias Rockel
>
> >       [[alternative HTML version deleted]]
>
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: quantile() type 1 for some ordered factors in R-devel

Kurt Hornik-5
>>>>> Trang Le writes:

> Hi Kurt,
> Thank you for fixing quantile(). However, do you think c.factor() can
> potentially break more functions? For example, with this new change,
> classification from the partykit package using predict() comes back NA because
> of this:

> https://github.com/cran/partykit/blob/597245ef3dfc98411ce919b74c68ba565f077c47/R/party.R#L500

All issues in the CRAN checks were identified and reported, and should
be fixed shortly.

Best
-k

> I understand that most of the fixes will probably be simple with as.numeric()
> or as.integer(), but tracing down these breaks can be time-consuming. What
> about a warning whenever code that would trigger c.factor() is called? This
> way users are given a chance to update packages and code.

> Thanks,
> Trang

> On Wed, May 20, 2020 at 1:53 AM Kurt Hornik <[hidden email]> wrote:

>>>>>> Tobias Rockel writes:
   
>     Thanks for spotting this, and also to Hadley for reporting to me
>     directly.
   
>     Fixed now with c78501.
   
>     Best
>     -k
   

>> Hi,
>> In R-devel (2020-05-17 r78478) quantile() type 1 seems to behave a
>     little
>> bit strange for some ordered factors:
>> quantile(factor(1:3, ordered = TRUE), 0.5, type = 1)
>> returns “2” as expected. But
>> quantile(factor(2:4, ordered = TRUE), 0.5, type = 1)
>> returns “4” and
>> quantile(factor(3:5, ordered = TRUE), 0.5, type = 1)
>> returns “NA”. Furthermore, the function returns “NA” for calls like
>> quantile(factor(c("a", "b", "c"), ordered = TRUE), 0.5, type = 1)
   
>> In R 4.0.0 everything seems fine (return values “2”, “3”, “4”, “b”). If
>     the
>> vectors are treated as numeric, everything seems to work fine in
>     R-devel,
>> too. For example
>> quantile(3:5, 0.5, type = 1)
>> returns “4” in R-devel and R 4.0.0.
   
>> Best regards,
>> Tobias Rockel
   
>>        [[alternative HTML version deleted]]
   
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
   
>     ______________________________________________
>     [hidden email] mailing list
>     https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel