Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fit <- glm(am ~ mpg, family=binomial(), data=mtcars) # Logistic regression
- p <- c(.05,.10,.20,.50,.80,.90,.95) # Specify probabilities
- x <- (log(p/(1-p)) - coef(fit)[1])/coef(fit)[2] # Compute corresponding x's
- i <- ordered(1:length(p)) # Identify p's and x's for plotting
- #
- # Compute the fitted curve.
- #
- Z <- data.frame(mpg=with(mtcars, seq(min(mpg), max(mpg), length.out=101)))
- Z$am <- predict(fit, Z, type="response")
- #
- # Plot everything.
- #
- library (ggplot2)
- ggplot(mtcars, aes(mpg, am)) +
- geom_hline(aes(yintercept=p, color=i), data.frame(p, i), alpha=0.8, show.legend=FALSE)+
- geom_vline(aes(xintercept=x, color=i), data.frame(x, i), alpha=0.8, show.legend=FALSE)+
- geom_point(size=2, alpha=0.25, fill="Black", shape=21) +
- geom_line(aes(mpg, am), Z, size=1.25, color="#303030") +
- ylab("Probability(am=1)") +
- ggtitle("Probability grid with associated regressor values") +
- theme(panel.grid=element_blank())
Add Comment
Please, Sign In to add comment