Problem with boxcox_trans

93 views
Skip to first unread message

Mark

unread,
Nov 20, 2012, 10:51:50 AM11/20/12
to ggp...@googlegroups.com
The boxcox_trans seems to return the wrong thing when using p=0. Rather than return a transformation it returns the function log_trans.
 
> boxcox_trans(p=0)
function (base = exp(1)) 
{
    trans <- function(x) log(x, base)
    inv <- function(x) base^x
    trans_new(str_c("log-", format(base)), trans, inv, log_breaks(base = base), 
        domain = c(1e-100, Inf))
}
<environment: namespace:scales>
> boxcox_trans(p=-1)
Transformer:  pow--1 
 
This seems to work fine
> qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0.5))
 
This gives an error
> qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0))
 
But this works
> qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0)())
 
Mark Lyman

Dianne Cook

unread,
Nov 20, 2012, 11:13:09 AM11/20/12
to Mark, ggp...@googlegroups.com
Mark,

This is because it is understood when p=0, that you will actually make a log transformation.

cheers,
Di

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---------------------------
Di Cook




Mark

unread,
Nov 20, 2012, 11:27:40 AM11/20/12
to ggp...@googlegroups.com, Mark
But wouldn't it make more sense to return the transformation object itself rather than the function that returns the transformation object? Otherwise you are force to treat 0 differently.
 
What I'm suggesting is that boxcox_trans should be something like this
 
boxcox_trans <- function(p){
    if (abs(p) < 1e-07) 
        return(log_trans()) # changed from return(log_trans)
    trans <- function(x) (x^p - 1)/p * sign(x - 1)
    inv <- function(x) (abs(x) * p + 1 * sign(x))^(1/p)
    trans_new(str_c("pow-", format(p)), trans, inv)
}

Mark

unread,
Nov 26, 2012, 2:48:18 PM11/26/12
to ggplot2
Judging by Dianne Cook's response and another response that apparently
didn't get posted to the group, I think my concern is misunderstood.
I understand that a Box-Cox Transformation with p=0 is defined as the
log transformation, but I believe it is a bug that the function
boxcox_trans(p=0) returns an object of class "function" (the log_trans
function) and not an object with class "trans" as all of the other
*_trans functions and boxcox_trans with any other values of p do.

> class(log_trans(base=10))
[1] "trans"
> class(log_trans())
[1] "trans"
> class(logit_trans())
[1] "trans"
> class(atanh_trans())
[1] "trans"
> class(boxcox_trans(p=0.5))
[1] "trans"
> class(boxcox_trans(p=0))
[1] "function"

It seems that rather than return log_trans boxcox_trans should return
log_trans(base=exp(1)).

Mark Lyman

On Nov 20, 9:27 am, Mark <mark.ly...@gmail.com> wrote:
> But wouldn't it make more sense to return the transformation object
> itself rather than the function that returns the transformation object?
> Otherwise you are force to treat 0 differently.
>
> What I'm suggesting is that boxcox_trans should be something like this
>
> boxcox_trans <- function(p){
>
>     if (abs(p) < 1e-07)
>         return(log_trans()) # changed from return(log_trans)
>     trans <- function(x) (x^p - 1)/p * sign(x - 1)
>     inv <- function(x) (abs(x) * p + 1 * sign(x))^(1/p)
>     trans_new(str_c("pow-", format(p)), trans, inv)
>
>
>
> }
> On Tuesday, November 20, 2012 9:13:16 AM UTC-7, Di wrote:
> > Mark,
>
> > This is because it is understood when p=0, that you will actually make a
> > log transformation.
>
> > cheers,
> > Di
>
> >  On Nov 20, 2012, at 9:51 AM, Mark wrote:
>
> >  The boxcox_trans seems to return the wrong thing when using p=0. Rather
> > than return a transformation it returns the function log_trans.
>
> > > boxcox_trans(p=0)function (base = exp(1))
> > {
> >     trans <- function(x) log(x, base)
> >     inv <- function(x) base^x
> >     trans_new(str_c("log-", format(base)), trans, inv, log_breaks(base = base),
> >         domain = c(1e-100, Inf))
> > }
> > <environment: namespace:scales>> boxcox_trans(p=-1)Transformer:  pow--1
>
> > This seems to work fine
>
> > > qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0.5))
>
> > This gives an error
>
> > > qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0))
>
> > But this works> qplot(sample=rlnorm(20)) + scale_y_continuous(trans=boxcox_trans(0)())
>
> > Mark Lyman
>
> > --
> > You received this message because you are subscribed to the ggplot2
> > mailing list.
> > Please provide a reproducible example:
> >https://github.com/hadley/devtools/wiki/Reproducibility
>
> > To post: email ggp...@googlegroups.com <javascript:>
> > To unsubscribe: email ggplot2+u...@googlegroups.com <javascript:>
> > More options:http://groups.google.com/group/ggplot2
>
> >  ---------------------------
> > Di Cook
> > vis...@gmail.com <javascript:>- Hide quoted text -
>
> - Show quoted text -

Hadley Wickham

unread,
Nov 26, 2012, 3:29:11 PM11/26/12
to Mark, ggplot2
> Judging by Dianne Cook's response and another response that apparently
> didn't get posted to the group, I think my concern is misunderstood.
> I understand that a Box-Cox Transformation with p=0 is defined as the
> log transformation, but I believe it is a bug that the function
> boxcox_trans(p=0) returns an object of class "function" (the log_trans
> function) and not an object with class "trans" as all of the other
> *_trans functions and boxcox_trans with any other values of p do.

Issue filed at https://github.com/hadley/scales/issues/31

Hadley

--
RStudio / Rice University
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages