[R] Conditioning plots (wth coplot function) with logistic regression curves

Michael Friendly friendly at yorku.ca
Mon Sep 23 01:46:04 CEST 2013


On 9/21/2013 11:12 PM, Kiyoshi Sasaki wrote:
> I have been trying to produce a conditional plot using coplot function (http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/coplot.html) for a binary response ("Presence" in my case) variable and one continuous variable ("Overstory") given a specific levels of the other continuous variable ("Ivy"). But, my codes produces an overlapping graph. Also, I want to use three equal intervals for "Ivy" (i.e.,33.3 each), but I could not figure out how. Here is my data and codes I used:
>

If you feel it hurts because you are banging your head into a wall 
trying to get coplot to do this, the answer is: Don't do that!

Instead, you might consider using ggplot2, which handles this case
nicely, as far as I can tell from your description.

But first a due diligence caveat:  Say you come to me for consulting
on this little plotting question. I look at your data frame, dat,
and I see there are a number of other variables that might explain
Presence, so maybe the marginal plot that ignores them could be
misleading, e.g., any of Moist, Leaf, Prey, ... could moderate
the relation between overstory and presence, but you won't see that
in a marginal plot.


Here are a couple of quick ggplot examples, plotting classes of Ivy in the
same plot frame, and on the probability scale.

library(ggplot2)
ggplot(dat, aes(Overstory, Presence), color=Ivy>50 ) +
   stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy>50))

dat$Ivy3 <- factor(cut(dat$Ivy,3))
plt <- ggplot(dat, aes(Overstory, Presence), color=Ivy3 ) +
   stat_smooth(method="glm", family=binomial, formula= y~x, alpha=0.3, 
aes(fill=Ivy3))
plt

If you want separate panels, try something like

plt + facet_grid(. ~ Ivy3)

For plots on the logit scale, try something like
logit <- function(p) log(p)/log(1-p), then

plt + coord_trans(y="logit")

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



More information about the R-help mailing list