Skip to content

Instantly share code, notes, and snippets.

@goldingn
Created July 21, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldingn/577d1d6b7eb14cb981d8 to your computer and use it in GitHub Desktop.
Save goldingn/577d1d6b7eb14cb981d8 to your computer and use it in GitHub Desktop.
barplot on binomial GLM results as an option for this series of tweets: https://twitter.com/SophDavison1/status/623491784530915328
# set RNG seed
set.seed(1)
# make some fake data
n <- 100
m <- 5
y <- rbinom(n, 1, 0.85)
x <- sample(letters[1:m], n, replace = TRUE)
# fit a logistic regression model
m <- glm(y ~ x, family = binomial)
# get predicted probabilities and standard errors
x_new <- data.frame(x = letters[1:m])
pred <- predict(m, newdata = x_new, type = 'response', se.fit = TRUE)
# make a barplot, saving the bar locations
bp <- barplot(pred$fit, names.arg = x_new$x, ylim = c(0, 1))
# add the error bars (surprisingly hard, this)
arrows(x0 = bp[, 1],
y0 = pred$fit - pred$se.fit,
y1 = pred$fit + pred$se.fit,
angle = 90,
code = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment