[R] ggplot2: stat_smooth for family=binomial with cbind(Y, N) formula
Michael Friendly
friendly at yorku.ca
Tue Dec 17 14:57:35 CET 2013
With ggplot2, I can plot the glm stat_smooth for binomial data when the
response is binary or
a two-level factor as follows:
data("Donner", package="vcdExtra")
ggplot(Donner, aes(age, survived)) +
geom_point(position = position_jitter(height = 0.02, width = 0)) +
stat_smooth(method = "glm", family = binomial, formula = y ~ x,
alpha = 0.2, size=2)
But how can I specify the formula for stat_smooth when the response is
cbind(successes, failures)?
The equivalent with plot (minus the confidence band) for the example I
want is:
data("SpaceShuttle", package="vcd")
> head(SpaceShuttle, 5)
FlightNumber Temperature Pressure Fail nFailures Damage
1 1 66 50 no 0 0
2 2 70 50 yes 1 4
3 3 69 50 no 0 0
4 4 80 50 <NA> NA NA
5 5 68 50 no 0 0
>
plot(nFailures/6 ~ Temperature, data = SpaceShuttle,
xlim = c(30, 81), ylim = c(0,1),
main = "NASA Space Shuttle O-Ring Failures",
ylab = "Estimated failure probability",
xlab = "Temperature (degrees F)",
pch = 19, col = "blue", cex=1.2)
fm <- glm(cbind(nFailures, 6 - nFailures) ~ Temperature,
data = SpaceShuttle,
family = binomial)
pred <- predict(fm, data.frame(Temperature = 30 : 81), se=TRUE)
lines(30 : 81,
predict(fm, data.frame(Temperature = 30 : 81), type = "response"),
lwd = 3)
--
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