On 4/17/2014 5:44 AM, Michael Friendly wrote:
I know I can do that.  My example was just a toy version of a more
complex graph I
generate on the logit scale, and save as gg.
I wanted to know if there was a way to transform it to the probability
scale by using

gg + coord_trans()
with some suitable argument(s)

for example, this *does* work to transform x -> log(x)

gg + coord_trans(x="log")

Reading the documentation in scales, I tried

plogis_trans <- function () {
     probability_trans("logis")
}

but I get errors when I try to use it:

 > gg + coord_trans(y="plogis")
Error in if (zero_range(range)) { : missing value where TRUE/FALSE needed
In addition: Warning message:
In qfun(x, ...) : NaNs produced

The logis distribution (q/p functions) is the inverse of the one that you want for this transformation. As such, you need to set up a custom transformation.

inverse_logit_trans <- trans_new("inverse logit",
                                 transform = plogis,
                                 inverse = qlogis)

gg + coord_trans(ytrans = inverse_logit_trans)

gg + scale_y_continuous(trans = inverse_logit_trans)

The difference between these is that coord_trans only transforms the coordinate scale; it does not change the location of the breaks or the labels associated with them. The trans argument in a scale, on the other hand, determines appropriate breaks slightly differently but sill labels them based on the data (not transformed) values.

There are two ways to get what you want: do the transformations in the aesthetics, or create a break determining function for the custom transformation which works on the transformed scale as well as forcing the labeling on the transformed scale.

ggplot(pred, aes(x=Age, y=plogis(fit))) +
  geom_line(size = 2) + theme_bw() +
  geom_ribbon(aes(ymin = plogis(fit - 1.96 * se.fit),
ymax = plogis(fit + 1.96 * se.fit)), alpha = 0.2, color = "transparent") +
  labs(x = "Age", y = "Probability (Better)")


OR

library("functional")
inverse_logit_brks_trans <-
  trans_new("inverse logit",
            transform = plogis,
            inverse = qlogis,
            breaks = Compose(plogis, extended_breaks(), qlogis),
            format = Compose(plogis, format_format()))

gg + coord_trans(ytrans = inverse_logit_brks_trans)

gg + scale_y_continuous(trans = inverse_logit_brks_trans)

Note that the coord_trans version of this second one ignores the breaks and labels aspects of the defined transformation. You need to use the latter (passing it to scale_y_continuous) to get the full effect of what you showed in gg2.


-Michael

On 4/17/2014 6:23 AM, ONKELINX, Thierry wrote:
Dear Michael,

You can use geom_smooth directly.

ggplot(pred, aes(x = Age, y = Better)) + geom_smooth(method = "glm",
family = binomial)

Best regards,

Thierry

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
+ 32 2 525 02 51
+ 32 54 43 61 85
thierry.onkel...@inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no
more than asking him to perform a post-mortem examination: he may be
able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does
not ensure that a reasonable answer can be extracted from a given body
of data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] Namens Michael Friendly
Verzonden: donderdag 17 april 2014 4:03
Aan: R-help
Onderwerp: [R] ggplot2: using coord_trans for logit -> probability

I'm trying to see if & how I can use coord_trans() with ggplot2 to
transform the Y axis of a plot on the logit scale to the probability
scale, as opposed to  recalculating everything "manually" and
constructing a new plot.
Here is a simple example of the 'base' plot I'd like to transform:

data(Arthritis, package="vcdExtra")
Arthritis$Better <- as.numeric(Arthritis$Improved > "None")
arth.logistic <- glm(Better ~ Age, data=Arthritis, family=binomial)

# get fitted values on the logit scale
pred <- data.frame(Arthritis,
                     predict(arth.logistic, se.fit=TRUE))
library(ggplot2)
library(scales)
# plot on logit scale
gg <- ggplot(pred, aes(x=Age, y=fit)) +
    geom_line(size = 2) + theme_bw() +
    geom_ribbon(aes(ymin = fit - 1.96 * se.fit,
                    ymax = fit + 1.96 * se.fit,), alpha = 0.2,  color =
"transparent") +
    labs(x = "Age", y = "Log odds (Better)") gg

Things I've tried that don't work:

  > gg + coord_trans(ytrans="logis")
Error in get(as.character(FUN), mode = "function", envir = envir) :
    object 'logis_trans' of mode 'function' was not found  >  > gg +
coord_trans(ytrans=probability_trans("logis"))
Error in if (zero_range(range)) { : missing value where TRUE/FALSE
needed In addition: Warning message:
In qfun(x, ...) : NaNs produced
  >

Doing what I want "manually":

# doing it manually
pred2 <- within(pred, {
               prob  <- plogis(fit)
               lower <- plogis(fit - 1.96 * se.fit)
               upper <- plogis(fit + 1.96 * se.fit)
               })


gg2 <- ggplot(pred2, aes(x=Age, y=prob)) +
    geom_line(size = 2) + theme_bw() +
    geom_ribbon(aes(ymin = lower,
                    ymax = upper), alpha = 0.2,  color = "transparent") +
    labs(x = "Age", y = "Probability (Better)")
gg2



--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept. & Chair, Quantitative Methods
York University      Voice: 416 736-2100 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

______________________________________________
R-help@r-project.org mailing list
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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de
schrijver weer en binden het INBO onder geen enkel beding, zolang dit
bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of
the writer and may not be regarded as stating an official position of
INBO, as long as the message is not confirmed by a duly signed document.




______________________________________________
R-help@r-project.org mailing list
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 via email to