Michael Friendly
2014-Apr-17 02:03 UTC
[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
Tim Marcella
2014-Apr-17 02:37 UTC
[R] ggplot2: using coord_trans for logit -> probability
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 <friendly@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 timmarcella@gmail.com [[alternative HTML version deleted]]
ONKELINX, Thierry
2014-Apr-17 10:23 UTC
[R] ggplot2: using coord_trans for logit -> probability
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.Onkelinx at 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-bounces at r-project.org [mailto:r-help-bounces at 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 at 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.