I think all you have to do is add type="response" to your call for the
predictions.

Does this work for you

# get fitted values on the logit scale
pred <- data.frame(Arthritis,
                   predict(arth.logistic, se.fit=TRUE,type="response"))

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

-Tim


On Wed, Apr 16, 2014 at 7:03 PM, Michael Friendly <frien...@yorku.ca> wrote:

> 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.
>



-- 
Tim Marcella
508.498.2989
timmarce...@gmail.com

        [[alternative HTML version deleted]]

______________________________________________
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