Produce some data for logistic GLM:
set.seed(123)
x1 = rnorm(2000)
z = 1 + 3*x1 + 3*exp(x1)
pr = 1/(1+exp(-z))
y = rbinom(2000,1,pr)
df = data.frame(y=y,x1=x1)
Running the model:
mod <- glm(y ~ x1,data=df,family=binomial(link=logit))
Logit plot:
library(visreg)
library(ggplot2)
visreg(mod, 'x1', scale='response', rug=2, gg=TRUE)+
theme_bw(18)
I need to calculate the cutoff of x1 which defines a 50% probability of being y=1.
I guess I need the predict
function:
pred <- predict(mod, type = "response")
EDIT
As suggested below I found the cutoff; however, I would like to perform a ROC analysis in order to verify its specificity and sensibility. Is it sufficient to run this code?
prob=predict(mod,type=c("response"))
df$prob=prob
library(pROC)
g <- roc(y ~ prob, data = df)
plot(g)
g
You can use dose.p
from MASS
. Try out:
library(MASS)
dose.p(mod, p = 0.5)
# Dose SE
#p = 0.5: -0.8457261 0.02039277
Using predict
, x1[as.numeric(names(pred[round(pred, 2) == 0.5]))]
provide points from x1
that are close (to the nearest hundredth) to the cutoff
[1] -0.8497043 -0.8490611 -0.8445834 -0.8468964 -0.8491746