3

Say I have this data:

n = 1e3
x = rnorm(n)
linear_process = 1 + 5*x
y = rbinom(n = n, size = 1, prob = plogis(linear_process))
plot(x,y)

enter image description here

I fit a GLM and make predictions:

m = glm(y~x, family = binomial(link = 'logit'))
y_hat = predict(m, type = 'response')

and when I plot y_hat as points I get what I expected:

plot(x,y)
points(x,y_hat, col = 'blue')

enter image description here

but when I plot the predicted probabilities as a line I get this weird thing:

plot(x,y)
lines(x,y_hat, col = 'blue')

enter image description here

Am I doing something wrong with predict? or plot?

invictus
  • 1,821
  • 3
  • 25
  • 30

0 Answers0