1

I have a dataset of choices on a task (either 1 or 0) given a variable x. To use mtcars as an example

#binomial_smooth() from https://ggplot2.tidyverse.org/reference/geom_smooth.html
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

#plot
ggplot(data = mtcars, aes(x = disp, y = am)) + geom_point(alpha = 0.5) + binomial_smooth()

#create a model
model <- glm(am ~ disp, family = "binomial", data = mtcars)

enter image description here

where am would be the subjects choices and disp the x variable. I'd like to work out the value of x +/- SE (which I imagine is what binomial_smooth is plotting though I could be wrong) for the binary variable = 0.5.

Using mtcars, I'd like to find out for which disp +/- SE am = 0.5. Looked around and just been getting more confused so any help would be much appreciated!

best,

Robert Hickman
  • 869
  • 1
  • 6
  • 22
  • maybe following helps [link](https://stackoverflow.com/questions/8496072/extract-standard-errors-from-glm)? – CER Jul 01 '18 at 20:15
  • 1
    If I understand correctly, you're after something like this. https://stats.stackexchange.com/questions/44790/inverse-prediction-from-binomial-glm-how-to-obtain-confidence-intervals – Roman Luštrik Jul 01 '18 at 21:57

1 Answers1

0

Ok, so I figured this out after following a rabbit hole from Roman Luštrik (cheers!).

Uses the MASS package and a function used to calculate the LD50. Also allow for manually choosing a p value to look for.

library(ggplot2)
library(MASS)
#binomial_smooth() from https://ggplot2.tidyverse.org/reference/geom_smooth.html
binomial_smooth <- function(...) {
  geom_smooth(method = "glm", method.args = list(family = "binomial"), ...)
}

#create a model
model <- glm(am ~ disp, family = "binomial", data = mtcars)

#get the 'LD50'- the point at which the binomial regression crosses 50%
LD50 <- dose.p(model, p = 0.5)

#print the details
print(LD50)

#replot the figure with the LD50 vlines
ggplot(data = mtcars, aes(x = disp, y = am)) + 
  geom_point(alpha = 0.5) + 
  binomial_smooth() +
  geom_vline(xintercept = LD50[[1]])

enter image description here

Robert Hickman
  • 869
  • 1
  • 6
  • 22